-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Serialize network constants and logging with anchors (#153)
* Serialize network constants with anchors * Add an anchor serialization helper + add anchors to logging * Fix lint * Add basic tests for marshalling with anchors
- Loading branch information
1 parent
8cf47f9
commit b97b0b2
Showing
4 changed files
with
212 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package config | ||
|
||
import ( | ||
"reflect" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// MarshalYAML marshals the NetworkOverrides value to yaml handling anchors where necessary | ||
func (nc *NetworkOverrides) MarshalYAML() (interface{}, error) { | ||
return anchorHelper(nc, "network_overrides") | ||
} | ||
|
||
// MarshalYAML marshals the LoggingConfig value to yaml handling anchors where necessary | ||
func (lc *LoggingConfig) MarshalYAML() (interface{}, error) { | ||
return anchorHelper(lc, "logging") | ||
} | ||
|
||
// Anchorable defines the methods a type must implement to support anchors | ||
type Anchorable interface { | ||
AnchorNode() *yaml.Node | ||
SetAnchorNode(*yaml.Node) | ||
} | ||
|
||
func anchorHelper(in Anchorable, tag string) (*yaml.Node, error) { | ||
if in.AnchorNode() != nil { | ||
node := &yaml.Node{ | ||
Kind: yaml.AliasNode, | ||
Alias: in.AnchorNode(), | ||
Value: tag, | ||
} | ||
return node, nil | ||
} | ||
|
||
// Get the underlying value of 'in' for marshalling or else we end up recursively in this function | ||
value := reflect.ValueOf(in) | ||
if value.Kind() == reflect.Ptr { | ||
// Dereference if it's a pointer | ||
value = value.Elem() | ||
} | ||
|
||
// Marshal the struct to a yaml.Node | ||
var node yaml.Node | ||
if err := node.Encode(value.Interface()); err != nil { | ||
return nil, err | ||
} | ||
node.Anchor = tag | ||
|
||
// Store the node as the anchor for future iterations | ||
in.SetAnchorNode(&node) | ||
|
||
// Return the node to be marshalled | ||
return &node, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package config_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/chia-network/go-chia-libs/pkg/config" | ||
) | ||
|
||
// TestLoggingConfigAnchors verifies that logging is serialized with anchors | ||
func TestLoggingConfigAnchors(t *testing.T) { | ||
type testStruct struct { | ||
LoggingConfig1 *config.LoggingConfig `yaml:"logging1"` | ||
LoggingConfig2 *config.LoggingConfig `yaml:"logging2"` | ||
} | ||
loggingCfg := config.LoggingConfig{} | ||
testInstance := &testStruct{ | ||
LoggingConfig1: &loggingCfg, | ||
LoggingConfig2: &loggingCfg, | ||
} | ||
|
||
expected := `logging1: &logging | ||
log_stdout: false | ||
log_filename: "" | ||
log_level: "" | ||
log_maxfilesrotation: 0 | ||
log_maxbytesrotation: 0 | ||
log_use_gzip: false | ||
log_syslog: false | ||
log_syslog_host: "" | ||
log_syslog_port: 0 | ||
logging2: *logging | ||
` | ||
|
||
out, err := yaml.Marshal(testInstance) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expected, string(out)) | ||
} | ||
|
||
// TestNetworkOverridesAnchors verifies that logging is serialized with anchors | ||
func TestNetworkOverridesAnchors(t *testing.T) { | ||
type testStruct struct { | ||
Network1 *config.NetworkOverrides `yaml:"network_overrides1"` | ||
Network2 *config.NetworkOverrides `yaml:"network_overrides2"` | ||
} | ||
no := config.NetworkOverrides{ | ||
Constants: map[string]config.NetworkConstants{ | ||
"mainnet": {}, | ||
}, | ||
Config: map[string]config.NetworkConfig{ | ||
"mainnet": { | ||
AddressPrefix: "xch", | ||
DefaultFullNodePort: 8444, | ||
}, | ||
}, | ||
} | ||
testInstance := &testStruct{ | ||
Network1: &no, | ||
Network2: &no, | ||
} | ||
|
||
expected := `network_overrides1: &network_overrides | ||
constants: | ||
mainnet: | ||
GENESIS_CHALLENGE: "" | ||
GENESIS_PRE_FARM_POOL_PUZZLE_HASH: "" | ||
GENESIS_PRE_FARM_FARMER_PUZZLE_HASH: "" | ||
config: | ||
mainnet: | ||
address_prefix: xch | ||
default_full_node_port: 8444 | ||
network_overrides2: *network_overrides | ||
` | ||
|
||
out, err := yaml.Marshal(testInstance) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expected, string(out)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters