-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add constant-case strategy in building env key names #33
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import ( | |
"strconv" | ||
"strings" | ||
"time" | ||
"unicode" | ||
|
||
"github.com/mitchellh/mapstructure" | ||
"github.com/pelletier/go-toml/v2" | ||
|
@@ -112,14 +113,15 @@ func defaultFig() *fig { | |
} | ||
|
||
type fig struct { | ||
filename string | ||
dirs []string | ||
tag string | ||
timeLayout string | ||
useEnv bool | ||
useStrict bool | ||
ignoreFile bool | ||
envPrefix string | ||
filename string | ||
dirs []string | ||
tag string | ||
timeLayout string | ||
useEnv bool | ||
useConstantCase bool | ||
useStrict bool | ||
ignoreFile bool | ||
envPrefix string | ||
} | ||
|
||
func (f *fig) Load(cfg interface{}) error { | ||
|
@@ -312,12 +314,48 @@ func (f *fig) setFromEnv(fv reflect.Value, key string) error { | |
func (f *fig) formatEnvKey(key string) string { | ||
// loggers[0].level --> loggers_0_level | ||
key = strings.NewReplacer(".", "_", "[", "_", "]", "").Replace(key) | ||
if f.useConstantCase { | ||
key = f.toConstantCase(key) | ||
} | ||
if f.envPrefix != "" { | ||
key = fmt.Sprintf("%s_%s", f.envPrefix, key) | ||
} | ||
return strings.ToUpper(key) | ||
} | ||
|
||
func (f *fig) toConstantCase(key string) string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is called constant case but it's not upper casing the output. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes |
||
var b strings.Builder | ||
runes := []rune(key) | ||
|
||
for i := 0; i < len(runes); i++ { | ||
if !unicode.IsUpper(runes[i]) { | ||
b.WriteRune(runes[i]) | ||
continue | ||
} | ||
|
||
if i == 0 { | ||
b.WriteRune(runes[i]) | ||
continue | ||
} | ||
|
||
if unicode.IsUpper(runes[i-1]) || !unicode.IsLetter(runes[i-1]) { | ||
if len(runes) == i+1 { | ||
b.WriteRune(runes[i]) | ||
} else if unicode.IsLower(runes[i+1]) { | ||
b.WriteString("_") | ||
b.WriteRune(runes[i]) | ||
} else { | ||
b.WriteRune(runes[i]) | ||
} | ||
} else { | ||
b.WriteString("_") | ||
b.WriteRune(runes[i]) | ||
} | ||
} | ||
|
||
return b.String() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With input = "helloWORLD" this algorithm will output "helloWORLD". Should it not output "hello_WORLD"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be helloWorld, I did this to avoid converting USA to u_s_a. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would "helloWORLD" -> "hello_WORLD" imply "USA" -> "u_s_a"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If next letter of an upper case letter is lower case, it means it has to be separated with an underline. if an upper case letter is preceded by another upper case letter, it means it's something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Current behavior only checks next letters each letter, that's why we can convert something like I'm gonna think about another algorithm. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I updated the algorithm and added more test cases, please check if that's what you meant. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Algorithm seems to work but is a little hard to read - what do you think about this version? func (f *fig) toConstantCase(key string) string {
var b strings.Builder
runes := []rune(key)
lowerToUpper := func(i int) bool {
return i > 0 && unicode.IsLower(runes[i-1]) && unicode.IsUpper(runes[i])
}
upperToLower := func(i int) bool {
return i+1 < len(runes) && unicode.IsUpper(runes[i]) && unicode.IsLower(runes[i+1])
}
for i := range runes {
if i > 0 && (lowerToUpper(i) || upperToLower(i)) {
b.WriteByte('_')
}
b.WriteRune(runes[i])
}
return strings.ToUpper(b.String())
} |
||
} | ||
|
||
// setDefaultValue calls setValue but disallows booleans from | ||
// being set. | ||
func (f *fig) setDefaultValue(fv reflect.Value, val string) error { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can introduce an abstraction here so that multiple strategies can coexist without cluttering this struct and
formatEnvKey
with each option:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we pass the env prefix to transformers? it's needed for adding prefix to the generated env key.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah we should add it