Ensure arrays and objects are always set
This commit is contained in:
parent
ec6a44f2d6
commit
14c0d93635
2 changed files with 35 additions and 0 deletions
|
@ -295,6 +295,10 @@ func (d *document) mrgFld(ctx context.Context, met method) (err error) {
|
|||
|
||||
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
|
||||
|
||||
if val != nil {
|
||||
|
|
|
@ -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) {
|
||||
switch now := obj.(type) {
|
||||
case string:
|
||||
|
|
Loading…
Reference in a new issue