Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(codegen): Removed deprecated code and improved speed #2899

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading