From f36c5cfe47e6f53a0555ad581762c3c19126c11e Mon Sep 17 00:00:00 2001 From: Muhammad Surya Date: Tue, 5 Mar 2019 14:06:59 +0700 Subject: [PATCH] fix empty values when casting other changeset (#53) --- changeset/cast.go | 5 ++--- changeset/changeset.go | 1 + 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/changeset/cast.go b/changeset/cast.go index 521c4ee..ad2f91f 100644 --- a/changeset/cast.go +++ b/changeset/cast.go @@ -21,7 +21,6 @@ func Cast(data interface{}, params params.Params, fields []string, opts ...Optio options.apply(opts) var ch *Changeset - var zero bool if existingCh, ok := data.(Changeset); ok { ch = &existingCh } else if existingCh, ok := data.(*Changeset); ok { @@ -30,7 +29,7 @@ func Cast(data interface{}, params params.Params, fields []string, opts ...Optio ch = &Changeset{} ch.params = params ch.changes = make(map[string]interface{}) - ch.values, ch.types, zero = mapSchema(data) + ch.values, ch.types, ch.zero = mapSchema(data) } for _, field := range fields { @@ -48,7 +47,7 @@ func Cast(data interface{}, params params.Params, fields []string, opts ...Optio if change, valid := params.GetWithType(field, typ); valid { value, vexist := ch.values[field] - if (typ.Kind() == reflect.Slice || typ.Kind() == reflect.Array) || (zero && change != nil) || (!vexist && change != nil) || (vexist && value != change) { + if (typ.Kind() == reflect.Slice || typ.Kind() == reflect.Array) || (ch.zero && change != nil) || (!vexist && change != nil) || (vexist && value != change) { ch.changes[field] = change } } else { diff --git a/changeset/changeset.go b/changeset/changeset.go index a69c723..486ae97 100644 --- a/changeset/changeset.go +++ b/changeset/changeset.go @@ -15,6 +15,7 @@ type Changeset struct { values map[string]interface{} types map[string]reflect.Type constraints Constraints + zero bool } // Errors of changeset.