Skip to content

Commit

Permalink
chore: field tags constants (#1088)
Browse files Browse the repository at this point in the history
  • Loading branch information
ubogdan committed Dec 23, 2021
1 parent 557e1c6 commit a8ef569
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
28 changes: 14 additions & 14 deletions field_parser.go
Expand Up @@ -164,8 +164,8 @@ func (ps *tagBaseFieldParser) ComplementSchema(schema *spec.Schema) error {

structField := &structField{
schemaType: types[0],
formatType: ps.tag.Get("format"),
readOnly: ps.tag.Get("readonly") == "true",
formatType: ps.tag.Get(formatTag),
readOnly: ps.tag.Get(readOnlyTag) == "true",
}

if len(types) > 1 && (types[0] == ARRAY || types[0] == OBJECT) {
Expand All @@ -179,10 +179,10 @@ func (ps *tagBaseFieldParser) ComplementSchema(schema *spec.Schema) error {
structField.desc = strings.TrimSpace(ps.field.Comment.Text())
}

jsonTag := ps.tag.Get("json")
jsonTag := ps.tag.Get(jsonTag)
// json:"name,string" or json:",string"

exampleTag := ps.tag.Get("example")
exampleTag := ps.tag.Get(exampleTag)
if exampleTag != "" {
structField.exampleValue = exampleTag
if !strings.Contains(jsonTag, ",string") {
Expand All @@ -194,17 +194,17 @@ func (ps *tagBaseFieldParser) ComplementSchema(schema *spec.Schema) error {
}
}

bindingTag := ps.tag.Get("binding")
bindingTag := ps.tag.Get(bindingTag)
if bindingTag != "" {
ps.parseValidTags(bindingTag, structField)
}

validateTag := ps.tag.Get("validate")
validateTag := ps.tag.Get(validateTag)
if validateTag != "" {
ps.parseValidTags(validateTag, structField)
}

extensionsTag := ps.tag.Get("extensions")
extensionsTag := ps.tag.Get(extensionsTag)
if extensionsTag != "" {
structField.extensions = map[string]interface{}{}
for _, val := range strings.Split(extensionsTag, ",") {
Expand All @@ -221,7 +221,7 @@ func (ps *tagBaseFieldParser) ComplementSchema(schema *spec.Schema) error {
}
}

enumsTag := ps.tag.Get("enums")
enumsTag := ps.tag.Get(enumsTag)
if enumsTag != "" {
enumType := structField.schemaType
if structField.schemaType == ARRAY {
Expand Down Expand Up @@ -252,7 +252,7 @@ func (ps *tagBaseFieldParser) ComplementSchema(schema *spec.Schema) error {
}
structField.extensions["x-enum-varnames"] = structField.enumVarNames
}
defaultTag := ps.tag.Get("default")
defaultTag := ps.tag.Get(defaultTag)
if defaultTag != "" {
value, err := defineType(structField.schemaType, defaultTag)
if err != nil {
Expand All @@ -262,23 +262,23 @@ func (ps *tagBaseFieldParser) ComplementSchema(schema *spec.Schema) error {
}

if IsNumericType(structField.schemaType) || IsNumericType(structField.arrayType) {
maximum, err := getFloatTag(ps.tag, "maximum")
maximum, err := getFloatTag(ps.tag, maximumTag)
if err != nil {
return err
}
if maximum != nil {
structField.maximum = maximum
}

minimum, err := getFloatTag(ps.tag, "minimum")
minimum, err := getFloatTag(ps.tag, minimumTag)
if err != nil {
return err
}
if minimum != nil {
structField.minimum = minimum
}

multipleOf, err := getFloatTag(ps.tag, "multipleOf")
multipleOf, err := getFloatTag(ps.tag, multipleOfTag)
if err != nil {
return err
}
Expand Down Expand Up @@ -396,7 +396,7 @@ func (ps *tagBaseFieldParser) IsRequired() (bool, error) {
return false, nil
}

bindingTag := ps.tag.Get("binding")
bindingTag := ps.tag.Get(bindingTag)
if bindingTag != "" {
for _, val := range strings.Split(bindingTag, ",") {
if val == "required" {
Expand All @@ -405,7 +405,7 @@ func (ps *tagBaseFieldParser) IsRequired() (bool, error) {
}
}

validateTag := ps.tag.Get("validate")
validateTag := ps.tag.Get(validateTag)
if validateTag != "" {
for _, val := range strings.Split(validateTag, ",") {
if val == "required" {
Expand Down
6 changes: 6 additions & 0 deletions operation.go
Expand Up @@ -355,13 +355,19 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
}

const (
jsonTag = "json"
bindingTag = "binding"
defaultTag = "default"
enumsTag = "enums"
exampleTag = "example"
formatTag = "format"
validateTag = "validate"
minimumTag = "minimum"
maximumTag = "maximum"
minLengthTag = "minlength"
maxLengthTag = "maxlength"
multipleOfTag = "multipleOf"
readOnlyTag = "readonly"
extensionsTag = "extensions"
collectionFormatTag = "collectionFormat"
)
Expand Down

0 comments on commit a8ef569

Please sign in to comment.