Skip to content

Commit

Permalink
refactor(pg): store empty map as {} not null
Browse files Browse the repository at this point in the history
  • Loading branch information
janisz committed Jul 24, 2023
1 parent 28823ac commit baad7ef
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion central/cluster/store/cluster/postgres/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions central/deployment/store/postgres/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions central/namespace/store/postgres/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions central/rbac/k8srole/internal/store/postgres/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions central/rbac/k8srolebinding/internal/store/postgres/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions central/serviceaccount/internal/store/postgres/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions pkg/postgres/pgutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ func NilOrUUID(value string) *uuid.UUID {
return &id
}

// EmptyOrMap allows for map to be stored explicit as an empty object ({}) rather than null.
func EmptyOrMap[K comparable, V any, M map[K]V](m M) interface{} {
if m == nil {
return make(M)
}
return m
}

// CreateTableFromModel executes input create statement using the input connection.
func CreateTableFromModel(ctx context.Context, db *gorm.DB, createStmt *postgres.CreateStmts) {
// Partitioned tables are not supported by Gorm migration or models
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tools/generate-helpers/pg-table-bindings/store.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ func {{ template "insertFunctionName" $schema }}({{ if eq (len $schema.Children)
pgutils.NilOrTime({{$field.Getter "obj"}}),
{{- else if eq $field.SQLType "uuid" }}
pgutils.NilOrUUID({{$field.Getter "obj"}}),
{{- else if eq $field.DataType "map" }}
pgutils.EmptyOrMap({{$field.Getter "obj"}}),
{{- else }}
{{$field.Getter "obj"}},{{end}}
{{- end}}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ func (s *TestSingleKeyStructsStoreSuite) TestStoreNilMap() {
row := s.testDB.QueryRow(ctx, "select labels from test_single_key_structs")
err := row.Scan(&val)
s.NoError(err)
s.Equal("null", val)
s.Equal("{}", val)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit baad7ef

Please sign in to comment.