Skip to content

Commit e543ee2

Browse files
committed
database.BuildInsertStmtWithout: Variadic column exclusion
Doing so allows multiple columns to be excluded without breaking the API compatibility. Recently added slices functions even shortens the code.
1 parent df503af commit e543ee2

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

database/utils.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/icinga/icinga-go-library/types"
1111
"github.com/jmoiron/sqlx"
1212
"github.com/pkg/errors"
13+
"slices"
1314
"strings"
1415
)
1516

@@ -82,15 +83,11 @@ func InsertObtainID(ctx context.Context, conn TxOrDB, stmt string, arg any) (int
8283
return resultID, nil
8384
}
8485

85-
// BuildInsertStmtWithout builds an insert stmt without the provided column.
86-
func BuildInsertStmtWithout(db *DB, into interface{}, withoutColumn string) string {
87-
columns := db.BuildColumns(into)
88-
for i, column := range columns {
89-
if column == withoutColumn {
90-
columns = append(columns[:i], columns[i+1:]...)
91-
break
92-
}
93-
}
86+
// BuildInsertStmtWithout builds an insert stmt without the provided columns.
87+
func BuildInsertStmtWithout(db *DB, into interface{}, withoutColumns ...string) string {
88+
columns := slices.DeleteFunc(
89+
db.BuildColumns(into),
90+
func(column string) bool { return slices.Contains(withoutColumns, column) })
9491

9592
return fmt.Sprintf(
9693
`INSERT INTO "%s" ("%s") VALUES (%s)`,

0 commit comments

Comments
 (0)