From c8fc6963c4fcce2e4f81f3d0ee97d2ac9d56a20d Mon Sep 17 00:00:00 2001 From: Charles Neill <1749665+cneill@users.noreply.github.com> Date: Thu, 17 Aug 2023 20:40:19 -0500 Subject: [PATCH] cleaning up a little --- utils.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/utils.go b/utils.go index 75eb995..24a15e0 100644 --- a/utils.go +++ b/utils.go @@ -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(' ') } } @@ -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{}