Skip to content
Open
Show file tree
Hide file tree
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
34 changes: 24 additions & 10 deletions x/config/cli/binder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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))
Expand Down
41 changes: 41 additions & 0 deletions x/config/cli/kebab_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
2 changes: 1 addition & 1 deletion x/config/cli/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 40 additions & 4 deletions x/config/cli/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion x/config/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions x/config/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading