From bb2f11283d1ad0f21bd15bc890782bcad156ac50 Mon Sep 17 00:00:00 2001 From: Marco Ollivier Date: Mon, 20 May 2024 05:54:59 -0300 Subject: [PATCH 1/2] Add NewBool function to convert regular bool to hubspot boolean --- type.go | 6 ++++++ type_test.go | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/type.go b/type.go index c7df15b..a13c1b9 100644 --- a/type.go +++ b/type.go @@ -33,6 +33,12 @@ func (hs *HsStr) String() string { // HsBool is defined to marshal the HubSpot boolean fields of `true`, `"true"`, and so on, into a bool type. type HsBool bool +// NewBoolean returns pointer HsBool(bool) +func NewBoolean(b bool) *HsBool { + v := HsBool(b) + return &v +} + // UnmarshalJSON implemented json.Unmarshaler. // This is because there are cases where the Time value returned by HubSpot is null or "true" / "false". func (hb *HsBool) UnmarshalJSON(b []byte) error { diff --git a/type_test.go b/type_test.go index 20a87c0..5bd1a44 100644 --- a/type_test.go +++ b/type_test.go @@ -39,6 +39,26 @@ func TestHsStr_String(t *testing.T) { } } +func TestHsBool_Boolean(t *testing.T) { + tests := []struct { + input bool + expected bool + }{ + {true, true}, + {false, false}, + } + + for _, test := range tests { + result := hubspot.NewBoolean(test.input) + if result == nil { + t.Errorf("NewBoolean(%v) = nil; want *HsBool with value %v", test.input, test.expected) + } + if *result != hubspot.HsBool(test.expected) { + t.Errorf("NewBoolean(%v) = %v; want %v", test.input, *result, test.expected) + } + } +} + var testDate = time.Date(2022, time.February, 28, 0, 0, 0, 0, time.UTC) func TestHsTime_String(t *testing.T) { From a08b968ef5396f11bc4b52d0883dabf47ddf389b Mon Sep 17 00:00:00 2001 From: Marco Ollivier Date: Mon, 20 May 2024 06:05:48 -0300 Subject: [PATCH 2/2] Remove impossible case to return nill --- type_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/type_test.go b/type_test.go index 5bd1a44..9ae9e76 100644 --- a/type_test.go +++ b/type_test.go @@ -50,9 +50,6 @@ func TestHsBool_Boolean(t *testing.T) { for _, test := range tests { result := hubspot.NewBoolean(test.input) - if result == nil { - t.Errorf("NewBoolean(%v) = nil; want *HsBool with value %v", test.input, test.expected) - } if *result != hubspot.HsBool(test.expected) { t.Errorf("NewBoolean(%v) = %v; want %v", test.input, *result, test.expected) }