Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle and skip structs with "func" fields. #616

Merged
merged 3 commits into from
Feb 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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