Skip to content
This repository has been archived by the owner on Feb 2, 2024. It is now read-only.

Commit

Permalink
refactor: custom unmarshal
Browse files Browse the repository at this point in the history
  • Loading branch information
engvik committed Apr 4, 2022
1 parent caa3910 commit f2bb503
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 35 deletions.
32 changes: 32 additions & 0 deletions internal/unmarshal/unmarshal.go
Original file line number Diff line number Diff line change
@@ -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
}
21 changes: 3 additions & 18 deletions mailbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/engvik/sbanken-go/internal/transport"
"github.com/engvik/sbanken-go/internal/unmarshal"
)

var (
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
19 changes: 2 additions & 17 deletions transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/engvik/sbanken-go/internal/transport"
"github.com/engvik/sbanken-go/internal/unmarshal"
)

var (
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit f2bb503

Please sign in to comment.