diff --git a/cmd/icingadb-migrate/convert.go b/cmd/icingadb-migrate/convert.go index e14746e46..64a6b3498 100644 --- a/cmd/icingadb-migrate/convert.go +++ b/cmd/icingadb-migrate/convert.go @@ -150,19 +150,9 @@ func convertCommentRows( }, AckHistoryUpserter: history.AckHistoryUpserter{ClearTime: clearTime}, SetTime: setTime, - Author: icingadbTypes.String{ - NullString: sql.NullString{ - String: row.AuthorName, - Valid: true, - }, - }, - Comment: icingadbTypes.String{ - NullString: sql.NullString{ - String: row.CommentData, - Valid: true, - }, - }, - ExpireTime: convertTime(row.ExpirationTime, 0), + Author: icingadbTypes.MakeString(row.AuthorName), + Comment: icingadbTypes.MakeString(row.CommentData), + ExpireTime: convertTime(row.ExpirationTime, 0), IsPersistent: icingadbTypes.Bool{ Bool: row.IsPersistent != 0, Valid: true, @@ -656,13 +646,8 @@ func convertNotificationRows( SendTime: ts, State: row.State, PreviousHardState: previousHardState, - Text: icingadbTypes.String{ - NullString: sql.NullString{ - String: text, - Valid: true, - }, - }, - UsersNotified: row.ContactsNotified, + Text: icingadbTypes.MakeString(text), + UsersNotified: row.ContactsNotified, }) allHistory = append(allHistory, &history.HistoryNotification{ diff --git a/pkg/flatten/flatten.go b/pkg/flatten/flatten.go index 94a6e7ebc..698eff178 100644 --- a/pkg/flatten/flatten.go +++ b/pkg/flatten/flatten.go @@ -1,7 +1,6 @@ package flatten import ( - "database/sql" "fmt" "github.com/icinga/icingadb/pkg/types" "strconv" @@ -32,12 +31,12 @@ func Flatten(value interface{}, prefix string) map[string]types.String { for i, v := range value { flatten(key+"["+strconv.Itoa(i)+"]", v) } + case nil: + flattened[key] = types.MakeString("null") + case float64: + flattened[key] = types.MakeString(strconv.FormatFloat(value, 'f', -1, 64)) default: - val := "null" - if value != nil { - val = fmt.Sprintf("%v", value) - } - flattened[key] = types.String{NullString: sql.NullString{String: val, Valid: true}} + flattened[key] = types.MakeString(fmt.Sprintf("%v", value)) } } diff --git a/pkg/flatten/flatten_test.go b/pkg/flatten/flatten_test.go new file mode 100644 index 000000000..f84b8d9ec --- /dev/null +++ b/pkg/flatten/flatten_test.go @@ -0,0 +1,45 @@ +package flatten + +import ( + "github.com/icinga/icingadb/pkg/types" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestFlatten(t *testing.T) { + for _, st := range []struct { + name string + prefix string + value any + output map[string]types.String + }{ + {"nil", "a", nil, map[string]types.String{"a": types.MakeString("null")}}, + {"bool", "b", true, map[string]types.String{"b": types.MakeString("true")}}, + {"int", "c", 42, map[string]types.String{"c": types.MakeString("42")}}, + {"float", "d", 77.7, map[string]types.String{"d": types.MakeString("77.7")}}, + {"large_float", "e", 1e23, map[string]types.String{"e": types.MakeString("100000000000000000000000")}}, + {"string", "f", "\x00", map[string]types.String{"f": types.MakeString("\x00")}}, + {"nil_slice", "g", []any(nil), map[string]types.String{"g": {}}}, + {"empty_slice", "h", []any{}, map[string]types.String{"h": {}}}, + {"slice", "i", []any{nil}, map[string]types.String{"i[0]": types.MakeString("null")}}, + {"nil_map", "j", map[string]any(nil), map[string]types.String{"j": {}}}, + {"empty_map", "k", map[string]any{}, map[string]types.String{"k": {}}}, + {"map", "l", map[string]any{" ": nil}, map[string]types.String{"l. ": types.MakeString("null")}}, + {"map_with_slice", "m", map[string]any{"\t": []any{"ä", "ö", "ü"}, "ß": "s"}, map[string]types.String{ + "m.\t[0]": types.MakeString("ä"), + "m.\t[1]": types.MakeString("ö"), + "m.\t[2]": types.MakeString("ü"), + "m.ß": types.MakeString("s"), + }}, + {"slice_with_map", "n", []any{map[string]any{"ä": "a", "ö": "o", "ü": "u"}, "ß"}, map[string]types.String{ + "n[0].ä": types.MakeString("a"), + "n[0].ö": types.MakeString("o"), + "n[0].ü": types.MakeString("u"), + "n[1]": types.MakeString("ß"), + }}, + } { + t.Run(st.name, func(t *testing.T) { + assert.Equal(t, st.output, Flatten(st.value, st.prefix)) + }) + } +} diff --git a/pkg/icingadb/ha.go b/pkg/icingadb/ha.go index 74d3b3234..de31f773f 100644 --- a/pkg/icingadb/ha.go +++ b/pkg/icingadb/ha.go @@ -186,12 +186,7 @@ func (h *HA) controller() { EntityWithoutChecksum: v1.EntityWithoutChecksum{IdMeta: v1.IdMeta{ Id: envId, }}, - Name: types.String{ - NullString: sql.NullString{ - String: envId.String(), - Valid: true, - }, - }, + Name: types.MakeString(envId.String()), } h.environmentMu.Unlock() } diff --git a/pkg/types/string.go b/pkg/types/string.go index f8ead450c..ce2a4ac69 100644 --- a/pkg/types/string.go +++ b/pkg/types/string.go @@ -15,6 +15,14 @@ type String struct { sql.NullString } +// MakeString constructs a new non-NULL String from s. +func MakeString(s string) String { + return String{sql.NullString{ + String: s, + Valid: true, + }} +} + // MarshalJSON implements the json.Marshaler interface. // Supports JSON null. func (s String) MarshalJSON() ([]byte, error) {