1
0
mirror of synced 2024-11-25 14:36:03 +03:00

make originator unmarshalable (#77)

This commit is contained in:
Pavel 2021-12-01 14:50:32 +03:00 committed by GitHub
parent 97bec2e80f
commit e870447e75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 0 deletions

View File

@ -521,6 +521,20 @@ func (o Originator) MarshalText() ([]byte, error) {
return nil, ErrInvalidOriginator return nil, ErrInvalidOriginator
} }
// UnmarshalText unmarshals originator from text to the value.
func (o *Originator) UnmarshalText(text []byte) error {
switch string(text) {
case "customer":
*o = OriginatorCustomer
return nil
case "channel":
*o = OriginatorChannel
return nil
}
return ErrInvalidOriginator
}
type TransportErrorCode string type TransportErrorCode string
const ( const (

View File

@ -98,6 +98,37 @@ func TestOriginator(t *testing.T) {
}) })
t.Run("MarshalJSON_Invalid", OriginatorMarshalJSONInvalid) t.Run("MarshalJSON_Invalid", OriginatorMarshalJSONInvalid)
t.Run("UnmarshalText_Valid", func(t *testing.T) {
cases := []struct {
value Originator
text []byte
}{
{1, []byte("customer")},
{2, []byte("channel")},
}
for _, c := range cases {
var o Originator
err := o.UnmarshalText(c.text)
assert.NoError(t, err)
assert.Equal(t, c.value, o)
}
})
t.Run("UnmarshalText_Invalid", func(t *testing.T) {
var o Originator
err := o.UnmarshalText([]byte{})
assert.Empty(t, o)
assert.Equal(t, err, ErrInvalidOriginator)
})
t.Run("UnmarshalJSON_Invalid", func(t *testing.T) {
var o Originator
err := json.Unmarshal([]byte("\"unknown\""), &o)
assert.Empty(t, o)
assert.Error(t, err)
assert.Equal(t, ErrInvalidOriginator, err)
})
} }
func TestTransportErrorResponse(t *testing.T) { func TestTransportErrorResponse(t *testing.T) {