Skip to content

Commit 67cd647

Browse files
oxziyhabteab
andcommitted
types: Add Int.ZeroToNull, String.EmptyToNull
These two methods were derived from Icinga Notifications utility functions, mapping an empty string or zero to a NULL value. In order to integrate this utility functions in the IGL, they were added as builder methods to types.Int and types.String. Imported from: - ToDBInt, Icinga/icinga-notifications@f0b39f0 - ToDBString, Icinga/icinga-notifications@8183344 As a drive-by change, a MakeInt function was added similar to MakeString. Otherwise, creating types.Int instances from within Icinga Notifications was a bit ugly. Co-Authored-By: Yonas Habteab <[email protected]>
1 parent e543ee2 commit 67cd647

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

types/int.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ type Int struct {
1414
sql.NullInt64
1515
}
1616

17+
// MakeInt constructs a new non-NULL Int from i.
18+
func MakeInt(i int64) Int {
19+
return Int{sql.NullInt64{
20+
Int64: i,
21+
Valid: true,
22+
}}
23+
}
24+
1725
// MarshalJSON implements the json.Marshaler interface.
1826
// Supports JSON null.
1927
func (i Int) MarshalJSON() ([]byte, error) {
@@ -57,6 +65,14 @@ func (i *Int) UnmarshalJSON(data []byte) error {
5765
return nil
5866
}
5967

68+
// ZeroToNull either returns this Int or a NULL Int if the internal int is zero.
69+
func (i Int) ZeroToNull() Int {
70+
if i.Int64 != 0 {
71+
return i
72+
}
73+
return Int{sql.NullInt64{Valid: false}}
74+
}
75+
6076
// Assert interface compliance.
6177
var (
6278
_ json.Marshaler = Int{}

types/int_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,22 @@ func TestInt_UnmarshalJSON(t *testing.T) {
8585
})
8686
}
8787
}
88+
89+
func TestInt_ZeroToNull(t *testing.T) {
90+
subtests := []struct {
91+
name string
92+
input Int
93+
output Int
94+
}{
95+
{"nil", Int{sql.NullInt64{}}, Int{sql.NullInt64{}}},
96+
{"zero", MakeInt(0), Int{sql.NullInt64{Valid: false}}},
97+
{"non-zero", MakeInt(1), MakeInt(1)},
98+
}
99+
100+
for _, st := range subtests {
101+
t.Run(st.name, func(t *testing.T) {
102+
actual := st.input.ZeroToNull()
103+
require.Equal(t, st.output, actual)
104+
})
105+
}
106+
}

types/string.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ func (s String) Value() (driver.Value, error) {
7171
return strings.ReplaceAll(s.String, "\x00", ""), nil
7272
}
7373

74+
// EmptyToNull either returns this String or a NULL String if the internal string is empty.
75+
func (s String) EmptyToNull() String {
76+
if s.String != "" {
77+
return s
78+
}
79+
return String{sql.NullString{Valid: false}}
80+
}
81+
7482
// Assert interface compliance.
7583
var (
7684
_ json.Marshaler = String{}

types/string_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,22 @@ func TestString_Value(t *testing.T) {
124124
})
125125
}
126126
}
127+
128+
func TestString_EmptyToNull(t *testing.T) {
129+
subtests := []struct {
130+
name string
131+
input String
132+
output String
133+
}{
134+
{"nil", String{sql.NullString{}}, String{sql.NullString{}}},
135+
{"empty", MakeString(""), String{sql.NullString{Valid: false}}},
136+
{"non-empty", MakeString("foo"), String{sql.NullString{String: "foo", Valid: true}}},
137+
}
138+
139+
for _, st := range subtests {
140+
t.Run(st.name, func(t *testing.T) {
141+
actual := st.input.EmptyToNull()
142+
require.Equal(t, st.output, actual)
143+
})
144+
}
145+
}

0 commit comments

Comments
 (0)