Skip to content

Commit

Permalink
Allows filling structs/maps/slices with the actual type in addition t…
Browse files Browse the repository at this point in the history
…o the json/yaml string
  • Loading branch information
cmmarslender committed Nov 21, 2024
1 parent eccfa8e commit 51c8154
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,9 @@ func setFieldByPath(v reflect.Value, path []string, value any) error {
if err := yaml.Unmarshal(yamlData, fieldValue.Addr().Interface()); err != nil {
return fmt.Errorf("failed to unmarshal yaml into field: %w", err)
}
// If we successfully replaced by doing yaml parsing into the field, then we should not try anything else
return nil
}
return nil
}

val := reflect.ValueOf(value)
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 @@ -103,16 +103,37 @@ func TestChiaConfig_SetFieldByPath_Lists(t *testing.T) {
assert.Equal(t, []string{}, defaultConfig.Seeder.StaticPeers)
assert.Equal(t, []config.Peer{}, defaultConfig.FullNode.FullNodePeers)

// Test json encoded version
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)

// Test with the actual type as the data to set
// First reset
defaultConfig.Seeder.StaticPeers = []string{}
assert.Equal(t, []string{}, defaultConfig.Seeder.StaticPeers)
err = defaultConfig.SetFieldByPath([]string{"seeder", "static_peers"}, []string{"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)

defaultConfig.FullNode.FullNodePeers = []config.Peer{}
assert.Equal(t, []config.Peer{}, defaultConfig.FullNode.FullNodePeers)
err = defaultConfig.SetFieldByPath([]string{"full_node", "full_node_peers"}, []config.Peer{
{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) {
Expand Down

0 comments on commit 51c8154

Please sign in to comment.