diff options
Diffstat (limited to 'vendor/github.com/uptrace/bun/schema/field.go')
-rw-r--r-- | vendor/github.com/uptrace/bun/schema/field.go | 36 |
1 files changed, 25 insertions, 11 deletions
diff --git a/vendor/github.com/uptrace/bun/schema/field.go b/vendor/github.com/uptrace/bun/schema/field.go index 59990b924..ac6359da4 100644 --- a/vendor/github.com/uptrace/bun/schema/field.go +++ b/vendor/github.com/uptrace/bun/schema/field.go @@ -10,6 +10,7 @@ import ( type Field struct { StructField reflect.StructField + IsPtr bool Tag tagparser.Tag IndirectType reflect.Type @@ -51,15 +52,36 @@ func (f *Field) Value(strct reflect.Value) reflect.Value { return fieldByIndexAlloc(strct, f.Index) } +func (f *Field) HasNilValue(v reflect.Value) bool { + if len(f.Index) == 1 { + return v.Field(f.Index[0]).IsNil() + } + + for _, index := range f.Index { + if v.Kind() == reflect.Ptr { + if v.IsNil() { + return true + } + v = v.Elem() + } + v = v.Field(index) + } + return v.IsNil() +} + func (f *Field) HasZeroValue(v reflect.Value) bool { - for _, idx := range f.Index { + if len(f.Index) == 1 { + return f.IsZero(v.Field(f.Index[0])) + } + + for _, index := range f.Index { if v.Kind() == reflect.Ptr { if v.IsNil() { return true } v = v.Elem() } - v = v.Field(idx) + v = v.Field(index) } return f.IsZero(v) } @@ -70,7 +92,7 @@ func (f *Field) AppendValue(fmter Formatter, b []byte, strct reflect.Value) []by return dialect.AppendNull(b) } - if f.NullZero && f.IsZero(fv) { + if (f.IsPtr && fv.IsNil()) || (f.NullZero && f.IsZero(fv)) { return dialect.AppendNull(b) } if f.Append == nil { @@ -98,14 +120,6 @@ func (f *Field) ScanValue(strct reflect.Value, src interface{}) error { return f.ScanWithCheck(fv, src) } -func (f *Field) markAsPK() { - f.IsPK = true - f.NotNull = true - if !f.Tag.HasOption("allowzero") { - f.NullZero = true - } -} - func indexEqual(ind1, ind2 []int) bool { if len(ind1) != len(ind2) { return false |