From 82bd7f24f1c29d665f8b2d8cd0d4e381f68eb3cb Mon Sep 17 00:00:00 2001 From: a <a@tuxpa.in> Date: Tue, 29 Aug 2023 19:07:38 -0500 Subject: [PATCH] one less --- contrib/openrpc/generate/generate.go | 4 +- contrib/openrpc/util/camel.go | 67 ++++++++++++++++++++++++++++ go.mod | 1 - go.sum | 2 - 4 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 contrib/openrpc/util/camel.go diff --git a/contrib/openrpc/generate/generate.go b/contrib/openrpc/generate/generate.go index 2404677..a29d268 100644 --- a/contrib/openrpc/generate/generate.go +++ b/contrib/openrpc/generate/generate.go @@ -11,14 +11,14 @@ import ( "gfx.cafe/open/jrpc/contrib/openrpc/templates" "gfx.cafe/open/jrpc/contrib/openrpc/types" - "github.com/iancoleman/strcase" + "gfx.cafe/open/jrpc/contrib/openrpc/util" ) var funcs = template.FuncMap{ "list": func(v ...any) []any { return v }, - "camelCase": strcase.ToCamel, + "camelCase": util.ToCamel, "goType": func(v string) string { switch v { case "boolean": diff --git a/contrib/openrpc/util/camel.go b/contrib/openrpc/util/camel.go new file mode 100644 index 0000000..b4b01fb --- /dev/null +++ b/contrib/openrpc/util/camel.go @@ -0,0 +1,67 @@ +package util + +import ( + "strings" + "sync" +) + +var uppercaseAcronym = sync.Map{} + +//"ID": "id", + +// ConfigureAcronym allows you to add additional words which will be considered acronyms +func ConfigureAcronym(key, val string) { + uppercaseAcronym.Store(key, val) +} + +// Converts a string to CamelCase +func toCamelInitCase(s string, initCase bool) string { + s = strings.TrimSpace(s) + if s == "" { + return s + } + a, hasAcronym := uppercaseAcronym.Load(s) + if hasAcronym { + s = a.(string) + } + + n := strings.Builder{} + n.Grow(len(s)) + capNext := initCase + prevIsCap := false + for i, v := range []byte(s) { + vIsCap := v >= 'A' && v <= 'Z' + vIsLow := v >= 'a' && v <= 'z' + if capNext { + if vIsLow { + v += 'A' + v -= 'a' + } + } else if i == 0 { + if vIsCap { + v += 'a' + v -= 'A' + } + } else if prevIsCap && vIsCap && !hasAcronym { + v += 'a' + v -= 'A' + } + prevIsCap = vIsCap + + if vIsCap || vIsLow { + n.WriteByte(v) + capNext = false + } else if vIsNum := v >= '0' && v <= '9'; vIsNum { + n.WriteByte(v) + capNext = true + } else { + capNext = v == '_' || v == ' ' || v == '-' || v == '.' + } + } + return n.String() +} + +// ToCamel converts a string to CamelCase +func ToCamel(s string) string { + return toCamelInitCase(s, true) +} diff --git a/go.mod b/go.mod index e01e5d8..2c59317 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,6 @@ require ( github.com/alecthomas/kong v0.8.0 github.com/go-faster/jx v1.1.0 github.com/goccy/go-json v0.10.2 - github.com/iancoleman/strcase v0.3.0 github.com/rs/xid v1.5.0 github.com/stretchr/testify v1.8.4 sigs.k8s.io/yaml v1.3.0 diff --git a/go.sum b/go.sum index 4334c9f..9665661 100644 --- a/go.sum +++ b/go.sum @@ -28,8 +28,6 @@ github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg= -github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI= -github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I= github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= -- GitLab