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

Flatten(): render even large numbers as-is, not using scientific notation #657

Merged
merged 5 commits into from
Mar 25, 2024
Merged
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
25 changes: 5 additions & 20 deletions cmd/icingadb-migrate/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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{
Expand Down
11 changes: 5 additions & 6 deletions pkg/flatten/flatten.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package flatten

import (
"database/sql"
"fmt"
"github.com/icinga/icingadb/pkg/types"
"strconv"
Expand Down Expand Up @@ -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))
}
}

Expand Down
45 changes: 45 additions & 0 deletions pkg/flatten/flatten_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
}
}
7 changes: 1 addition & 6 deletions pkg/icingadb/ha.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/types/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading