Skip to content
This repository was archived by the owner on Nov 20, 2021. It is now read-only.

Commit 0d63a3a

Browse files
authored
Merge pull request #7 from criticalstack/e2db/update-required-field
pkg/e2db: omit zero-value required fields from Update
2 parents a546630 + f05d389 commit 0d63a3a

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

pkg/e2db/tx.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,18 @@ func (tx *Tx) Update(iface interface{}) error {
155155
if reflect.DeepEqual(f.value.Interface(), dbFieldValue.Interface()) {
156156
continue
157157
}
158+
159+
// if field is required and zero-value, it is safe to presume it was
160+
// already set
161+
if f.hasTag("required") && f.isZero() {
162+
continue
163+
}
158164
for _, tag := range f.Tags {
159165
switch tag.Name {
160166
case "index":
161167
oldIdx := key.Index(m.Name, f.Name, toString(dbFieldValue.Interface()), id)
162168
newIdx := key.Index(m.Name, f.Name, toString(f.value.Interface()), id)
163169
indexes[oldIdx] = newIdx
164-
case "required":
165-
if f.isZero() {
166-
return errors.Wrap(ErrFieldRequired, f.Name)
167-
}
168170
case "unique":
169171
oldIdx := key.Unique(m.Name, f.Name, toString(dbFieldValue.Interface()))
170172
newIdx := key.Unique(m.Name, f.Name, toString(f.value.Interface()))

0 commit comments

Comments
 (0)