Skip to content

Commit

Permalink
Implement getting name override from comment for enum ordered consts.
Browse files Browse the repository at this point in the history
  • Loading branch information
drewsilcock committed Mar 28, 2024
1 parent 91624ad commit 3ccadea
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 25 deletions.
10 changes: 10 additions & 0 deletions const.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ type ConstVariable struct {
Pkg *PackageDefinitions
}

func (cv *ConstVariable) VariableName() string {
if ignoreNameOverride(cv.Name.Name) {
return cv.Name.Name[1:]
} else if overriddenName := nameOverride(cv.Comment); overriddenName != "" {
return overriddenName
}

return cv.Name.Name
}

var escapedChars = map[uint8]uint8{
'n': '\n',
'r': '\r',
Expand Down
14 changes: 4 additions & 10 deletions packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,21 +362,15 @@ func (pkgDefs *PackagesDefinitions) collectConstEnums(parsedSchemas map[*TypeSpe
typeDef.Enums = make([]EnumValue, 0)
}

name := constVar.Name.Name
name := constVar.VariableName()
if _, ok = constVar.Value.(ast.Expr); ok {
continue
}

enumValue := EnumValue{
key: name,
Value: constVar.Value,
}
if constVar.Comment != nil && len(constVar.Comment.List) > 0 {
enumValue.Comment = constVar.Comment.List[0].Text
enumValue.Comment = strings.TrimPrefix(enumValue.Comment, "//")
enumValue.Comment = strings.TrimPrefix(enumValue.Comment, "/*")
enumValue.Comment = strings.TrimSuffix(enumValue.Comment, "*/")
enumValue.Comment = strings.TrimSpace(enumValue.Comment)
key: name,
Value: constVar.Value,
Comment: commentWithoutNameOverride(constVar.Comment),
}
typeDef.Enums = append(typeDef.Enums, enumValue)
}
Expand Down
41 changes: 41 additions & 0 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"errors"
"fmt"
"github.com/go-openapi/spec"
"go/ast"
"regexp"
"strings"
)

const (
Expand Down Expand Up @@ -134,6 +137,44 @@ func ignoreNameOverride(name string) bool {
return len(name) != 0 && name[0] == IgnoreNameOverridePrefix
}

var overrideNameRegex = regexp.MustCompile(`(?i)^@name\s+(\S+)`)

func nameOverride(commentGroup *ast.CommentGroup) string {
if commentGroup == nil {
return ""
}

// get alias from comment '// @name '
for _, comment := range commentGroup.List {
trimmedComment := strings.TrimSpace(strings.TrimLeft(comment.Text, "/"))
texts := overrideNameRegex.FindStringSubmatch(trimmedComment)
if len(texts) > 1 {
return texts[1]
}
}

return ""
}

func commentWithoutNameOverride(commentGroup *ast.CommentGroup) string {
if commentGroup == nil {
return ""
}

commentBuilder := strings.Builder{}
for _, comment := range commentGroup.List {
commentText := comment.Text
commentText = strings.TrimPrefix(commentText, "//")
commentText = strings.TrimPrefix(commentText, "/*")
commentText = strings.TrimSuffix(commentText, "*/")
commentText = strings.TrimSpace(commentText)
commentText = overrideNameRegex.ReplaceAllString(commentText, "")
commentText = strings.TrimSpace(commentText)
commentBuilder.WriteString(commentText)
}
return commentBuilder.String()
}

// IsComplexSchema whether a schema is complex and should be a ref schema
func IsComplexSchema(schema *spec.Schema) bool {
// a enum type should be complex
Expand Down
17 changes: 2 additions & 15 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package swag
import (
"go/ast"
"go/token"
"regexp"
"strings"

"github.com/go-openapi/spec"
Expand Down Expand Up @@ -46,20 +45,8 @@ func (t *TypeSpecDef) Name() string {
func (t *TypeSpecDef) TypeName() string {
if ignoreNameOverride(t.TypeSpec.Name.Name) {
return t.TypeSpec.Name.Name[1:]
} else if t.TypeSpec.Comment != nil {
// get alias from comment '// @name '
const regexCaseInsensitive = "(?i)"
reTypeName, err := regexp.Compile(regexCaseInsensitive + `^@name\s+(\S+)`)
if err != nil {
panic(err)
}
for _, comment := range t.TypeSpec.Comment.List {
trimmedComment := strings.TrimSpace(strings.TrimLeft(comment.Text, "/"))
texts := reTypeName.FindStringSubmatch(trimmedComment)
if len(texts) > 1 {
return texts[1]
}
}
} else if overriddenName := nameOverride(t.TypeSpec.Comment); overriddenName != "" {
return overriddenName
}

var names []string
Expand Down

0 comments on commit 3ccadea

Please sign in to comment.