mirror of
https://github.com/Icinga/icingadb.git
synced 2026-05-28 04:35:54 -04:00
Flatten empty custom vars of type array & map correctly
This commit is contained in:
parent
78fa223cab
commit
fa0a712bac
6 changed files with 30 additions and 14 deletions
|
|
@ -1,26 +1,43 @@
|
|||
package flatten
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/icinga/icingadb/pkg/types"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Flatten creates flat, one-dimensional maps from arbitrarily nested values, e.g. JSON.
|
||||
func Flatten(value interface{}, prefix string) map[string]interface{} {
|
||||
func Flatten(value interface{}, prefix string) map[string]types.String {
|
||||
var flatten func(string, interface{})
|
||||
flattened := make(map[string]interface{})
|
||||
flattened := make(map[string]types.String)
|
||||
|
||||
flatten = func(key string, value interface{}) {
|
||||
switch value := value.(type) {
|
||||
case map[string]interface{}:
|
||||
if len(value) == 0 {
|
||||
flattened[key] = types.String{}
|
||||
break
|
||||
}
|
||||
|
||||
for k, v := range value {
|
||||
flatten(key+"."+k, v)
|
||||
}
|
||||
case []interface{}:
|
||||
if len(value) == 0 {
|
||||
flattened[key] = types.String{}
|
||||
break
|
||||
}
|
||||
|
||||
for i, v := range value {
|
||||
flatten(key+"["+strconv.Itoa(i)+"]", v)
|
||||
}
|
||||
default:
|
||||
flattened[key] = value
|
||||
val := "null"
|
||||
if value != nil {
|
||||
val = fmt.Sprintf("%v", value)
|
||||
}
|
||||
flattened[key] = types.String{NullString: sql.NullString{String: val, Valid: true}}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ package v1
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/icinga/icingadb/internal"
|
||||
"github.com/icinga/icingadb/pkg/com"
|
||||
"github.com/icinga/icingadb/pkg/contracts"
|
||||
|
|
@ -25,7 +24,7 @@ type CustomvarFlat struct {
|
|||
CustomvarMeta `json:",inline"`
|
||||
Flatname string `json:"flatname"`
|
||||
FlatnameChecksum types.Binary `json:"flatname_checksum"`
|
||||
Flatvalue string `json:"flatvalue"`
|
||||
Flatvalue types.String `json:"flatvalue"`
|
||||
}
|
||||
|
||||
func NewCustomvar() contracts.Entity {
|
||||
|
|
@ -117,11 +116,9 @@ func flattenCustomvars(ctx context.Context, g *errgroup.Group, cvs <-chan contra
|
|||
flattened := flatten.Flatten(value, customvar.Name)
|
||||
|
||||
for flatname, flatvalue := range flattened {
|
||||
var fv string
|
||||
if flatvalue == nil {
|
||||
fv = "null"
|
||||
} else {
|
||||
fv = fmt.Sprintf("%v", flatvalue)
|
||||
var fv interface{}
|
||||
if flatvalue.Valid {
|
||||
fv = flatvalue.String
|
||||
}
|
||||
|
||||
select {
|
||||
|
|
@ -131,7 +128,7 @@ func flattenCustomvars(ctx context.Context, g *errgroup.Group, cvs <-chan contra
|
|||
IdMeta: IdMeta{
|
||||
// TODO(el): Schema comment is wrong.
|
||||
// Without customvar.Id we would produce duplicate keys here.
|
||||
Id: utils.Checksum(objectpacker.MustPackSlice(customvar.EnvironmentId, customvar.Id, flatname, flatvalue)),
|
||||
Id: utils.Checksum(objectpacker.MustPackSlice(customvar.EnvironmentId, customvar.Id, flatname, fv)),
|
||||
},
|
||||
},
|
||||
EnvironmentMeta: EnvironmentMeta{
|
||||
|
|
@ -141,7 +138,7 @@ func flattenCustomvars(ctx context.Context, g *errgroup.Group, cvs <-chan contra
|
|||
},
|
||||
Flatname: flatname,
|
||||
FlatnameChecksum: utils.Checksum(flatname),
|
||||
Flatvalue: fv,
|
||||
Flatvalue: flatvalue,
|
||||
}:
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
|
|
|
|||
|
|
@ -983,7 +983,7 @@ CREATE TABLE customvar_flat (
|
|||
flatname_checksum binary(20) NOT NULL COMMENT 'sha1(flatname after conversion)',
|
||||
|
||||
flatname varchar(512) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'Path converted with `.` and `[ ]`',
|
||||
flatvalue text NOT NULL,
|
||||
flatvalue text DEFAULT NULL,
|
||||
|
||||
PRIMARY KEY (id),
|
||||
|
||||
|
|
|
|||
1
schema/mysql/upgrades/1.2.0.sql
Normal file
1
schema/mysql/upgrades/1.2.0.sql
Normal file
|
|
@ -0,0 +1 @@
|
|||
ALTER TABLE customvar_flat MODIFY COLUMN flatvalue text DEFAULT NULL;
|
||||
|
|
@ -1553,7 +1553,7 @@ CREATE TABLE customvar_flat (
|
|||
flatname_checksum bytea20 NOT NULL,
|
||||
|
||||
flatname citext NOT NULL,
|
||||
flatvalue text NOT NULL,
|
||||
flatvalue text DEFAULT NULL,
|
||||
|
||||
CONSTRAINT pk_customvar_flat PRIMARY KEY (id)
|
||||
);
|
||||
|
|
|
|||
1
schema/pgsql/upgrades/1.2.0.sql
Normal file
1
schema/pgsql/upgrades/1.2.0.sql
Normal file
|
|
@ -0,0 +1 @@
|
|||
ALTER TABLE customvar_flat ALTER COLUMN flatvalue DROP NOT NULL;
|
||||
Loading…
Reference in a new issue