Skip to content

Commit

Permalink
Registration Override
Browse files Browse the repository at this point in the history
This path introduces the ability to override config registrations of the
same name.
  • Loading branch information
akutz committed Apr 28, 2017
1 parent 862741c commit a7ef3bf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions gofig.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ func SetUserConfigPath(path string) {
func Register(r types.ConfigRegistration) {
registrationsRWL.Lock()
defer registrationsRWL.Unlock()
for x, rr := range registrations {
if rr.Name() == r.Name() {
registrations[x] = r
return
}
}
registrations = append(registrations, r)
}

Expand Down
38 changes: 38 additions & 0 deletions gofig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,31 @@ func TestAssertConfigDefaults(t *testing.T) {
}
}

func TestAssertConfigDefaultsRegisterOverride(t *testing.T) {
Register(testReg1a())
defer func() {
Register(testReg1())
}()

newConfigDirs("TestAssertConfigDefaults", t)
wipeEnv()
c := New()

osDrivers := c.GetStringSlice("rexray.osDrivers")
volDrivers := c.GetStringSlice("rexray.volumeDrivers")

assertString(t, c, "rexray.host", "tcp://:7980")
assertString(t, c, "rexray.logLevel", "warn")

if len(osDrivers) != 1 || osDrivers[0] != "linux" {
t.Fatalf("osDrivers != []string{\"linux\"}, == %v", osDrivers)
}

if len(volDrivers) != 1 || volDrivers[0] != "docker" {
t.Fatalf("volumeDrivers != []string{\"docker\"}, == %v", volDrivers)
}
}

func TestAssertTestRegistration(t *testing.T) {
newConfigDirs("TestAssertTestRegistration", t)
wipeEnv()
Expand Down Expand Up @@ -870,6 +895,19 @@ func testReg1() *configReg {
return r
}

func testReg1a() *configReg {
r := newRegistration("Global")
r.SetYAML(`rexray:
host: tcp://:7980
logLevel: warn
`)
r.Key(types.String, "h", "tcp://:7980",
"The REX-Ray host", "rexray.host")
r.Key(types.String, "l", "warn",
"The log level (error, warn, info, debug)", "rexray.logLevel")
return r
}

func testReg2() *configReg {
r := newRegistration("Driver")
r.SetYAML(`rexray:
Expand Down

0 comments on commit a7ef3bf

Please sign in to comment.