Skip to content

Commit

Permalink
feat: add support for map and struct pointer types and selector in ar…
Browse files Browse the repository at this point in the history
…ray pointer (#244)

* Add support for map and struct pointer types

* Add test for map pointer type property

* Add support for selector expression in array pointer type

* add test for StarExpr Selector
  • Loading branch information
bojand authored and easonlin404 committed Nov 13, 2018
1 parent 8f09470 commit 56047e6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
11 changes: 11 additions & 0 deletions property.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ func getPropertyName(field *ast.Field, parser *Parser) propertyName {
if astTypeSelectorExpr, ok := ptr.X.(*ast.SelectorExpr); ok {
return parseFieldSelectorExpr(astTypeSelectorExpr, parser, newProperty)
}
// TODO support custom pointer type?
if _, ok := ptr.X.(*ast.MapType); ok { // if map
//TODO support map
return propertyName{SchemaType: "object", ArrayType: "object"}
}
if _, ok := ptr.X.(*ast.StructType); ok { // if struct
return propertyName{SchemaType: "object", ArrayType: "object"}
}
if astTypeIdent, ok := ptr.X.(*ast.Ident); ok {
name := astTypeIdent.Name
schemeType := TransToValidSchemeType(name)
Expand All @@ -106,6 +114,9 @@ func getPropertyName(field *ast.Field, parser *Parser) propertyName {
return parseFieldSelectorExpr(astTypeArrayExpr, parser, newArrayProperty)
}
if astTypeArrayExpr, ok := astTypeArray.Elt.(*ast.StarExpr); ok {
if astTypeArraySel, ok := astTypeArrayExpr.X.(*ast.SelectorExpr); ok {
return parseFieldSelectorExpr(astTypeArraySel, parser, newArrayProperty)
}
if astTypeArrayIdent, ok := astTypeArrayExpr.X.(*ast.Ident); ok {
name := TransToValidSchemeType(astTypeArrayIdent.Name)
return propertyName{SchemaType: "array", ArrayType: name}
Expand Down
56 changes: 56 additions & 0 deletions property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,33 @@ func TestGetPropertyNameStarExprIdent(t *testing.T) {
assert.Equal(t, expected, getPropertyName(input, New()))
}

func TestGetPropertyNameStarExprMap(t *testing.T) {
input := &ast.Field{
Type: &ast.StarExpr{
Star: 1026,
X: &ast.MapType{
Map: 1027,
Key: &ast.Ident{
NamePos: 1034,
Name: "string",
Obj: (*ast.Object)(nil),
},
Value: &ast.Ident{
NamePos: 1041,
Name: "string",
Obj: (*ast.Object)(nil),
},
},
},
}
expected := propertyName{
"object",
"object",
"",
}
assert.Equal(t, expected, getPropertyName(input, New()))
}

func TestGetPropertyNameArrayStarExpr(t *testing.T) {
input := &ast.Field{
Type: &ast.ArrayType{
Expand All @@ -163,6 +190,35 @@ func TestGetPropertyNameArrayStarExpr(t *testing.T) {
assert.Equal(t, expected, getPropertyName(input, New()))
}

func TestGetPropertyNameArrayStarExprSelector(t *testing.T) {
input := &ast.Field{
Type: &ast.ArrayType{
Lbrack: 1111,
Len: nil,
Elt: &ast.StarExpr{
X: &ast.SelectorExpr{
X: &ast.Ident{
NamePos: 1136,
Name: "hoge",
Obj: (*ast.Object)(nil),
},
Sel: &ast.Ident{
NamePos: 1141,
Name: "ObjectId",
Obj: (*ast.Object)(nil),
},
},
},
},
}
expected := propertyName{
"array",
"string",
"",
}
assert.Equal(t, expected, getPropertyName(input, New()))
}

func TestGetPropertyNameMap(t *testing.T) {
input := &ast.Field{
Type: &ast.MapType{
Expand Down

0 comments on commit 56047e6

Please sign in to comment.