Ensure arrays and objects are always set

This commit is contained in:
Tobie Morgan Hitchcock 2018-04-27 15:23:58 +01:00
parent ec6a44f2d6
commit 14c0d93635
2 changed files with 35 additions and 0 deletions

View file

@ -295,6 +295,10 @@ func (d *document) mrgFld(ctx context.Context, met method) (err error) {
var old = d.initial.Get(key).Data() var old = d.initial.Get(key).Data()
// Ensure object and arrays are set
val = conv.MustBe(fd.Type, val)
// Ensure the field is the correct type // Ensure the field is the correct type
if val != nil { if val != nil {

View file

@ -53,6 +53,37 @@ func toBoolean(str string) (bool, error) {
// -------------------------------------------------- // --------------------------------------------------
func MustBe(t, obj interface{}) (val interface{}) {
switch t {
default:
return obj
case "array":
return MustBeArray(obj)
case "object":
return MustBeObject(obj)
}
}
func MustBeArray(obj interface{}) (val interface{}) {
if now, ok := obj.([]interface{}); ok {
val = now
} else {
val = make([]interface{}, 0)
}
return
}
func MustBeObject(obj interface{}) (val interface{}) {
if now, ok := obj.(map[string]interface{}); ok {
val = now
} else {
val = make(map[string]interface{})
}
return
}
// --------------------------------------------------
func MightBe(obj interface{}) (val interface{}, ok bool) { func MightBe(obj interface{}) (val interface{}, ok bool) {
switch now := obj.(type) { switch now := obj.(type) {
case string: case string: