Skip to content

Commit

Permalink
feat: ignore uri tag with gin
Browse files Browse the repository at this point in the history
  • Loading branch information
weilence committed Sep 3, 2022
1 parent e7ccdf4 commit ba6cb48
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
18 changes: 16 additions & 2 deletions field_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
optionalLabel = "optional"
swaggerTypeTag = "swaggertype"
swaggerIgnoreTag = "swaggerignore"
uriTag = "uri"
)

type tagBaseFieldParser struct {
Expand Down Expand Up @@ -52,8 +53,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 @@ -66,6 +66,20 @@ func (ps *tagBaseFieldParser) ShouldSkip() bool {
return false
}

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

_, ok = tag.Lookup(uriTag)
if ok {
return true
}

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 @@ -1230,8 +1230,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.Names == nil {
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 ba6cb48

Please sign in to comment.