Skip to content

Commit dec9c81

Browse files
committed
rename PostSet to SetV and split the dynamic log level example/code
1 parent ac3ba1f commit dec9c81

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

dyngeneric.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,14 @@ func (d *DynValue[T]) Set(rawInput string) error {
225225
if err != nil {
226226
return err
227227
}
228-
return d.PostSet(val)
228+
return d.SetV(val)
229229
}
230230

231-
func (d *DynValue[T]) PostSet(val T) error {
231+
// SetV is for when the value is already parsed/of the correct type.
232+
// Validators and notifiers are triggered (only input mutator and parsing from string is skipped).
233+
// Ideally this would be called Set() and the other SetAsString() but
234+
// the flag api needs Set() to be the one taking a string.
235+
func (d *DynValue[T]) SetV(val T) error {
232236
if d.mutator != nil {
233237
val = d.mutator(val)
234238
}

dynjson.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (d *DynJSONValue) Set(rawInput string) error {
5757
if err := json.Unmarshal([]byte(input), val); err != nil {
5858
return err
5959
}
60-
return d.PostSet(val)
60+
return d.SetV(val)
6161
}
6262

6363
// String returns the canonical string representation of the type.

dynloglevel/dynloglevel.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ func LoggerFlagSetup() {
3535
return // avoid redefining flag/make it ok for multiple function to init this.
3636
}
3737
// virtual dynLevel flag that maps back to actual level
38-
_ = dflag.DynString(flag.CommandLine, "loglevel", log.GetLogLevel().String(),
39-
fmt.Sprintf("loglevel, one of %v", log.LevelToStrA)).WithInputMutator(
38+
defVal := log.GetLogLevel().String()
39+
usage := fmt.Sprintf("`loglevel`, one of %v", log.LevelToStrA)
40+
flag := dflag.New(defVal, usage).WithInputMutator(
4041
func(inp string) string {
4142
// The validation map has full lowercase and capitalized first letter version
4243
return strings.ToLower(strings.TrimSpace(inp))
@@ -48,6 +49,7 @@ func LoggerFlagSetup() {
4849
func(old, newStr string) {
4950
_ = log.SetLogLevelStr(newStr) // will succeed as we just validated it first
5051
})
52+
dflag.Flag("loglevel", flag)
5153
done = true
5254
}
5355

0 commit comments

Comments
 (0)