Skip to content

Commit

Permalink
feat: ignore uri tag
Browse files Browse the repository at this point in the history
  • Loading branch information
weilence committed Dec 14, 2022
1 parent dfce6c8 commit 45cb88a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
22 changes: 20 additions & 2 deletions field_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
optionalLabel = "optional"
swaggerTypeTag = "swaggertype"
swaggerIgnoreTag = "swaggerignore"
ignoreTags = "uri"
)

type tagBaseFieldParser struct {
Expand Down Expand Up @@ -51,8 +52,7 @@ func (ps *tagBaseFieldParser) ShouldSkip() bool {
return false
}

ignoreTag := ps.tag.Get(swaggerIgnoreTag)
if strings.EqualFold(ignoreTag, "true") {
if isIgnore(ps.tag) {
return true
}

Expand All @@ -65,6 +65,24 @@ func (ps *tagBaseFieldParser) ShouldSkip() bool {
return false
}

var ignoreTagArray = strings.Split(ignoreTags, ",")

func isIgnore(tag reflect.StructTag) bool {
ignoreTag, ok := tag.Lookup(swaggerIgnoreTag)
if ok && strings.EqualFold(ignoreTag, "true") {
return true
}

for _, v := range ignoreTagArray {
_, ok = tag.Lookup(v)
if ok {
return ok
}
}

return false
}

func (ps *tagBaseFieldParser) FieldName() (string, error) {
var name string
if ps.field.Tag != nil {
Expand Down
4 changes: 2 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1276,8 +1276,8 @@ func (parser *Parser) parseStruct(file *ast.File, fields *ast.FieldList) (*spec.

func (parser *Parser) parseStructField(file *ast.File, field *ast.Field) (map[string]spec.Schema, []string, error) {
if field.Tag != nil {
skip, ok := reflect.StructTag(strings.ReplaceAll(field.Tag.Value, "`", "")).Lookup("swaggerignore")
if ok && strings.EqualFold(skip, "true") {
tag := reflect.StructTag(strings.ReplaceAll(field.Tag.Value, "`", ""))
if isIgnore(tag) {
return nil, nil, nil
}
}
Expand Down

0 comments on commit 45cb88a

Please sign in to comment.