From b4dfaf28981eb2b960367e513e8f56dbdd9671d4 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Wed, 14 Feb 2018 15:48:42 +0000 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20assert=20the=20value=20of=20a?= =?UTF-8?q?=20field=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously if a field had a type, and a value was entered which did not match that type, then an error was raised, even though the ASSERT clause was not set. Now the field will be set to nil if the field does not match the specified type, and the ASSERT clause can be used to ensure that a valid value is always entered. --- db/merge.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/db/merge.go b/db/merge.go index bd35e46a..8feef851 100644 --- a/db/merge.go +++ b/db/merge.go @@ -290,7 +290,7 @@ func (d *document) mrgFld(ctx context.Context) (err error) { for _, fd := range fds { - err = d.current.Walk(func(key string, val interface{}) (err error) { + err = d.current.Walk(func(key string, val interface{}) error { vars := data.New() @@ -299,8 +299,10 @@ func (d *document) mrgFld(ctx context.Context) (err error) { // Ensure the field is the correct type if val != nil { - if val, err = conv.ConvertTo(fd.Type, fd.Kind, val); err != nil { - val = old + if now, err := conv.ConvertTo(fd.Type, fd.Kind, val); err != nil { + val = nil + } else { + val = now } } @@ -314,8 +316,10 @@ func (d *document) mrgFld(ctx context.Context) (err error) { // We are setting the value of the field if fd.Value != nil { - if val, err = d.i.e.fetch(ctx, fd.Value, d.current); err != nil { + if now, err := d.i.e.fetch(ctx, fd.Value, d.current); err != nil { return err + } else { + val = now } } @@ -343,7 +347,7 @@ func (d *document) mrgFld(ctx context.Context) (err error) { d.current.Del(key) } - return + return nil }, fd.Name.ID)