Skip to content

Commit

Permalink
Support setting entire slices when filling config values (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmmarslender authored Nov 21, 2024
1 parent ead46cf commit eccfa8e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func setFieldByPath(v reflect.Value, path []string, value any) error {

// Handle YAML (and therefore JSON) parsing for passing in entire structs/maps
// This is particularly useful if you want to pass in a whole blob of network constants at once
if fieldValue.Kind() == reflect.Struct || fieldValue.Kind() == reflect.Map {
if fieldValue.Kind() == reflect.Struct || fieldValue.Kind() == reflect.Map || fieldValue.Kind() == reflect.Slice {
if strValue, ok := value.(string); ok {
yamlData := []byte(strValue)
if err := yaml.Unmarshal(yamlData, fieldValue.Addr().Interface()); err != nil {
Expand Down
21 changes: 21 additions & 0 deletions pkg/config/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,27 @@ func TestChiaConfig_SetFieldByPath_FullObjects(t *testing.T) {
assert.Equal(t, "9eb3cec765fb3b3508f82e090374d5913d24806e739da31bcc4ab1767d9f1ca9", defaultConfig.NetworkOverrides.Constants["yamlnet"].GenesisChallenge)
}

// TestChiaConfig_SetFieldByPath_FullObjects Tests that we can pass in and correctly parse a whole section of config
// as json or yaml and that it gets set properly
func TestChiaConfig_SetFieldByPath_Lists(t *testing.T) {
defaultConfig, err := config.LoadDefaultConfig()
assert.NoError(t, err)
// Make assertions about the default state, to ensure the assumed initial values are correct
assert.Equal(t, []string{}, defaultConfig.Seeder.StaticPeers)
assert.Equal(t, []config.Peer{}, defaultConfig.FullNode.FullNodePeers)

err = defaultConfig.SetFieldByPath([]string{"seeder", "static_peers"}, `["node-test.chia.net","node-test-2.chia.net"]`)
assert.NoError(t, err)
assert.Equal(t, []string{"node-test.chia.net", "node-test-2.chia.net"}, defaultConfig.Seeder.StaticPeers)

err = defaultConfig.SetFieldByPath([]string{"full_node", "full_node_peers"}, `[{"host":"testnode.example.com","port":1234},{"host":"testnode2.example.com","port":5678}]`)
assert.NoError(t, err)
assert.Equal(t, []config.Peer{
{Host: "testnode.example.com", Port: 1234},
{Host: "testnode2.example.com", Port: 5678},
}, defaultConfig.FullNode.FullNodePeers)
}

func TestChiaConfig_FillValuesFromEnvironment(t *testing.T) {
defaultConfig, err := config.LoadDefaultConfig()
assert.NoError(t, err)
Expand Down

0 comments on commit eccfa8e

Please sign in to comment.