Skip to content

Import Icinga Notifications internal/utils #127

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

Merged
merged 5 commits into from
May 8, 2025
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
15 changes: 15 additions & 0 deletions database/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/icinga/icinga-go-library/types"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
"slices"
"strings"
)

// CantPerformQuery wraps the given error with the specified query that cannot be executed.
Expand Down Expand Up @@ -81,6 +83,19 @@ func InsertObtainID(ctx context.Context, conn TxOrDB, stmt string, arg any) (int
return resultID, nil
}

// BuildInsertStmtWithout builds an insert stmt without the provided columns.
func BuildInsertStmtWithout(db *DB, into interface{}, withoutColumns ...string) string {
columns := slices.DeleteFunc(
db.BuildColumns(into),
func(column string) bool { return slices.Contains(withoutColumns, column) })

return fmt.Sprintf(
`INSERT INTO "%s" ("%s") VALUES (%s)`,
TableName(into), strings.Join(columns, `", "`),
fmt.Sprintf(":%s", strings.Join(columns, ", :")),
)
}

// unsafeSetSessionVariableIfExists sets the given MySQL/MariaDB system variable for the specified database session.
//
// NOTE: It is unsafe to use this function with untrusted/user supplied inputs and poses an SQL injection,
Expand Down
23 changes: 23 additions & 0 deletions types/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,29 @@ type Int struct {
sql.NullInt64
}

// TransformZeroIntToNull transforms a valid Int carrying a zero value to a SQL NULL.
func TransformZeroIntToNull(i *Int) {
if i.Valid && i.Int64 == 0 {
i.Valid = false
}
}

// MakeInt constructs a new Int.
//
// Multiple transformer functions can be given, each transforming the generated Int, e.g., TransformZeroIntToNull.
func MakeInt(in int64, transformers ...func(*Int)) Int {
i := Int{sql.NullInt64{
Int64: in,
Valid: true,
}}

for _, transformer := range transformers {
transformer(&i)
}

return i
}

// MarshalJSON implements the json.Marshaler interface.
// Supports JSON null.
func (i Int) MarshalJSON() ([]byte, error) {
Expand Down
43 changes: 43 additions & 0 deletions types/int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,49 @@ import (
"testing"
)

func TestMakeInt(t *testing.T) {
subtests := []struct {
name string
input int64
transformers []func(*Int)
output sql.NullInt64
}{
{
name: "zero",
input: 0,
output: sql.NullInt64{Int64: 0, Valid: true},
},
{
name: "positive",
input: 1,
output: sql.NullInt64{Int64: 1, Valid: true},
},
{
name: "negative",
input: -1,
output: sql.NullInt64{Int64: -1, Valid: true},
},
{
name: "zero-transform-zero-to-null",
input: 0,
transformers: []func(*Int){TransformZeroIntToNull},
output: sql.NullInt64{Valid: false},
},
{
name: "positive-transform-zero-to-null",
input: 1,
transformers: []func(*Int){TransformZeroIntToNull},
output: sql.NullInt64{Int64: 1, Valid: true},
},
}

for _, st := range subtests {
t.Run(st.name, func(t *testing.T) {
require.Equal(t, Int{NullInt64: st.output}, MakeInt(st.input, st.transformers...))
})
}
}

func TestInt_MarshalJSON(t *testing.T) {
subtests := []struct {
name string
Expand Down
23 changes: 19 additions & 4 deletions types/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,27 @@ 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,
// TransformEmptyStringToNull transforms a valid String carrying an empty text to a SQL NULL.
func TransformEmptyStringToNull(s *String) {
if s.Valid && s.String == "" {
s.Valid = false
}
}

// MakeString constructs a new String.
//
// Multiple transformer functions can be given, each transforming the generated String, e.g., TransformEmptyStringToNull.
func MakeString(in string, transformers ...func(*String)) String {
s := String{sql.NullString{
String: in,
Valid: true,
}}

for _, transformer := range transformers {
transformer(&s)
}

return s
}

// MarshalJSON implements the json.Marshaler interface.
Expand Down
44 changes: 37 additions & 7 deletions types/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,48 @@ import (

func TestMakeString(t *testing.T) {
subtests := []struct {
name string
io string
name string
input string
transformers []func(*String)
output sql.NullString
}{
{"empty", ""},
{"nul", "\x00"},
{"space", " "},
{"multiple", "abc"},
{
name: "empty",
input: "",
output: sql.NullString{String: "", Valid: true},
},
{
name: "nul",
input: "\x00",
output: sql.NullString{String: "\x00", Valid: true},
},
{
name: "space",
input: " ",
output: sql.NullString{String: " ", Valid: true},
},
{
name: "valid-text",
input: "abc",
output: sql.NullString{String: "abc", Valid: true},
},
{
name: "empty-transform-empty-to-null",
input: "",
transformers: []func(*String){TransformEmptyStringToNull},
output: sql.NullString{Valid: false},
},
{
name: "valid-text-transform-empty-to-null",
input: "abc",
transformers: []func(*String){TransformEmptyStringToNull},
output: sql.NullString{String: "abc", Valid: true},
},
}

for _, st := range subtests {
t.Run(st.name, func(t *testing.T) {
require.Equal(t, String{NullString: sql.NullString{String: st.io, Valid: true}}, MakeString(st.io))
require.Equal(t, String{NullString: st.output}, MakeString(st.input, st.transformers...))
})
}
}
Expand Down
23 changes: 23 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package utils

import (
"cmp"
"context"
"crypto/sha1" // #nosec G505 -- Blocklisted import crypto/sha1
"fmt"
"github.com/go-sql-driver/mysql"
"github.com/lib/pq"
"github.com/pkg/errors"
"golang.org/x/exp/utf8string"
"iter"
"net"
"os"
"path/filepath"
"slices"
"strings"
"time"
)
Expand Down Expand Up @@ -163,3 +166,23 @@ func PrintErrorThenExit(err error, exitCode int) {
fmt.Fprintln(os.Stderr, err)
os.Exit(exitCode)
}

// IterateOrderedMap implements iter.Seq2 to iterate over a map in the key's order.
//
// This function returns a func yielding key-value-pairs from a given map in the order of their keys, if their type
// is cmp.Ordered.
func IterateOrderedMap[K cmp.Ordered, V any](m map[K]V) iter.Seq2[K, V] {
keys := make([]K, 0, len(m))
for key := range m {
keys = append(keys, key)
}
slices.Sort(keys)

return func(yield func(K, V) bool) {
for _, key := range keys {
if !yield(key, m[key]) {
return
}
}
}
}
42 changes: 42 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)
Expand Down Expand Up @@ -52,3 +53,44 @@ func requireClosedEmpty(t *testing.T, ch <-chan int) {
require.Fail(t, "receiving should not block")
}
}

func TestIterateOrderedMap(t *testing.T) {
tests := []struct {
name string
in map[int]string
outKeys []int
}{
{"empty", map[int]string{}, nil},
{"single", map[int]string{1: "foo"}, []int{1}},
{"few-numbers", map[int]string{1: "a", 2: "b", 3: "c"}, []int{1, 2, 3}},
{
"1k-numbers",
func() map[int]string {
m := make(map[int]string)
for i := 0; i < 1000; i++ {
m[i] = "foo"
}
return m
}(),
func() []int {
keys := make([]int, 1000)
for i := 0; i < 1000; i++ {
keys[i] = i
}
return keys
}(),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var outKeys []int
for k, v := range IterateOrderedMap(tt.in) {
assert.Equal(t, tt.in[k], v)
outKeys = append(outKeys, k)
}

assert.Equal(t, tt.outKeys, outKeys)
})
}
}
Loading