-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(config): Deprecate
--value
flag in favor of --var
- Loading branch information
Showing
11 changed files
with
164 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package config | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
type Vars map[string]any | ||
|
||
func (vars Vars) Fill(src map[string]string) { | ||
for key, val := range src { | ||
split := strings.Split(key, ".") | ||
if len(split) == 1 { | ||
vars[key] = val | ||
} else { | ||
vars.SetNested(val, split...) | ||
} | ||
} | ||
} | ||
|
||
func (vars Vars) SetNested(v any, k ...string) { | ||
setNested(vars, v, k...) | ||
} | ||
|
||
func setNested(vars Vars, val any, keys ...string) { | ||
key := keys[0] | ||
if len(keys) == 1 { | ||
vars[key] = val | ||
} else { | ||
if _, ok := vars[key]; !ok { | ||
vars[key] = Vars{} | ||
} | ||
setNested(vars[key].(Vars), val, keys[1:]...) | ||
} | ||
} | ||
|
||
func (vars Vars) Val() any { | ||
return vars["Value"] | ||
} | ||
|
||
func (vars Vars) V() any { | ||
return vars["Value"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestVars_Fill(t *testing.T) { | ||
type args struct { | ||
rawValues map[string]string | ||
} | ||
tests := []struct { | ||
name string | ||
vars Vars | ||
args args | ||
want Vars | ||
}{ | ||
{"simple", make(Vars), args{map[string]string{"a": "a"}}, Vars{"a": "a"}}, | ||
{"nested", make(Vars), args{map[string]string{"a.b": "a"}}, Vars{"a": Vars{"b": "a"}}}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tt.vars.Fill(tt.args.rawValues) | ||
assert.Equal(t, tt.want, tt.vars) | ||
}) | ||
} | ||
} | ||
|
||
func TestVars_SetNested(t *testing.T) { | ||
type args struct { | ||
v any | ||
k []string | ||
} | ||
tests := []struct { | ||
name string | ||
vars Vars | ||
args args | ||
want Vars | ||
}{ | ||
{"simple", make(Vars), args{"a", []string{"a"}}, Vars{"a": "a"}}, | ||
{"nested", make(Vars), args{"a", []string{"a", "b"}}, Vars{"a": Vars{"b": "a"}}}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tt.vars.SetNested(tt.args.v, tt.args.k...) | ||
assert.Equal(t, tt.want, tt.vars) | ||
}) | ||
} | ||
} | ||
|
||
func TestVars_V(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
vars Vars | ||
want any | ||
}{ | ||
{"simple", Vars{"Value": "a"}, "a"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := tt.vars.V() | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} | ||
|
||
func TestVars_Val(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
vars Vars | ||
want any | ||
}{ | ||
{"simple", Vars{"Value": "a"}, "a"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := tt.vars.Val() | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} | ||
|
||
func Test_setNested(t *testing.T) { | ||
type args struct { | ||
input Vars | ||
value any | ||
keys []string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want Vars | ||
}{ | ||
{"simple", args{make(Vars), "a", []string{"a"}}, Vars{"a": "a"}}, | ||
{"nested", args{make(Vars), "a", []string{"a", "b"}}, Vars{"a": Vars{"b": "a"}}}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
setNested(tt.args.input, tt.args.value, tt.args.keys...) | ||
assert.Equal(t, tt.want, tt.args.input) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters