Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support setting entire slices when filling config values #176

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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