diff --git a/parser/parser.go b/parser/parser.go index be81187..4a577c6 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -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"` @@ -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)) diff --git a/render/example_golang.go b/render/example_golang.go index 3665b7c..803bd7c 100644 --- a/render/example_golang.go +++ b/render/example_golang.go @@ -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()) } diff --git a/render/example_golang_test.go b/render/example_golang_test.go index 65223bb..da1b226 100644 --- a/render/example_golang_test.go +++ b/render/example_golang_test.go @@ -1,7 +1,6 @@ package render import ( - "log" "strings" "testing" @@ -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)