Skip to content

Commit

Permalink
Just optimize code about parsing extensions (#1399)
Browse files Browse the repository at this point in the history
* just optimize code
  • Loading branch information
sdghchj committed Nov 26, 2022
1 parent ba5df82 commit a10fb9a
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions parser.go
Expand Up @@ -454,16 +454,11 @@ func parseGeneralAPIInfo(parser *Parser, comments []string) error {
value = fields[1]
}

multilineBlock := false
if previousAttribute == attribute {
multilineBlock = true
}

switch attr := strings.ToLower(attribute); attr {
case versionAttr, titleAttr, tosAttr, licNameAttr, licURLAttr, conNameAttr, conURLAttr, conEmailAttr:
setSwaggerInfo(parser.swagger, attr, value)
case descriptionAttr:
if multilineBlock {
if previousAttribute == attribute {
parser.swagger.Info.Description += "\n" + value

continue
Expand Down Expand Up @@ -543,14 +538,14 @@ func parseGeneralAPIInfo(parser *Parser, comments []string) error {
case "@query.collection.format":
parser.collectionFormatInQuery = value
default:
prefixExtension := "@x-"
// Prefix extension + 1 char + 1 space + 1 char
if len(attribute) > 5 && attribute[:len(prefixExtension)] == prefixExtension {
if strings.HasPrefix(attribute, "@x-") {
extensionName := attribute[1:]

extExistsInSecurityDef := false
// for each security definition
for _, v := range parser.swagger.SecurityDefinitions {
// check if extension exists
_, extExistsInSecurityDef = v.VendorExtensible.Extensions.GetString(attribute[1:])
_, extExistsInSecurityDef = v.VendorExtensible.Extensions.GetString(extensionName)
// if it exists in at least one, then we stop iterating
if extExistsInSecurityDef {
break
Expand All @@ -562,16 +557,12 @@ func parseGeneralAPIInfo(parser *Parser, comments []string) error {
break
}

var valueJSON interface{}

split := strings.SplitAfter(commentLine, attribute+" ")
if len(split) < 2 {
if len(value) == 0 {
return fmt.Errorf("annotation %s need a value", attribute)
}

extensionName := "x-" + strings.SplitAfter(attribute, prefixExtension)[1]

err := json.Unmarshal([]byte(split[1]), &valueJSON)
var valueJSON interface{}
err := json.Unmarshal([]byte(value), &valueJSON)
if err != nil {
return fmt.Errorf("annotation %s need a valid json value", attribute)
}
Expand Down

0 comments on commit a10fb9a

Please sign in to comment.