-
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.
Add wonkydict to account for default empty values of either [] or {}
- Loading branch information
1 parent
87d4a60
commit a171aa4
Showing
3 changed files
with
79 additions
and
9 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
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,25 @@ | ||
package types | ||
|
||
// WonkySet is just an alias for map[string]string that will unmarshal correctly from the empty forms {} and [] | ||
// In chia-blockchain, the default for some sets was incorrectly [] in the initial config | ||
// so this ensures compatability with both ways the empty values could show up | ||
type WonkySet map[string]string | ||
|
||
// UnmarshalYAML implements the yaml.Unmarshaler interface. | ||
func (ws *WonkySet) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
// Attempt to unmarshal into a slice of strings. | ||
var slice []string | ||
if err := unmarshal(&slice); err == nil { | ||
*ws = make(map[string]string) | ||
return nil | ||
} | ||
|
||
// Attempt to unmarshal into a map. | ||
var m map[string]string | ||
if err := unmarshal(&m); err != nil { | ||
return err | ||
} | ||
|
||
*ws = m | ||
return 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,45 @@ | ||
package types_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/chia-network/go-chia-libs/pkg/config" | ||
) | ||
|
||
// TestWonkySet_UnmarshalYAML ensures that this will unmarshal both empty list [] and dict {} into empty map[string]string | ||
// And also ensures an actal !!set as it would show up in the yaml unmarshals correctly | ||
func TestWonkySet_UnmarshalYAML(t *testing.T) { | ||
var yamlWithList = []byte(` | ||
farmer: | ||
pool_public_keys: [] | ||
`) | ||
var yamlWithDict = []byte(` | ||
farmer: | ||
pool_public_keys: {} | ||
`) | ||
|
||
var yamlWithData = []byte(` | ||
farmer: | ||
pool_public_keys: !!set | ||
abc123: null | ||
456xyz: null | ||
`) | ||
|
||
cfg := &config.ChiaConfig{} | ||
err := yaml.Unmarshal(yamlWithList, cfg) | ||
assert.NoError(t, err) | ||
assert.Len(t, cfg.Farmer.PoolPublicKeys, 0) | ||
|
||
cfg = &config.ChiaConfig{} | ||
err = yaml.Unmarshal(yamlWithDict, cfg) | ||
assert.NoError(t, err) | ||
assert.Len(t, cfg.Farmer.PoolPublicKeys, 0) | ||
|
||
cfg = &config.ChiaConfig{} | ||
err = yaml.Unmarshal(yamlWithData, cfg) | ||
assert.NoError(t, err) | ||
assert.Len(t, cfg.Farmer.PoolPublicKeys, 2) | ||
} |