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

Allow passing in a whole config section as a json/yaml string #146

Merged
merged 1 commit into from
Sep 10, 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
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