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 check for zero or blank #69

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions zero/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"database/sql"
"encoding/json"
"fmt"
"strings"
)

// nullBytes is a JSON null literal
Expand Down Expand Up @@ -105,6 +106,11 @@ func (s String) IsZero() bool {
return !s.Valid || s.String == ""
}

// IsZero returns true for null empty strings or strings which consist only of blanks.
func (s String) IsZeroOrBlank() bool {
return !s.Valid || strings.ReplaceAll(s.String, " ", "") == ""
}

// Equal returns true if both strings have the same value or are both either null or empty.
func (s String) Equal(other String) bool {
return s.ValueOrZero() == other.ValueOrZero()
Expand Down
27 changes: 27 additions & 0 deletions zero/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,33 @@ func TestStringIsZero(t *testing.T) {
}
}

func TestStringIsZeroOrBlank(t *testing.T) {
str := StringFrom("test")
if str.IsZeroOrBlank() {
t.Errorf("IsZero() should be false")
}

null := StringFrom("")
if !null.IsZeroOrBlank() {
t.Errorf("IsZeroOrBlank() should be true")
}

empty := NewString("", true)
if !empty.IsZeroOrBlank() {
t.Errorf("IsZeroOrBlank() should be true")
}

nullBlank := StringFrom(" ")
if !nullBlank.IsZeroOrBlank() {
t.Errorf("IsZeroOrBlank() should be true")
}

emptyBlank := NewString(" ", true)
if !emptyBlank.IsZeroOrBlank() {
t.Errorf("IsZeroOrBlank() should be true")
}
}

func TestStringScan(t *testing.T) {
var str String
err := str.Scan("test")
Expand Down