Skip to content

Commit

Permalink
Merge pull request #2132 from oasisprotocol/kostko/fix/rofl-fee-polic…
Browse files Browse the repository at this point in the history
…y-yaml

client-sdk/go: Fix fee policy YAML serialization
  • Loading branch information
kostko authored Jan 13, 2025
2 parents 4d23a00 + a491581 commit a0508eb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
19 changes: 17 additions & 2 deletions client-sdk/go/modules/rofl/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ const (
FeePolicyInstancePays FeePolicy = 1
// FeePolicyEndorsingNodePays is a fee policy where the endorsing node pays the gas fees.
FeePolicyEndorsingNodePays FeePolicy = 2

nameFeePolicyInstancePays = "instance"
nameFeePolicyEndorsingNodePays = "endorsing_node"
)

// UnmarshalYAML implements yaml.Unmarshaler.
Expand All @@ -59,9 +62,9 @@ func (fp *FeePolicy) UnmarshalYAML(value *yaml.Node) error {
}

switch feePolicyStr {
case "instance":
case nameFeePolicyInstancePays:
*fp = FeePolicyInstancePays
case "endorsing_node":
case nameFeePolicyEndorsingNodePays:
*fp = FeePolicyEndorsingNodePays
default:
return fmt.Errorf("unsupported fee policy: '%s'", feePolicyStr)
Expand All @@ -71,3 +74,15 @@ func (fp *FeePolicy) UnmarshalYAML(value *yaml.Node) error {
return fmt.Errorf("unsupported fee policy type")
}
}

// MarshalYAML implements yaml.Marshaler.
func (fp FeePolicy) MarshalYAML() (interface{}, error) {
switch fp {
case FeePolicyInstancePays:
return nameFeePolicyInstancePays, nil
case FeePolicyEndorsingNodePays:
return nameFeePolicyEndorsingNodePays, nil
default:
return nil, fmt.Errorf("unsupported fee policy: %d", fp)
}
}
7 changes: 7 additions & 0 deletions client-sdk/go/modules/rofl/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ func TestPolicySerialization(t *testing.T) {
if tc.ok {
require.NoError(err, "yaml.Unmarshal")
require.EqualValues(tc.expected, dec)

var enc []byte
enc, err = yaml.Marshal(dec)
require.NoError(err, "yaml.Marshal")
err = yaml.Unmarshal(enc, &dec)
require.NoError(err, "yaml.Unmarshal")
require.EqualValues(tc.expected, dec)
} else {
require.Error(err, "yaml.Unmarshal")
}
Expand Down

0 comments on commit a0508eb

Please sign in to comment.