callflag: allow to marshal call flags in YAML

Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgeniy Stratonikov 2022-02-11 13:30:15 +03:00
parent c212b2be95
commit 4aab85ef51
2 changed files with 33 additions and 0 deletions

View file

@ -117,3 +117,21 @@ func (f *CallFlag) UnmarshalJSON(data []byte) error {
*f = flag *f = flag
return nil return nil
} }
// MarshalYAML implements the YAML marshaler interface.
func (f CallFlag) MarshalYAML() (interface{}, error) {
return f.String(), nil
}
// UnmarshalYAML implements the YAML unmarshaler interface.
func (f *CallFlag) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
err := unmarshal(&s)
if err != nil {
return err
}
*f, err = FromString(s)
return err
}

View file

@ -5,6 +5,7 @@ import (
"github.com/nspcc-dev/neo-go/internal/testserdes" "github.com/nspcc-dev/neo-go/internal/testserdes"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
) )
func TestCallFlag_Has(t *testing.T) { func TestCallFlag_Has(t *testing.T) {
@ -75,3 +76,17 @@ func TestMarshalUnmarshalJSON(t *testing.T) {
require.Error(t, err) require.Error(t, err)
require.Equal(t, forig, f) require.Equal(t, forig, f)
} }
func TestMarshalUnmarshalYAML(t *testing.T) {
for _, expected := range []CallFlag{States, States | AllowNotify} {
data, err := yaml.Marshal(expected)
require.NoError(t, err)
var f CallFlag
require.NoError(t, yaml.Unmarshal(data, &f))
require.Equal(t, expected, f)
}
var f CallFlag
require.Error(t, yaml.Unmarshal([]byte(`[]`), &f))
}