Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions lokerpc/codegen/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ func GenGoClient(w io.Writer, meta lokerpc.Meta) error {
b.WriteString("\n")
b.WriteString("import (\n")
b.WriteString("\t\"context\"\n")
if metaUsesTimestamp(meta) {
b.WriteString("\t\"time\"\n")
}
b.WriteString("\n")
b.WriteString("\t\"github.com/LOKE/pkg/lokerpc\"\n")
b.WriteString(")\n")
Expand Down Expand Up @@ -171,6 +174,51 @@ func GenGoClient(w io.Writer, meta lokerpc.Meta) error {
return b.Flush()
}

func schemaUsesTimestamp(schema jtd.Schema) bool {
if schema.Type == jtd.TypeTimestamp {
return true
}
for _, v := range schema.Definitions {
if schemaUsesTimestamp(v) {
return true
}
}
for _, v := range schema.Properties {
if schemaUsesTimestamp(v) {
return true
}
}
for _, v := range schema.OptionalProperties {
if schemaUsesTimestamp(v) {
return true
}
}
if schema.Elements != nil && schemaUsesTimestamp(*schema.Elements) {
return true
}
if schema.Values != nil && schemaUsesTimestamp(*schema.Values) {
return true
}
return false
}

func metaUsesTimestamp(meta lokerpc.Meta) bool {
for _, v := range meta.Definitions {
if schemaUsesTimestamp(v) {
return true
}
}
for _, iface := range meta.Interfaces {
if iface.RequestTypeDef != nil && schemaUsesTimestamp(*iface.RequestTypeDef) {
return true
}
if iface.ResponseTypeDef != nil && schemaUsesTimestamp(*iface.ResponseTypeDef) {
return true
}
}
return false
}

// Regexp that matches word boundaries,
// e.g.
// "customer_id" -> "CustomerID"
Expand Down
1 change: 1 addition & 0 deletions lokerpc/codegen/testdata/nested.json.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nested

import (
"context"
"time"

"github.com/LOKE/pkg/lokerpc"
)
Expand Down
Loading