Skip to content

Commit

Permalink
fixed IsObject for pointers to objects
Browse files Browse the repository at this point in the history
matryer committed Aug 12, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 5fe9676 commit 6e3b5c2
Showing 4 changed files with 42 additions and 26 deletions.
44 changes: 23 additions & 21 deletions parser/example_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package parser

import (
"encoding/json"
"fmt"
"testing"

"github.com/matryer/is"
@@ -15,19 +13,23 @@ func TestObjectExample(t *testing.T) {
Name: "obj1",
Fields: []Field{
{
Name: "Name",
Example: "Mat",
Name: "Name",
NameLowerCamel: "name",
Example: "Mat",
},
{
Name: "Project",
Example: "Respond",
Name: "Project",
NameLowerCamel: "project",
Example: "Respond",
},
{
Name: "SinceYear",
Example: 2021,
Name: "SinceYear",
NameLowerCamel: "sinceYear",
Example: 2021,
},
{
Name: "Favourites",
Name: "Favourites",
NameLowerCamel: "favourites",
Type: FieldType{
TypeName: "obj2",
IsObject: true,
@@ -39,9 +41,9 @@ func TestObjectExample(t *testing.T) {
Name: "obj2",
Fields: []Field{
{
Type: FieldType{TypeName: "string", Multiple: true},
Name: "Languages",
Example: "Go",
Type: FieldType{TypeName: "string", Multiple: true},
NameLowerCamel: "languages",
Example: "Go",
},
},
}
@@ -52,17 +54,17 @@ func TestObjectExample(t *testing.T) {
is.NoErr(err)
is.True(example != nil)

b, err := json.MarshalIndent(example, "", "\t")
is.NoErr(err)
fmt.Println(string(b))
// b, err := json.MarshalIndent(example, "", "\t")
// is.NoErr(err)
// fmt.Println(string(b))

is.Equal(example["Name"], "Mat")
is.Equal(example["Project"], "Respond")
is.Equal(example["SinceYear"], 2021)
is.True(example["Favourites"] != nil)
favourites, ok := example["Favourites"].(map[string]interface{})
is.Equal(example["name"], "Mat")
is.Equal(example["project"], "Respond")
is.Equal(example["sinceYear"], 2021)
is.True(example["favourites"] != nil)
favourites, ok := example["favourites"].(map[string]interface{})
is.True(ok) // Favourites map[string]interface{}
languages, ok := favourites["Languages"].([]interface{})
languages, ok := favourites["languages"].([]interface{})
is.True(ok) // Languages []interface{}
is.Equal(len(languages), 3)

14 changes: 11 additions & 3 deletions parser/parser.go
Original file line number Diff line number Diff line change
@@ -404,11 +404,19 @@ func (p *Parser) parseFieldType(pkg *packages.Package, obj types.Object) (FieldT
}
return "" // no package prefix
}

typ := obj.Type()
if slice, ok := obj.Type().(*types.Slice); ok {
typ = slice.Elem()
ftype.Multiple = true
}
isPointer := true
originalTyp := typ
pointerType, isPointer := typ.(*types.Pointer)
if isPointer {
typ = pointerType.Elem()
isPointer = true
}
if named, ok := typ.(*types.Named); ok {
if structure, ok := named.Underlying().(*types.Struct); ok {
if err := p.parseObject(pkg, named.Obj(), structure); err != nil {
@@ -422,16 +430,16 @@ func (p *Parser) parseFieldType(pkg *packages.Package, obj types.Object) (FieldT
case *types.Struct:
return ftype, p.wrapErr(errors.New("nested structs not supported (create another type instead)"), pkg, obj.Pos())
}
ftype.TypeName = types.TypeString(typ, resolver)
ftype.ObjectName = types.TypeString(typ, func(other *types.Package) string { return "" })
ftype.TypeName = types.TypeString(originalTyp, resolver)
ftype.ObjectName = types.TypeString(originalTyp, func(other *types.Package) string { return "" })
ftype.ObjectNameLowerCamel = camelizeDown(ftype.ObjectName)
ftype.TypeID = pkgPath + "." + ftype.ObjectName
typeWithoutPointer := strings.TrimPrefix(ftype.TypeName, "*")
ftype.JSType = typeWithoutPointer
ftype.SwiftType = typeWithoutPointer
if ftype.IsObject {
ftype.JSType = "object"
ftype.SwiftType = "Any"
//ftype.SwiftType = "Any"
} else {
switch typeWithoutPointer {
case "interface{}":
8 changes: 7 additions & 1 deletion parser/parser_test.go
Original file line number Diff line number Diff line change
@@ -45,8 +45,14 @@ You will love it.`)
is.Equal(def.Services[0].Methods[1].InputObject.Package, "")
is.Equal(def.Services[0].Methods[1].OutputObject.TypeName, "GreetResponse")
is.Equal(def.Services[0].Methods[1].OutputObject.Multiple, false)
is.Equal(def.Services[0].Methods[1].OutputObject.IsObject, true)
is.Equal(def.Services[0].Methods[1].OutputObject.Package, "")

greetResponse, err := def.Object(def.Services[0].Methods[1].OutputObject.TypeName)
is.NoErr(err)
is.Equal(greetResponse.Fields[0].Name, "Greeting")
is.Equal(greetResponse.Fields[0].Type.IsObject, true)

formatCommentText := func(s string) string {
var buf bytes.Buffer
doc.ToText(&buf, s, "// ", "", 80)
@@ -148,7 +154,7 @@ You will love it.`)
is.Equal(welcomeInputObject.Fields[2].Type.SwiftType, "Double")

is.Equal(welcomeInputObject.Fields[3].Type.TypeName, "*CustomerDetails")
is.Equal(welcomeInputObject.Fields[3].Type.JSType, "CustomerDetails")
is.Equal(welcomeInputObject.Fields[3].Type.JSType, "object")
is.Equal(welcomeInputObject.Fields[3].Example, nil)
is.Equal(welcomeInputObject.Fields[3].Type.SwiftType, "CustomerDetails")

2 changes: 1 addition & 1 deletion parser/testdata/services/pleasantries/greeter.go
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ type GreetRequest struct {
// person's greeting.
type GreetResponse struct {
// Greeting is the greeted person's Greeting.
Greeting Greeting
Greeting *Greeting
}

// GetGreetingsRequest is the request object for GreeterService.GetGreetings.

0 comments on commit 6e3b5c2

Please sign in to comment.