Skip to content

Commit

Permalink
data: Add NilAt method to Field
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebrandt committed Jan 28, 2025
1 parent 73f56c7 commit ff2db34
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 0 deletions.
9 changes: 9 additions & 0 deletions data/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ func (f *Field) At(idx int) interface{} {
return f.vector.At(idx)
}

// NilAt returns true if the element at index idx of the Field is nil.
// This is useful since the interface returned by At() will not be nil
// even if the underlying element is nil (without an type assertion).
// It will always return false if the Field is not nullable.
// It can panic if idx is out of range.
func (f *Field) NilAt(idx int) bool {
return f.vector.NilAt(idx)
}

// Len returns the number of elements in the Field.
// It will return 0 if the field is nil.
func (f *Field) Len() int {
Expand Down
13 changes: 13 additions & 0 deletions data/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ func TestFieldLen(t *testing.T) {
require.Equal(t, 0, f.Len())
}

func TestFieldNilAt(t *testing.T) {
f := data.NewField("value", nil, []*float64{nil, float64Ptr(1)})

require.True(t, f.NilAt(0))
require.False(t, f.At(0) == nil) // Why we have NilAt()
require.False(t, f.NilAt(1))

f = data.NewField("value", nil, []string{"", "foo"})
require.False(t, f.NilAt(1))
require.False(t, f.NilAt(2))
require.False(t, f.NilAt(77))
}

func TestField_String(t *testing.T) {
field := data.NewField("value", nil, make([]*string, 3))

Expand Down
8 changes: 8 additions & 0 deletions data/field_type_enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func (v *enumVector) At(i int) interface{} {
return (*v)[i]
}

func (v *enumVector) NilAt(i int) bool {

Check failure on line 39 in data/field_type_enum.go

View workflow job for this annotation

GitHub Actions / Lint, Build, and Test

unused-parameter: parameter 'i' seems to be unused, consider removing or renaming it as _ (revive)
return false
}

func (v *enumVector) PointerAt(i int) interface{} {
return &(*v)[i]
}
Expand Down Expand Up @@ -115,6 +119,10 @@ func (v *nullableEnumVector) At(i int) interface{} {
return (*v)[i]
}

func (v *nullableEnumVector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableEnumVector) CopyAt(i int) interface{} {
if (*v)[i] == nil {
var g *EnumItemIndex
Expand Down
4 changes: 4 additions & 0 deletions data/generic_nullable_vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func (v *nullablegenVector) Append(i interface{}) {
*v = append(*v, i.(*gen))
}

func (v *nullablegenVector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullablegenVector) At(i int) interface{} {
return (*v)[i]
}
Expand Down
4 changes: 4 additions & 0 deletions data/generic_vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func (v *genVector) Append(i interface{}) {
*v = append(*v, i.(gen))
}

func (v *genVector) NilAt(i int) bool {
return false
}

func (v *genVector) At(i int) interface{} {
return (*v)[i]
}
Expand Down
56 changes: 56 additions & 0 deletions data/nullable_vector.gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func (v *nullableUint8Vector) Append(i interface{}) {
*v = append(*v, i.(*uint8))
}

func (v *nullableUint8Vector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableUint8Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -134,6 +138,10 @@ func (v *nullableUint16Vector) Append(i interface{}) {
*v = append(*v, i.(*uint16))
}

func (v *nullableUint16Vector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableUint16Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -225,6 +233,10 @@ func (v *nullableUint32Vector) Append(i interface{}) {
*v = append(*v, i.(*uint32))
}

func (v *nullableUint32Vector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableUint32Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -316,6 +328,10 @@ func (v *nullableUint64Vector) Append(i interface{}) {
*v = append(*v, i.(*uint64))
}

func (v *nullableUint64Vector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableUint64Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -407,6 +423,10 @@ func (v *nullableInt8Vector) Append(i interface{}) {
*v = append(*v, i.(*int8))
}

func (v *nullableInt8Vector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableInt8Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -498,6 +518,10 @@ func (v *nullableInt16Vector) Append(i interface{}) {
*v = append(*v, i.(*int16))
}

func (v *nullableInt16Vector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableInt16Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -589,6 +613,10 @@ func (v *nullableInt32Vector) Append(i interface{}) {
*v = append(*v, i.(*int32))
}

func (v *nullableInt32Vector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableInt32Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -680,6 +708,10 @@ func (v *nullableInt64Vector) Append(i interface{}) {
*v = append(*v, i.(*int64))
}

func (v *nullableInt64Vector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableInt64Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -771,6 +803,10 @@ func (v *nullableFloat32Vector) Append(i interface{}) {
*v = append(*v, i.(*float32))
}

func (v *nullableFloat32Vector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableFloat32Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -862,6 +898,10 @@ func (v *nullableFloat64Vector) Append(i interface{}) {
*v = append(*v, i.(*float64))
}

func (v *nullableFloat64Vector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableFloat64Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -953,6 +993,10 @@ func (v *nullableStringVector) Append(i interface{}) {
*v = append(*v, i.(*string))
}

func (v *nullableStringVector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableStringVector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -1044,6 +1088,10 @@ func (v *nullableBoolVector) Append(i interface{}) {
*v = append(*v, i.(*bool))
}

func (v *nullableBoolVector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableBoolVector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -1135,6 +1183,10 @@ func (v *nullableTimeTimeVector) Append(i interface{}) {
*v = append(*v, i.(*time.Time))
}

func (v *nullableTimeTimeVector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableTimeTimeVector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -1226,6 +1278,10 @@ func (v *nullableJsonRawMessageVector) Append(i interface{}) {
*v = append(*v, i.(*json.RawMessage))
}

func (v *nullableJsonRawMessageVector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableJsonRawMessageVector) At(i int) interface{} {
return (*v)[i]
}
Expand Down
56 changes: 56 additions & 0 deletions data/vector.gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func (v *uint8Vector) Append(i interface{}) {
*v = append(*v, i.(uint8))
}

func (v *uint8Vector) NilAt(i int) bool {
return false
}

func (v *uint8Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -106,6 +110,10 @@ func (v *uint16Vector) Append(i interface{}) {
*v = append(*v, i.(uint16))
}

func (v *uint16Vector) NilAt(i int) bool {
return false
}

func (v *uint16Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -178,6 +186,10 @@ func (v *uint32Vector) Append(i interface{}) {
*v = append(*v, i.(uint32))
}

func (v *uint32Vector) NilAt(i int) bool {
return false
}

func (v *uint32Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -250,6 +262,10 @@ func (v *uint64Vector) Append(i interface{}) {
*v = append(*v, i.(uint64))
}

func (v *uint64Vector) NilAt(i int) bool {
return false
}

func (v *uint64Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -322,6 +338,10 @@ func (v *int8Vector) Append(i interface{}) {
*v = append(*v, i.(int8))
}

func (v *int8Vector) NilAt(i int) bool {
return false
}

func (v *int8Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -394,6 +414,10 @@ func (v *int16Vector) Append(i interface{}) {
*v = append(*v, i.(int16))
}

func (v *int16Vector) NilAt(i int) bool {
return false
}

func (v *int16Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -466,6 +490,10 @@ func (v *int32Vector) Append(i interface{}) {
*v = append(*v, i.(int32))
}

func (v *int32Vector) NilAt(i int) bool {
return false
}

func (v *int32Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -538,6 +566,10 @@ func (v *int64Vector) Append(i interface{}) {
*v = append(*v, i.(int64))
}

func (v *int64Vector) NilAt(i int) bool {
return false
}

func (v *int64Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -610,6 +642,10 @@ func (v *float32Vector) Append(i interface{}) {
*v = append(*v, i.(float32))
}

func (v *float32Vector) NilAt(i int) bool {
return false
}

func (v *float32Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -682,6 +718,10 @@ func (v *float64Vector) Append(i interface{}) {
*v = append(*v, i.(float64))
}

func (v *float64Vector) NilAt(i int) bool {
return false
}

func (v *float64Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -754,6 +794,10 @@ func (v *stringVector) Append(i interface{}) {
*v = append(*v, i.(string))
}

func (v *stringVector) NilAt(i int) bool {
return false
}

func (v *stringVector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -826,6 +870,10 @@ func (v *boolVector) Append(i interface{}) {
*v = append(*v, i.(bool))
}

func (v *boolVector) NilAt(i int) bool {
return false
}

func (v *boolVector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -898,6 +946,10 @@ func (v *timeTimeVector) Append(i interface{}) {
*v = append(*v, i.(time.Time))
}

func (v *timeTimeVector) NilAt(i int) bool {
return false
}

func (v *timeTimeVector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -970,6 +1022,10 @@ func (v *jsonRawMessageVector) Append(i interface{}) {
*v = append(*v, i.(json.RawMessage))
}

func (v *jsonRawMessageVector) NilAt(i int) bool {
return false
}

func (v *jsonRawMessageVector) At(i int) interface{} {
return (*v)[i]
}
Expand Down
1 change: 1 addition & 0 deletions data/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type vector interface {
Append(i interface{})
Extend(i int)
At(i int) interface{}
NilAt(i int) bool
Len() int
Type() FieldType
PointerAt(i int) interface{}
Expand Down

0 comments on commit ff2db34

Please sign in to comment.