From e7ab51f655c7a04aae1d5b2817420889c4ce2573 Mon Sep 17 00:00:00 2001 From: Mat Ryer Date: Fri, 24 Feb 2023 00:42:43 +0000 Subject: [PATCH] added go object experiment and ExternalObjectName field --- main.go | 3 +++ parser/parser.go | 10 +++++++--- render/example_golang.go | 25 ++++++++++++++++--------- render/example_golang_test.go | 4 ++-- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index df43438..67b080c 100644 --- a/main.go +++ b/main.go @@ -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 diff --git a/parser/parser.go b/parser/parser.go index 03dcef3..be81187 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -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"` @@ -163,6 +164,8 @@ type Parser struct { ExcludeInterfaces []string + PackageName string + patterns []string def Definition @@ -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, "*") diff --git a/render/example_golang.go b/render/example_golang.go index a63210f..d77b131 100644 --- a/render/example_golang.go +++ b/render/example_golang.go @@ -3,15 +3,19 @@ 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)) @@ -19,20 +23,23 @@ func ObjectGolang(def parser.Definition, object parser.Object, tabs int) string // 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 { diff --git a/render/example_golang_test.go b/render/example_golang_test.go index 663dd49..21bdd89 100644 --- a/render/example_golang_test.go +++ b/render/example_golang_test.go @@ -19,7 +19,7 @@ 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 @@ -27,7 +27,7 @@ func TestExmapleGolang(t *testing.T) { "// Package services contains services.", "package services", } { - if !strings.Contains(example, should) { + if !strings.Contains(string(example), should) { t.Errorf("missing: %s", should) is.Fail() }