Skip to content

Commit

Permalink
better example handling (may require an update to example comments)
Browse files Browse the repository at this point in the history
  • Loading branch information
matryer committed Jan 24, 2023
1 parent 9382886 commit 663ae5d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
8 changes: 0 additions & 8 deletions parser/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,9 @@ func (d *Definition) Example(o Object) (map[string]interface{}, error) {
return nil, err
}
obj[field.NameLowerCamel] = example
if field.Type.Multiple {
// turn it into an array
obj[field.NameLowerCamel] = []interface{}{obj[field.NameLowerCamel]}
}
continue
}
obj[field.NameLowerCamel] = field.Example
if field.Type.Multiple {
// turn it into an array
obj[field.NameLowerCamel] = []interface{}{obj[field.NameLowerCamel], obj[field.NameLowerCamel], obj[field.NameLowerCamel]}
}
}
return obj, nil
}
Expand Down
23 changes: 20 additions & 3 deletions parser/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ func TestObjectExample(t *testing.T) {
CleanObjectName: "obj2",
},
},
{
Name: "Tags",
NameLowerCamel: "tags",
Type: FieldType{
Multiple: true,
TypeName: "string",
},
Example: []interface{}{"security", "customer-affected", "review-needed"},
},
},
}
obj2 := Object{
Expand All @@ -44,7 +53,7 @@ func TestObjectExample(t *testing.T) {
{
Type: FieldType{TypeName: "string", Multiple: true, CleanObjectName: "string"},
NameLowerCamel: "languages",
Example: "Go",
Example: []interface{}{"Go"},
},
},
}
Expand All @@ -55,6 +64,11 @@ func TestObjectExample(t *testing.T) {
is.NoErr(err)
is.True(example != nil)

// check it out:
// b, err := json.MarshalIndent(example, "", " ")
// is.NoErr(err)
// fmt.Println(string(b))

is.Equal(example["name"], "Mat")
is.Equal(example["project"], "Respond")
is.Equal(example["sinceYear"], 2021)
Expand All @@ -63,10 +77,13 @@ func TestObjectExample(t *testing.T) {
is.True(ok) // Favourites map[string]interface{}
languages, ok := favourites["languages"].([]interface{})
is.True(ok) // Languages []interface{}
is.Equal(len(languages), 3)
is.Equal(len(languages), 1)

exampleJSON, err := def.Example(obj1)
is.NoErr(err)
is.Equal(len(exampleJSON), 4)

is.Equal(len(exampleJSON), 5)
is.Equal(len(exampleJSON["tags"].([]interface{})), 3)
is.Equal(exampleJSON["tags"].([]interface{})[0], "security")

}
10 changes: 9 additions & 1 deletion parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ You will love it.`)
is.Equal(greetResponse.Fields[0].Name, "Greeting")
is.Equal(greetResponse.Fields[0].Type.IsObject, true)

greetRequest, err := def.Object(def.Services[0].Methods[1].InputObject.TypeName)
is.NoErr(err)
is.Equal(len(greetRequest.Fields), 1)
is.Equal(greetRequest.Fields[0].Name, "Names") // the "Names" field
is.Equal(greetRequest.Fields[0].Type.IsObject, false) // IsObject
is.Equal(greetRequest.Fields[0].Type.Multiple, true) // Multiple
is.Equal(greetRequest.Fields[0].Type.TypeName, "string") // TypeName

formatCommentText := func(s string) string {
var buf bytes.Buffer
doc.ToText(&buf, s, "// ", "", 80)
Expand All @@ -77,7 +85,7 @@ You will love it.`)
is.Equal(greetInputObject.Fields[0].Type.ObjectName, "Page")
is.Equal(greetInputObject.Fields[0].Type.ObjectNameLowerCamel, "page")
is.Equal(greetInputObject.Fields[0].Type.JSType, "object")
is.Equal(greetInputObject.Fields[0].Type.TSType, "services.Page")
is.Equal(greetInputObject.Fields[0].Type.TSType, "Page")
is.Equal(greetInputObject.Fields[0].Type.TypeID, "github.com/pacedotdev/oto/testdata/services.Page")
is.Equal(greetInputObject.Fields[0].Type.IsObject, true)
is.Equal(greetInputObject.Fields[0].Type.Multiple, false)
Expand Down

0 comments on commit 663ae5d

Please sign in to comment.