Skip to content

Commit

Permalink
fixed where arrays of objects looked strange
Browse files Browse the repository at this point in the history
  • Loading branch information
matryer committed Jan 24, 2023
1 parent 663ae5d commit 35e084d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
6 changes: 5 additions & 1 deletion parser/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ func (d *Definition) Example(o Object) (map[string]interface{}, error) {
if err != nil {
return nil, err
}
obj[field.NameLowerCamel] = example
if field.Type.Multiple {
obj[field.NameLowerCamel] = []interface{}{example, example}
} else {
obj[field.NameLowerCamel] = example
}
continue
}
obj[field.NameLowerCamel] = field.Example
Expand Down
30 changes: 28 additions & 2 deletions parser/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ func TestObjectExample(t *testing.T) {
CleanObjectName: "obj2",
},
},
{
Name: "SomeObject",
NameLowerCamel: "someObject",
Type: FieldType{
TypeName: "obj3",
IsObject: true,
CleanObjectName: "obj3",
Multiple: false,
},
},
{
Name: "Tags",
NameLowerCamel: "tags",
Expand All @@ -57,8 +67,24 @@ func TestObjectExample(t *testing.T) {
},
},
}
obj3 := Object{
Name: "obj3",
Fields: []Field{
{
Name: "Tags",
NameLowerCamel: "tags",
Example: []interface{}{"one", "two", "three"},
Type: FieldType{
TypeName: "obj2",
CleanObjectName: "obj2",
Multiple: true,
IsObject: true,
},
},
},
}
def := &Definition{
Objects: []Object{obj1, obj2},
Objects: []Object{obj1, obj2, obj3},
}
example, err := def.Example(obj1)
is.NoErr(err)
Expand All @@ -82,7 +108,7 @@ func TestObjectExample(t *testing.T) {
exampleJSON, err := def.Example(obj1)
is.NoErr(err)

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

Expand Down

0 comments on commit 35e084d

Please sign in to comment.