From a8ef569b67b5614c07f27fcb5a5757b9a948f979 Mon Sep 17 00:00:00 2001 From: Bogdan U Date: Fri, 24 Dec 2021 00:13:35 +0200 Subject: [PATCH] chore: field tags constants (#1088) --- field_parser.go | 28 ++++++++++++++-------------- operation.go | 6 ++++++ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/field_parser.go b/field_parser.go index b688d7639..fba3d78f4 100644 --- a/field_parser.go +++ b/field_parser.go @@ -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) { @@ -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") { @@ -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, ",") { @@ -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 { @@ -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 { @@ -262,7 +262,7 @@ 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 } @@ -270,7 +270,7 @@ func (ps *tagBaseFieldParser) ComplementSchema(schema *spec.Schema) error { structField.maximum = maximum } - minimum, err := getFloatTag(ps.tag, "minimum") + minimum, err := getFloatTag(ps.tag, minimumTag) if err != nil { return err } @@ -278,7 +278,7 @@ func (ps *tagBaseFieldParser) ComplementSchema(schema *spec.Schema) error { structField.minimum = minimum } - multipleOf, err := getFloatTag(ps.tag, "multipleOf") + multipleOf, err := getFloatTag(ps.tag, multipleOfTag) if err != nil { return err } @@ -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" { @@ -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" { diff --git a/operation.go b/operation.go index 454ec6431..54c19ba01 100644 --- a/operation.go +++ b/operation.go @@ -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" )