Skip to content

Commit

Permalink
refactor(codegen): Removed deprecated code and improved speed (#2899)
Browse files Browse the repository at this point in the history
  • Loading branch information
quenbyako authored Oct 23, 2023
1 parent 4be55bd commit dece28f
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions internal/codegen/golang/enum.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package golang

import (
"regexp"
"strings"
"unicode"
)

var IdentPattern = regexp.MustCompile("[^a-zA-Z0-9_]+")

type Constant struct {
Name string
Type string
Expand All @@ -29,21 +27,38 @@ func (e Enum) ValidTag() string {
return TagsToString(e.ValidTags)
}

func enumReplacer(r rune) rune {
if strings.ContainsRune("-/:_", r) {
return '_'
} else if (r >= 'a' && r <= 'z') ||
(r >= 'A' && r <= 'Z') ||
(r >= '0' && r <= '9') {
return r
} else {
return -1
}
}

// EnumReplace removes all non ident symbols (all but letters, numbers and
// underscore) and returns valid ident name for provided name.
func EnumReplace(value string) string {
id := strings.Replace(value, "-", "_", -1)
id = strings.Replace(id, ":", "_", -1)
id = strings.Replace(id, "/", "_", -1)
return IdentPattern.ReplaceAllString(id, "")
return strings.Map(enumReplacer, value)
}

// EnumValueName removes all non ident symbols (all but letters, numbers and
// underscore) and converts snake case ident to camel case.
func EnumValueName(value string) string {
name := ""
id := strings.Replace(value, "-", "_", -1)
id = strings.Replace(id, ":", "_", -1)
id = strings.Replace(id, "/", "_", -1)
id = IdentPattern.ReplaceAllString(id, "")
for _, part := range strings.Split(id, "_") {
name += strings.Title(part)
parts := strings.Split(EnumReplace(value), "_")
for i, part := range parts {
parts[i] = titleFirst(part)
}
return name

return strings.Join(parts, "")
}

func titleFirst(s string) string {
r := []rune(s)
r[0] = unicode.ToUpper(r[0])

return string(r)
}

0 comments on commit dece28f

Please sign in to comment.