Skip to content

Commit 0eb0c0f

Browse files
author
Andrea Spacca
authored
Config setter (#119)
* TDD Config.SetField * Config.SetField
1 parent 605bde0 commit 0eb0c0f

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

pkg/genlib/config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,8 @@ func (c Config) GetField(fieldName string) (ConfigField, bool) {
154154
v, ok := c.m[fieldName]
155155
return v, ok
156156
}
157+
158+
func (c Config) SetField(fieldName string, configField ConfigField) {
159+
configField.Name = fieldName
160+
c.m[fieldName] = configField
161+
}

pkg/genlib/config/config_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ func TestLoadConfig(t *testing.T) {
3131
assert.Equal(t, "foobar", f.Value.(string))
3232
}
3333

34+
func TestSetField(t *testing.T) {
35+
fs := afero.NewMemMapFs()
36+
configFile := "/cfg.yml"
37+
38+
data := []byte(sampleConfigFile)
39+
afero.WriteFile(fs, configFile, data, 0666)
40+
41+
cfg, err := LoadConfig(fs, configFile)
42+
assert.Nil(t, err)
43+
44+
cfg.SetField("field", ConfigField{Value: "foobaz"})
45+
f, ok := cfg.GetField("field")
46+
assert.True(t, ok)
47+
assert.Equal(t, "field", f.Name)
48+
assert.Equal(t, "foobaz", f.Value.(string))
49+
}
50+
3451
func TestIsValidForDateField(t *testing.T) {
3552
testCases := []struct {
3653
scenario string

0 commit comments

Comments
 (0)