Skip to content

Commit

Permalink
added ExternalObjectName to objects
Browse files Browse the repository at this point in the history
  • Loading branch information
matryer committed Feb 24, 2023
1 parent a0027a5 commit 8f1608d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
16 changes: 11 additions & 5 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,13 @@ type Method struct {

// Object describes a data structure that is part of this definition.
type Object struct {
TypeID string `json:"typeID"`
Name string `json:"name"`
Imported bool `json:"imported"`
Fields []Field `json:"fields"`
Comment string `json:"comment"`
TypeID string `json:"typeID"`
ObjectName string `json:"objectName"`
ExternalObjectName string `json:"externalObjectName"`
Name string `json:"name"`
Imported bool `json:"imported"`
Fields []Field `json:"fields"`
Comment string `json:"comment"`
// Metadata are typed key/value pairs extracted from the
// comments.
Metadata map[string]interface{} `json:"metadata"`
Expand Down Expand Up @@ -336,6 +338,10 @@ func (p *Parser) parseObject(pkg *packages.Package, o types.Object, v *types.Str
return p.wrapErr(errors.New(obj.Name+" must be a struct"), pkg, o.Pos())
}
obj.TypeID = o.Pkg().Path() + "." + obj.Name

obj.ObjectName = types.TypeString(o.Type(), func(other *types.Package) string { return "" })
obj.ExternalObjectName = types.TypeString(o.Type(), func(other *types.Package) string { return p.PackageName })

obj.Fields = []Field{}
for i := 0; i < st.NumFields(); i++ {
field, err := p.parseField(pkg, obj.Name, st.Field(i), st.Tag(i))
Expand Down
21 changes: 7 additions & 14 deletions render/example_golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,27 @@ import (
"github.com/pacedotdev/oto/parser"
)

func ObjectGolang(def parser.Definition, parentField *parser.Field, object *parser.Object, tabs int) template.HTML {
func ObjectGolang(def parser.Definition, object *parser.Object, tabs int) template.HTML {
s := &strings.Builder{}
fmt.Fprintf(s, strings.Repeat("\t", tabs))
if parentField != nil {
fmt.Fprintf(s, "%s{\b", parentField.Type.ExternalObjectName)
}
fmt.Fprintf(s, "%s{", object.ExternalObjectName)
for _, field := range object.Fields {
fmt.Fprintf(s, "\n")
fmt.Fprintf(s, strings.Repeat("\t", tabs+1))
fmt.Fprint(s, strings.Repeat("\t", tabs+1))
if field.Type.IsObject {
// object
fieldObject, err := def.Object(field.Type.ObjectName)
if err != nil {
return template.HTML(field.Type.ObjectName + ": " + err.Error())
}
fmt.Fprintf(s, "%s: %s{\n%v", field.Name, field.Type.ExternalObjectName, ObjectGolang(def, &field, fieldObject, tabs+1))
fmt.Fprintf(s, "\n")
fmt.Fprintf(s, strings.Repeat("\t", tabs+1))
fmt.Fprintf(s, "},")
fmt.Fprintf(s, "%s: %v,", field.Name, ObjectGolang(def, fieldObject, tabs+1))
continue
}
// normal field
fmt.Fprintf(s, "%s: %v,", field.Name, jsonStr(field.Metadata["example"]))
}
fmt.Fprintf(s, strings.Repeat("\t", tabs+1))
if parentField != nil {
fmt.Fprintf(s, "}")
}
fmt.Fprintf(s, "\n")
fmt.Fprint(s, strings.Repeat("\t", tabs))
fmt.Fprintf(s, "}")
return template.HTML(s.String())
}

Expand Down
12 changes: 7 additions & 5 deletions render/example_golang_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package render

import (
"log"
"strings"
"testing"

Expand All @@ -19,15 +18,18 @@ func TestExmapleGolang(t *testing.T) {
is.NoErr(err)
inputObject, err := def.Object(def.Services[0].Methods[0].InputObject.ObjectName)
is.NoErr(err) // get inputObject
example := ObjectGolang(def, nil, inputObject, 0)
example := ObjectGolang(def, inputObject, 0)

// err = os.WriteFile("./delete-me-example.go.notgo", []byte(example), 0666)
// f, err := os.Create("./delete-me-example.go.notgo")
// is.NoErr(err) // write file
// defer f.Close()
// f.Write([]byte(example))

log.Printf("### %s ###", example)
// log.Printf("### %s ###", example)

for _, should := range []string{
"OrderField: null",
"GetGreetingsRequest{",
"Page: Page{",
} {
if !strings.Contains(string(example), should) {
t.Errorf("missing: %s", should)
Expand Down

0 comments on commit 8f1608d

Please sign in to comment.