Skip to content

Commit

Permalink
feature: add ConfigsWithSourceNames() function (#131)
Browse files Browse the repository at this point in the history
* feature: add ConfigsWithSourceNames() function, which returns all the key values along with its source, extra details can be used for debugging.

* edit test case.

Co-authored-by: Guang Yang <[email protected]>
  • Loading branch information
yoo-bit and Guang Yang authored Dec 29, 2020
1 parent 7450779 commit efa7972
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
10 changes: 10 additions & 0 deletions archaius.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ func GetConfigs() map[string]interface{} {
return manager.Configs()
}

// GetConfigsWithSourceNames gives the information about all configurations
// each config key, along with its source will be returned
// the returned map will be like:
// map[string]interface{}{
// key string: map[string]interface{"value": value, "sourceName": sourceName}
// }
func GetConfigsWithSourceNames() map[string]interface{} {
return manager.ConfigsWithSourceNames()
}

// AddDimensionInfo adds a NewDimensionInfo of which configurations needs to be taken
func AddDimensionInfo(labels map[string]string) (map[string]string, error) {
config, err := manager.AddDimensionInfo(labels)
Expand Down
9 changes: 9 additions & 0 deletions archaius_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ func TestConfig_Get(t *testing.T) {
m := archaius.GetConfigs()
t.Log(m)
assert.Equal(t, 1, m["number"])

// case: view config keys and its original source
// context: archaius is initialized and has config key: "number", value: 1 from source: FileSource
// expected result: able to get config key and its original source
t.Run("get all configs along with source", func(t *testing.T) {
c1 := archaius.GetConfigsWithSourceNames()
assert.Equal(t, 1, c1["number"].(map[string]interface{})["value"])
assert.Equal(t, "FileSource", c1["number"].(map[string]interface{})["source"])
})
}
func TestConfig_GetInt(t *testing.T) {
s := archaius.GetInt("number", 0)
Expand Down
20 changes: 20 additions & 0 deletions source/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,26 @@ func (m *Manager) Configs() map[string]interface{} {
return config
}

// ConfigsWithSourceNames returns all the key values along with its source name
// the returned map will be like:
// map[string]interface{}{
// key string: map[string]interface{"value": value, "source": sourceName}
// }
func (m *Manager) ConfigsWithSourceNames() map[string]interface{} {
config := make(map[string]interface{}, 0)

m.ConfigurationMap.Range(func(key, value interface{}) bool {
sValue := m.configValueBySource(key.(string), value.(string))
if sValue == nil {
return true
}
// each key stores its value and source name
config[key.(string)] = map[string]interface{}{"value": sValue, "source": value}
return true
})
return config
}

// AddDimensionInfo adds the dimensionInfo to the list of which configurations needs to be pulled
func (m *Manager) AddDimensionInfo(labels map[string]string) (map[string]string, error) {
config := make(map[string]string, 0)
Expand Down

0 comments on commit efa7972

Please sign in to comment.