Skip to content

Commit

Permalink
fix: add code to handle and skip structs with "func" fields. (#616)
Browse files Browse the repository at this point in the history
Co-authored-by: Eason Lin <easonlin404@gmail.com>
  • Loading branch information
idexter and easonlin404 authored Feb 8, 2020
1 parent 86e76b7 commit f150c13
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
5 changes: 5 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,11 @@ func (parser *Parser) parseField(field *ast.Field) (*structField, error) {
}
}

// Skip func fields.
if prop.SchemaType == "func" {
return &structField{name: ""}, nil
}

structField := &structField{
name: field.Names[0].Name,
schemaType: prop.SchemaType,
Expand Down
5 changes: 5 additions & 0 deletions property.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ func getPropertyName(expr ast.Expr, parser *Parser) (propertyName, error) {
if _, ok := expr.(*ast.InterfaceType); ok { // if interface{}
return propertyName{SchemaType: "object", ArrayType: "object"}, nil
}

if _, ok := expr.(*ast.FuncType); ok { // if func()
return propertyName{SchemaType: "func", ArrayType: ""}, nil
}

return propertyName{}, errors.New("not supported" + fmt.Sprint(expr))
}

Expand Down
2 changes: 1 addition & 1 deletion schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func IsSimplePrimitiveType(typeName string) bool {
// IsPrimitiveType determine whether the type name is a primitive type
func IsPrimitiveType(typeName string) bool {
switch typeName {
case "string", "number", "integer", "boolean", "array", "object":
case "string", "number", "integer", "boolean", "array", "object", "func":
return true
default:
return false
Expand Down
1 change: 1 addition & 0 deletions testdata/simple3/web/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Pet struct {
Hidden string `json:"-"`
UUID uuid.UUID
Decimal decimal.Decimal
Function func()
}

type Tag struct {
Expand Down

0 comments on commit f150c13

Please sign in to comment.