Skip to content

Commit

Permalink
cleaning up a little
Browse files Browse the repository at this point in the history
  • Loading branch information
cneill committed Aug 18, 2023
1 parent 6370ef6 commit c8fc696
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import (
"strings"
)

// GetNormalizedName takes in a field name or file name and returns a "normalized" (CamelCase) string suitable for use as a Go
// GetGoName takes in a field name or file name and returns a "normalized" (CamelCase) string suitable for use as a Go
// variable name.
func GetGoName(input string) string {
var cleaned strings.Builder

// remove garbage characters, replace separators with ' '
for _, r := range input {
if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') {
if isAlphaNum(r) {
cleaned.WriteRune(r)
} else if r == '_' || r == '.' || r == '-' || r == ' ' {
} else if isSeparator(r) {
cleaned.WriteRune(' ')
}
}
Expand All @@ -32,12 +32,21 @@ func GetGoName(input string) string {
temp = append(temp, word)
}

// TODO: replace this with golang.org/x/text/cases.Title()
result := strings.Title(strings.Join(temp, " "))
result = strings.ReplaceAll(result, " ", "")

return result
}

func isAlphaNum(r rune) bool {
return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9')
}

func isSeparator(r rune) bool {
return r == '_' || r == '.' || r == '-' || r == ' '
}

func anySliceToJSONStructs(input []any) (JSONStructs, error) {
result := JSONStructs{}

Expand Down

0 comments on commit c8fc696

Please sign in to comment.