Skip to content

Commit

Permalink
added go object experiment and ExternalObjectName field
Browse files Browse the repository at this point in the history
  • Loading branch information
matryer committed Feb 24, 2023
1 parent 81fd203 commit e7ab51f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ flags:`)
if p.Verbose {
fmt.Println("oto - github.com/pacedotdev/oto", Version)
}
if *pkg != "" {
p.PackageName = *pkg
}
def, err := p.Parse()
if err != nil {
return err
Expand Down
10 changes: 7 additions & 3 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ type FieldTag struct {
// FieldType holds information about the type of data that this
// Field stores.
type FieldType struct {
TypeID string `json:"typeID"`
TypeName string `json:"typeName"`
ObjectName string `json:"objectName"`
TypeID string `json:"typeID"`
TypeName string `json:"typeName"`
ObjectName string `json:"objectName"`
ExternalObjectName string `json:"externalObjectName"`
// CleanObjectName is the ObjectName with * removed
// for pointer types.
CleanObjectName string `json:"cleanObjectName"`
Expand All @@ -163,6 +164,8 @@ type Parser struct {

ExcludeInterfaces []string

PackageName string

patterns []string
def Definition

Expand Down Expand Up @@ -441,6 +444,7 @@ func (p *Parser) parseFieldType(pkg *packages.Package, obj types.Object) (FieldT
}
ftype.TypeName = types.TypeString(originalTyp, resolver)
ftype.ObjectName = types.TypeString(originalTyp, func(other *types.Package) string { return "" })
ftype.ExternalObjectName = types.TypeString(originalTyp, func(other *types.Package) string { return p.PackageName })
ftype.ObjectNameLowerCamel = camelizeDown(ftype.ObjectName)
ftype.TypeID = pkgPath + "." + ftype.ObjectName
ftype.CleanObjectName = strings.TrimPrefix(ftype.ObjectName, "*")
Expand Down
25 changes: 16 additions & 9 deletions render/example_golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,43 @@ package render
import (
"encoding/json"
"fmt"
"html/template"
"log"
"strings"

"github.com/pacedotdev/oto/parser"
)

func ObjectGolang(def parser.Definition, object parser.Object, tabs int) string {
func ObjectGolang(def parser.Definition, parentField *parser.Field, 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)
}
for _, field := range object.Fields {
fmt.Fprintf(s, "\n")
fmt.Fprintf(s, strings.Repeat("\t", tabs+1))
if field.Type.IsObject {
// object
fieldObject, err := def.Object(field.Type.ObjectName)
if err != nil {
return field.Type.ObjectName + ": " + err.Error()
return template.HTML(field.Type.ObjectName + ": " + err.Error())
}
fmt.Fprintf(s, `%s: %s{
%v
}`, field.Name, field.Type.TypeName, ObjectGolang(def, *fieldObject, tabs+2))
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, "},")
continue
}
// normal field
log.Printf("%+v", field)
fmt.Fprintf(s, "%s: %v", field.Name, jsonStr(field.Metadata["example"]))
fmt.Fprintf(s, "%s: %v,", field.Name, jsonStr(field.Metadata["example"]))
}
fmt.Fprintf(s, "\n")
fmt.Fprintf(s, strings.Repeat("\t", tabs))
return s.String()
fmt.Fprintf(s, strings.Repeat("\t", tabs+1))
if parentField != nil {
fmt.Fprintf(s, "}")
}
return template.HTML(s.String())
}

func jsonStr(v interface{}) string {
Expand Down
4 changes: 2 additions & 2 deletions render/example_golang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ 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, *inputObject, 0)
example := ObjectGolang(def, nil, inputObject, 0)
err = os.WriteFile("./delete-me-example.go.notgo", []byte(example), 0666)
is.NoErr(err) // write file

for _, should := range []string{
"// Package services contains services.",
"package services",
} {
if !strings.Contains(example, should) {
if !strings.Contains(string(example), should) {
t.Errorf("missing: %s", should)
is.Fail()
}
Expand Down

0 comments on commit e7ab51f

Please sign in to comment.