Skip to content

Commit

Permalink
Allow passing in a whole config section as a json/yaml string (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmmarslender authored Sep 10, 2024
1 parent 9298d82 commit fa1e8d0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
14 changes: 14 additions & 0 deletions pkg/config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strconv"
"strings"

"gopkg.in/yaml.v3"

"github.com/chia-network/go-chia-libs/pkg/types"
)

Expand Down Expand Up @@ -191,6 +193,18 @@ func setFieldByPath(v reflect.Value, path []string, value any) error {
return nil
}

// 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 strValue, ok := value.(string); ok {
yamlData := []byte(strValue)
if err := yaml.Unmarshal(yamlData, fieldValue.Addr().Interface()); err != nil {
return fmt.Errorf("failed to unmarshal yaml into field: %w", err)
}
}
return nil
}

val := reflect.ValueOf(value)

if fieldValue.Type() != val.Type() {
Expand Down
30 changes: 30 additions & 0 deletions pkg/config/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,36 @@ func TestChiaConfig_SetFieldByPath(t *testing.T) {
assert.Equal(t, defaultConfig.Logging.LogLevel, "INFO")
}

// 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_FullObjects(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, uint16(8444), defaultConfig.FullNode.Port)
assert.Equal(t, uint16(8555), defaultConfig.FullNode.RPCPort)
assert.NotNil(t, defaultConfig.NetworkOverrides.Constants["mainnet"])
assert.Equal(t, defaultConfig.NetworkOverrides.Constants["mainnet"].DifficultyConstantFactor, types.Uint128{})
assert.Equal(t, defaultConfig.SelectedNetwork, "mainnet")
assert.Equal(t, defaultConfig.Logging.LogLevel, "WARNING")

// Test passing in json blobs
err = defaultConfig.SetFieldByPath([]string{"network_overrides", "constants"}, `{"jsonnet":{"DIFFICULTY_CONSTANT_FACTOR":44445555,"GENESIS_CHALLENGE":e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806}}`)
assert.NoError(t, err)
assert.NotNil(t, defaultConfig.NetworkOverrides.Constants["jsonnet"])
assert.Equal(t, types.Uint128From64(44445555), defaultConfig.NetworkOverrides.Constants["jsonnet"].DifficultyConstantFactor)
assert.Equal(t, "e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806", defaultConfig.NetworkOverrides.Constants["jsonnet"].GenesisChallenge)

// Test passing in yaml blobs
err = defaultConfig.SetFieldByPath([]string{"network_overrides", "constants"}, `yamlnet:
DIFFICULTY_CONSTANT_FACTOR: 44445555
GENESIS_CHALLENGE: e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806`)
assert.NoError(t, err)
assert.NotNil(t, defaultConfig.NetworkOverrides.Constants["yamlnet"])
assert.Equal(t, types.Uint128From64(44445555), defaultConfig.NetworkOverrides.Constants["yamlnet"].DifficultyConstantFactor)
assert.Equal(t, "e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806", defaultConfig.NetworkOverrides.Constants["yamlnet"].GenesisChallenge)
}

func TestChiaConfig_FillValuesFromEnvironment(t *testing.T) {
defaultConfig, err := config.LoadDefaultConfig()
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/rpc/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (s *DaemonService) IsRunning(opts *IsRunningOptions) (*IsRunningResponse, *
}

// DaemonDeleteAllKeysOpts options for delete all keys request
type DaemonDeleteAllKeysOpts struct {}
type DaemonDeleteAllKeysOpts struct{}

// DaemonDeleteAllKeysResponse response when deleting all keys
type DaemonDeleteAllKeysResponse struct {
Expand Down

0 comments on commit fa1e8d0

Please sign in to comment.