diff --git a/internal/unmarshal/unmarshal.go b/internal/unmarshal/unmarshal.go new file mode 100644 index 0000000..b54065a --- /dev/null +++ b/internal/unmarshal/unmarshal.go @@ -0,0 +1,32 @@ +package unmarshal + +import ( + "fmt" +) + +// ErrUnexpectedType is returned when a field contains an unexpected type. +type ErrUnexpectedType struct { + t interface{} +} + +// Error implements the error interface. +func (e *ErrUnexpectedType) Error() string { + return fmt.Sprintf("Unexpected field type: %T", e.t) +} + +// GetIntStringField uses type asserstion to attempt to determine if the +// field argument is a string or a int. Returns a string or an int or an error +// if neither. +func GetIntStringField(field interface{}) (string, int, error) { + stringField, ok := field.(string) + if ok { + return stringField, -1, nil + } + + floatField, ok := field.(float64) + if !ok { + return "", -1, &ErrUnexpectedType{t: field} + } + + return "", int(floatField), nil +} diff --git a/mailbox.go b/mailbox.go index 33a8a3d..5db5269 100644 --- a/mailbox.go +++ b/mailbox.go @@ -11,6 +11,7 @@ import ( "time" "github.com/engvik/sbanken-go/internal/transport" + "github.com/engvik/sbanken-go/internal/unmarshal" ) var ( @@ -55,24 +56,8 @@ func (m *MailboxMessage) UnmarshalJSON(data []byte) error { return err } - // getField returns either the string value or int value. Returns an error - // if the argument passed is neither. - getField := func(field interface{}) (string, int, error) { - stringField, ok := field.(string) - if ok { - return stringField, -1, nil - } - - floatField, ok := field.(float64) - if !ok { - return "", -1, ErrMailboxMessageUnmarshal - } - - return "", int(floatField), nil - } - // Get and set proper category value. - categoryString, categoryInt, err := getField(tmpMailboxMessage.Category) + categoryString, categoryInt, err := unmarshal.GetIntStringField(tmpMailboxMessage.Category) if err != nil { return err } @@ -90,7 +75,7 @@ func (m *MailboxMessage) UnmarshalJSON(data []byte) error { } // Get and set proper status value. - statusString, statusInt, err := getField(tmpMailboxMessage.Status) + statusString, statusInt, err := unmarshal.GetIntStringField(tmpMailboxMessage.Status) if err != nil { return err } diff --git a/transactions.go b/transactions.go index 926d7fd..95076ef 100644 --- a/transactions.go +++ b/transactions.go @@ -10,6 +10,7 @@ import ( "time" "github.com/engvik/sbanken-go/internal/transport" + "github.com/engvik/sbanken-go/internal/unmarshal" ) var ( @@ -68,24 +69,8 @@ func (t *Transaction) UnmarshalJSON(data []byte) error { return err } - // getField returns either the string value or int value. Returns an error - // if the argument passed is neither. - getField := func(field interface{}) (string, int, error) { - stringField, ok := field.(string) - if ok { - return stringField, -1, nil - } - - floatField, ok := field.(float64) - if !ok { - return "", -1, ErrTransactionUnmarshal - } - - return "", int(floatField), nil - } - // Get and set proper source value. - sourceString, sourceInt, err := getField(tmpTransaction.Source) + sourceString, sourceInt, err := unmarshal.GetIntStringField(tmpTransaction.Source) if err != nil { return err }