Skip to content

Commit 8f1608d

Browse files
committed
added ExternalObjectName to objects
1 parent a0027a5 commit 8f1608d

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

parser/parser.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,13 @@ type Method struct {
100100

101101
// Object describes a data structure that is part of this definition.
102102
type Object struct {
103-
TypeID string `json:"typeID"`
104-
Name string `json:"name"`
105-
Imported bool `json:"imported"`
106-
Fields []Field `json:"fields"`
107-
Comment string `json:"comment"`
103+
TypeID string `json:"typeID"`
104+
ObjectName string `json:"objectName"`
105+
ExternalObjectName string `json:"externalObjectName"`
106+
Name string `json:"name"`
107+
Imported bool `json:"imported"`
108+
Fields []Field `json:"fields"`
109+
Comment string `json:"comment"`
108110
// Metadata are typed key/value pairs extracted from the
109111
// comments.
110112
Metadata map[string]interface{} `json:"metadata"`
@@ -336,6 +338,10 @@ func (p *Parser) parseObject(pkg *packages.Package, o types.Object, v *types.Str
336338
return p.wrapErr(errors.New(obj.Name+" must be a struct"), pkg, o.Pos())
337339
}
338340
obj.TypeID = o.Pkg().Path() + "." + obj.Name
341+
342+
obj.ObjectName = types.TypeString(o.Type(), func(other *types.Package) string { return "" })
343+
obj.ExternalObjectName = types.TypeString(o.Type(), func(other *types.Package) string { return p.PackageName })
344+
339345
obj.Fields = []Field{}
340346
for i := 0; i < st.NumFields(); i++ {
341347
field, err := p.parseField(pkg, obj.Name, st.Field(i), st.Tag(i))

render/example_golang.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,27 @@ import (
99
"github.com/pacedotdev/oto/parser"
1010
)
1111

12-
func ObjectGolang(def parser.Definition, parentField *parser.Field, object *parser.Object, tabs int) template.HTML {
12+
func ObjectGolang(def parser.Definition, object *parser.Object, tabs int) template.HTML {
1313
s := &strings.Builder{}
14-
fmt.Fprintf(s, strings.Repeat("\t", tabs))
15-
if parentField != nil {
16-
fmt.Fprintf(s, "%s{\b", parentField.Type.ExternalObjectName)
17-
}
14+
fmt.Fprintf(s, "%s{", object.ExternalObjectName)
1815
for _, field := range object.Fields {
1916
fmt.Fprintf(s, "\n")
20-
fmt.Fprintf(s, strings.Repeat("\t", tabs+1))
17+
fmt.Fprint(s, strings.Repeat("\t", tabs+1))
2118
if field.Type.IsObject {
2219
// object
2320
fieldObject, err := def.Object(field.Type.ObjectName)
2421
if err != nil {
2522
return template.HTML(field.Type.ObjectName + ": " + err.Error())
2623
}
27-
fmt.Fprintf(s, "%s: %s{\n%v", field.Name, field.Type.ExternalObjectName, ObjectGolang(def, &field, fieldObject, tabs+1))
28-
fmt.Fprintf(s, "\n")
29-
fmt.Fprintf(s, strings.Repeat("\t", tabs+1))
30-
fmt.Fprintf(s, "},")
24+
fmt.Fprintf(s, "%s: %v,", field.Name, ObjectGolang(def, fieldObject, tabs+1))
3125
continue
3226
}
3327
// normal field
3428
fmt.Fprintf(s, "%s: %v,", field.Name, jsonStr(field.Metadata["example"]))
3529
}
36-
fmt.Fprintf(s, strings.Repeat("\t", tabs+1))
37-
if parentField != nil {
38-
fmt.Fprintf(s, "}")
39-
}
30+
fmt.Fprintf(s, "\n")
31+
fmt.Fprint(s, strings.Repeat("\t", tabs))
32+
fmt.Fprintf(s, "}")
4033
return template.HTML(s.String())
4134
}
4235

render/example_golang_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package render
22

33
import (
4-
"log"
54
"strings"
65
"testing"
76

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

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

27-
log.Printf("### %s ###", example)
28+
// log.Printf("### %s ###", example)
2829

2930
for _, should := range []string{
30-
"OrderField: null",
31+
"GetGreetingsRequest{",
32+
"Page: Page{",
3133
} {
3234
if !strings.Contains(string(example), should) {
3335
t.Errorf("missing: %s", should)

0 commit comments

Comments
 (0)