Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add NewStringBool #66

Merged
merged 3 commits into from
Aug 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions boolean.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ import (
"strings"
)

// stringBool allows us to unmarhsal Adyen Boolean values
// StringBool allows us to unmarhsal Adyen Boolean values
// which appear as strings instead of bools.
type stringBool bool
type StringBool bool

func (b *stringBool) UnmarshalJSON(data []byte) (err error) {
// NewStringBool returns an instance of StringBool representing a given bool
func NewStringBool(b bool) *StringBool {
sb := StringBool(b)
return &sb
}

// UnmarshalJSON unmarshalls to a StringBool from a slice of bytes
func (b *StringBool) UnmarshalJSON(data []byte) (err error) {
str := strings.TrimFunc(strings.ToLower(string(data)), func(c rune) bool {
return c == ' ' || c == '"'
})
Expand All @@ -19,11 +26,12 @@ func (b *stringBool) UnmarshalJSON(data []byte) (err error) {
return
}

*b = stringBool(parsed)
*b = StringBool(parsed)
return
}

func (b stringBool) MarshalJSON() ([]byte, error) {
// MarshalJSON marshalls a StringBool to a slice of bytes
func (b StringBool) MarshalJSON() ([]byte, error) {
boolResult := bool(b)
var boolString string

Expand Down
14 changes: 7 additions & 7 deletions boolean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
)

type thing struct {
Bool stringBool `json:"value"`
Bool StringBool `json:"value"`
}

type thingWithEmpty struct {
Bool *stringBool `json:"value,omitempty"`
Bool *StringBool `json:"value,omitempty"`
}

func TestStringBool_Unmarshal(t *testing.T) {
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestStringBool_Unmarshal(t *testing.T) {
t.Fatalf("expected error but didn't get one")
}

if stringBool(c.exp) != th.Bool {
if StringBool(c.exp) != th.Bool {
t.Fatalf("my exp: %v but got %v", c.exp, th.Bool)
}
})
Expand All @@ -99,12 +99,12 @@ func TestStringBool_Marshal(t *testing.T) {
}{
{
name: "true",
object: thing{Bool: stringBool(true)},
object: thing{Bool: StringBool(true)},
expected: `{"value":"true"}`,
},
{
name: "false",
object: thing{Bool: stringBool(false)},
object: thing{Bool: StringBool(false)},
expected: `{"value":"false"}`,
},
}
Expand All @@ -122,8 +122,8 @@ func TestStringBool_Marshal(t *testing.T) {
}

func TestStringBool_MarshalWithOmitempty(t *testing.T) {
valueTrue := stringBool(true)
valueFalse := stringBool(false)
valueTrue := StringBool(true)
valueFalse := StringBool(false)

cases := []struct {
name string
Expand Down
4 changes: 2 additions & 2 deletions notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "time"
//
// Link - https://docs.adyen.com/developers/api-reference/notifications-api#notificationrequest
type NotificationRequest struct {
Live stringBool `json:"live"`
Live StringBool `json:"live"`
NotificationItems []NotificationRequestItem `json:"notificationItems"`
}

Expand Down Expand Up @@ -40,5 +40,5 @@ type NotificationRequestItemData struct {
OriginalReference string `json:"originalReference,omitempty"`
PaymentMethod string `json:"paymentMethod"`
Reason string `json:"reason,omitempty"`
Success stringBool `json:"success"`
Success StringBool `json:"success"`
}
2 changes: 1 addition & 1 deletion payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type AdditionalData struct {
Content string `json:"card.encrypted.json,omitempty"`
AliasType string `json:"aliasType,omitempty"`
Alias string `json:"alias,omitempty"`
ExecuteThreeD *stringBool `json:"executeThreeD,omitempty"`
ExecuteThreeD *StringBool `json:"executeThreeD,omitempty"`
}

// BrowserInfo hold information on the user browser
Expand Down