diff --git a/x/config/cli/binder_test.go b/x/config/cli/binder_test.go index 8c20b8501b..aafe1044fd 100644 --- a/x/config/cli/binder_test.go +++ b/x/config/cli/binder_test.go @@ -382,6 +382,20 @@ func TestKeyNames(t *testing.T) { }) } +func TestAcronymInEnvVarName(t *testing.T) { + type ocrConfig struct{ OnchainKeyBundleIDs string } + + var got ocrConfig + root := newRoot(t) + b, err := New(Options{Markup: testOptions.Markup, Prefixes: []string{"CL"}}) + require.NoError(t, err) + require.NoError(t, b.RegisterInNamespace(root, "OCR", &got)) + + t.Setenv("CL_OCR_ONCHAIN_KEY_BUNDLE_IDS", "x") + require.NoError(t, root.Execute()) + assert.Equal(t, "x", got.OnchainKeyBundleIDs) +} + func decodesTo[T any](t *testing.T, want T, args ...string) { t.Helper() @@ -541,17 +555,17 @@ func TestNumericFlagsAreSized(t *testing.T) { for _, tc := range []struct{ flag, limit, past string }{ {"int", "9223372036854775807", "9223372036854775808"}, - {"int-8", "-128", "-129"}, - {"int-16", "32767", "32768"}, - {"int-32", "-2147483648", "-2147483649"}, - {"int-64", "9223372036854775807", "9223372036854775808"}, + {"int8", "-128", "-129"}, + {"int16", "32767", "32768"}, + {"int32", "-2147483648", "-2147483649"}, + {"int64", "9223372036854775807", "9223372036854775808"}, {"uint", "18446744073709551615", "18446744073709551616"}, - {"uint-8", "255", "256"}, - {"uint-16", "65535", "65536"}, - {"uint-32", "4294967295", "4294967296"}, - {"uint-64", "18446744073709551615", "18446744073709551616"}, - {"float-32", "3.4e38", "3.5e38"}, - {"float-64", "1.7e308", "1.8e308"}, + {"uint8", "255", "256"}, + {"uint16", "65535", "65536"}, + {"uint32", "4294967295", "4294967296"}, + {"uint64", "18446744073709551615", "18446744073709551616"}, + {"float32", "3.4e38", "3.5e38"}, + {"float64", "1.7e308", "1.8e308"}, } { t.Run(tc.flag, func(t *testing.T) { require.NoError(t, run(t, &hasEveryNumber{}, testOptions, "--"+tc.flag, tc.limit)) diff --git a/x/config/cli/kebab_test.go b/x/config/cli/kebab_test.go new file mode 100644 index 0000000000..18193fcd77 --- /dev/null +++ b/x/config/cli/kebab_test.go @@ -0,0 +1,41 @@ +package cli + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestKebab(t *testing.T) { + t.Run("a lone s after an acronym is its plural", func(t *testing.T) { + for in, want := range map[string]string{ + "OnchainKeyBundleIDs": "onchain-key-bundle-ids", + "IDsByChain": "ids-by-chain", + "PeerURLs": "peer-urls", + "APIs": "apis", + } { + assert.Equal(t, want, kebab(in), in) + } + }) + t.Run("digits stay with the word before them", func(t *testing.T) { + for in, want := range map[string]string{ + "V2Bootstrappers": "v2-bootstrappers", + "OCR3Config": "ocr3-config", + "Http2Server": "http2-server", + "MaxRetries3": "max-retries3", + } { + assert.Equal(t, want, kebab(in), in) + } + }) + + t.Run("basic words", func(t *testing.T) { + for in, want := range map[string]string{ + "TwoWords": "two-words", + "HTTPServer": "http-server", + "ASet": "a-set", + "max_size": "max-size", + } { + assert.Equal(t, want, kebab(in), in) + } + }) +} diff --git a/x/config/cli/profile_test.go b/x/config/cli/profile_test.go index f8c73d1700..d21b384710 100644 --- a/x/config/cli/profile_test.go +++ b/x/config/cli/profile_test.go @@ -111,7 +111,7 @@ func TestOverlappingProfilesAreRejected(t *testing.T) { require.ErrorContains(t, newBinder(t, testOptions).Register(newRoot(t), &twoSelectors{}, Profile(func(c *twoSelectors) *uint32 { return &c.Selector }, map[uint32]func(*twoSelectors){}), Profile(func(c *twoSelectors) *uint32 { return &c.Selector2 }, map[uint32]func(*twoSelectors){})), - "profiles for selector and selector-2 overlap: each rebuilds its whole section, so one would drop the other's values") + "profiles for selector and selector2 overlap: each rebuilds its whole section, so one would drop the other's values") type nestsProfiled struct { Selector uint32 diff --git a/x/config/cli/walk.go b/x/config/cli/walk.go index 615766cdb3..109e8596fa 100644 --- a/x/config/cli/walk.go +++ b/x/config/cli/walk.go @@ -6,8 +6,7 @@ import ( "reflect" "slices" "strings" - - "github.com/iancoleman/strcase" + "unicode" "github.com/smartcontractkit/chainlink-common/x/config/commentparsing" "github.com/smartcontractkit/chainlink-common/x/config/internal/decode" @@ -56,7 +55,7 @@ func bindStruct[T any](entry *typedEntry[T]) error { scope := walkScope{ancestors: []reflect.Type{v.Type()}} if ns := entry.ns; ns != "" { - scope.key, scope.fileKey = strcase.ToKebab(ns), []string{ns} + scope.key, scope.fileKey = kebab(ns), []string{ns} } return entry.walk(v, scope) @@ -139,13 +138,50 @@ func (s walkScope) child(field reflect.StructField, key string) walkScope { next.goPath = append(slices.Clone(s.goPath), field.Name) if key != "" { // Kebab case, so a rename reads as if it were the name. - next.key = strings.TrimPrefix(s.key+"."+strcase.ToKebab(key), ".") + next.key = strings.TrimPrefix(s.key+"."+kebab(key), ".") next.fileKey = append(slices.Clone(s.fileKey), key) } return next } +// kebab is name in kebab case. Digits stay with the word before them, and a lone s after an acronym is its plural, +// so V2Bootstrappers is v2-bootstrappers and OnchainKeyBundleIDs is onchain-key-bundle-ids. +func kebab(name string) string { + r := []rune(name) + var b strings.Builder + for i, c := range r { + if c == '_' || c == '-' { + b.WriteByte('-') + continue + } + + if i > 0 && unicode.IsUpper(c) && startsWord(r, i) { + b.WriteByte('-') + } + + b.WriteRune(unicode.ToLower(c)) + } + + return b.String() +} + +// startsWord reports whether the capital at r[i] begins a word: after a lowercase letter or a digit, or as the last +// capital of an acronym before a lowercase word, as the S in HTTPServer. A lone s after an acronym is its plural. +func startsWord(r []rune, i int) bool { + prev := r[i-1] + if unicode.IsLower(prev) || unicode.IsDigit(prev) { + return true + } + + if !unicode.IsUpper(prev) || i+1 >= len(r) || !unicode.IsLower(r[i+1]) { + return false + } + + plural := r[i+1] == 's' && (i+2 == len(r) || !unicode.IsLower(r[i+2])) + return !plural +} + // A nil pointer section still gets its flags, so its zero value stands in for it. func derefOrZero(f reflect.Value) reflect.Value { for f.Kind() == reflect.Pointer { diff --git a/x/config/go.mod b/x/config/go.mod index 76e36040cd..5c1841e5ae 100644 --- a/x/config/go.mod +++ b/x/config/go.mod @@ -5,7 +5,6 @@ go 1.27.3 require ( github.com/go-playground/validator/v10 v10.26.0 github.com/huandu/go-clone v1.7.3 - github.com/iancoleman/strcase v0.3.0 github.com/pelletier/go-toml/v2 v2.4.3 github.com/smartcontractkit/chainlink-common v0.11.2-0.20261027153112-98a50810196d github.com/spf13/cobra v1.10.2 diff --git a/x/config/go.sum b/x/config/go.sum index 796bdc7d37..29e606e5fc 100644 --- a/x/config/go.sum +++ b/x/config/go.sum @@ -19,8 +19,6 @@ github.com/huandu/go-assert v1.1.5 h1:fjemmA7sSfYHJD7CUqs9qTwwfdNAx7/j2/ZlHXzNB3 github.com/huandu/go-assert v1.1.5/go.mod h1:yOLvuqZwmcHIC5rIzrBhT7D3Q9c3GFnd0JrPVhn/06U= github.com/huandu/go-clone v1.7.3 h1:rtQODA+ABThEn6J5LBTppJfKmZy/FwfpMUWa8d01TTQ= github.com/huandu/go-clone v1.7.3/go.mod h1:ReGivhG6op3GYr+UY3lS6mxjKp7MIGTknuU5TbTVaXE= -github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI= -github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=