Skip to content

Commit

Permalink
Add NewBool function to convert regular bool to hubspot boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
marcopollivier committed May 20, 2024
1 parent d62f156 commit bb2f112
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Check failure on line 53 in type_test.go

View workflow job for this annotation

GitHub Actions / test

SA5011(related information): this check suggests that the pointer can be nil (staticcheck)
t.Errorf("NewBoolean(%v) = nil; want *HsBool with value %v", test.input, test.expected)
}
if *result != hubspot.HsBool(test.expected) {

Check failure on line 56 in type_test.go

View workflow job for this annotation

GitHub Actions / test

SA5011: possible nil pointer dereference (staticcheck)
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) {
Expand Down

0 comments on commit bb2f112

Please sign in to comment.