Skip to content

Commit

Permalink
feat: add tag 'swaggerignore' to exclude a field from struct (#635)
Browse files Browse the repository at this point in the history
* add tag 'swaggerignore' to exclude a field from struct

* update
  • Loading branch information
sdghchj authored Mar 14, 2020
1 parent 7290e9b commit 4676cb1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ Swag converts Go annotations to Swagger Documentation 2.0. We've created a varie
- [Example value of struct](#example-value-of-struct)
- [Description of struct](#description-of-struct)
- [Use swaggertype tag to supported custom type](#use-swaggertype-tag-to-supported-custom-type)
- [Use swaggerignore tag to exclude a field](#use-swaggerignore-tag-to-exclude-a-field)
- [Add extension info to struct field](#add-extension-info-to-struct-field)
- [Rename model to display](#rename-model-to-display)
- [How to using security annotations](#how-to-using-security-annotations)
- [About the Project](#about-the-project)

Expand Down Expand Up @@ -596,6 +598,17 @@ generated swagger doc as follows:

```


### Use swaggerignore tag to exclude a field

```go
type Account struct {
ID string `json:"id"`
Name string `json:"name"`
Ignored int `swaggerignore:"true"`
}
```

### Add extension info to struct field

```go
Expand Down
7 changes: 7 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,12 @@ func (parser *Parser) parseField(pkgName string, field *ast.Field) (*structField
}
// `json:"tag"` -> json:"tag"
structTag := reflect.StructTag(strings.Replace(field.Tag.Value, "`", "", -1))

if ignoreTag := structTag.Get("swaggerignore"); ignoreTag == "true" {
structField.name = ""
return structField, nil
}

jsonTag := structTag.Get("json")
// json:"tag,hoge"
if strings.Contains(jsonTag, ",") {
Expand All @@ -1184,6 +1190,7 @@ func (parser *Parser) parseField(pkgName string, field *ast.Field) (*structField
}
if jsonTag == "-" {
structField.name = ""
return structField, nil
} else if jsonTag != "" {
structField.name = jsonTag
}
Expand Down

0 comments on commit 4676cb1

Please sign in to comment.