Skip to content

Commit

Permalink
Merge pull request #12 from unravelin/shorthand
Browse files Browse the repository at this point in the history
Shorthand constructors
  • Loading branch information
icio authored Nov 7, 2022
2 parents 1c5d699 + 2132f03 commit 59f0b32
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func NewBool(b bool, valid bool) Bool {
}
}

// B creates a new Bool that will always be valid.
func B(b bool) Bool {
return BoolFrom(b)
}

// BoolFrom creates a new Bool that will always be valid.
func BoolFrom(b bool) Bool {
return NewBool(b, true)
Expand Down
5 changes: 5 additions & 0 deletions float.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ func NewFloat(f float64, valid bool) Float {
}
}

// F creates a new Float that will always be valid.
func F(f float64) Float {
return FloatFrom(f)
}

// FloatFrom creates a new Float that will always be valid.
func FloatFrom(f float64) Float {
return NewFloat(f, true)
Expand Down
5 changes: 5 additions & 0 deletions int.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func NewInt(i int64, valid bool) Int {
}
}

// I creates a new Int that will always be valid.
func I(i int64) Int {
return IntFrom(i)
}

// IntFrom creates a new Int that will always be valid.
func IntFrom(i int64) Int {
return NewInt(i, true)
Expand Down
10 changes: 10 additions & 0 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ type String struct {
sql.NullString
}

// S creates a new String that will never be blank.
func S(s string) String {
return StringFrom(s)
}

// ZS creates a new String that is valid if s is not zero.
func ZS(s string) String {
return NewString(s, s != "")
}

// StringFrom creates a new String that will never be blank.
func StringFrom(s string) String {
return NewString(s, true)
Expand Down
5 changes: 5 additions & 0 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ func NewTime(t time.Time, valid bool) Time {
}
}

// T creates a new Time that will always be valid.
func T(t time.Time) Time {
return TimeFrom(t)
}

// TimeFrom creates a new Time that will always be valid.
func TimeFrom(t time.Time) Time {
return NewTime(t, true)
Expand Down

0 comments on commit 59f0b32

Please sign in to comment.