Skip to content

Commit d477c83

Browse files
authored
data: Add NilAt method to Field and Frame (#1208)
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).
1 parent 69da630 commit d477c83

File tree

9 files changed

+157
-0
lines changed

9 files changed

+157
-0
lines changed

data/field.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,15 @@ func (f *Field) At(idx int) interface{} {
173173
return f.vector.At(idx)
174174
}
175175

176+
// NilAt returns true if the element at index idx of the Field is nil.
177+
// This is useful since the interface returned by At() will not be nil
178+
// even if the underlying element is nil (without an type assertion).
179+
// It will always return false if the Field is not nullable.
180+
// It can panic if idx is out of range.
181+
func (f *Field) NilAt(idx int) bool {
182+
return f.vector.NilAt(idx)
183+
}
184+
176185
// Len returns the number of elements in the Field.
177186
// It will return 0 if the field is nil.
178187
func (f *Field) Len() int {

data/field_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,19 @@ func TestFieldLen(t *testing.T) {
7979
require.Equal(t, 0, f.Len())
8080
}
8181

82+
func TestFieldNilAt(t *testing.T) {
83+
f := data.NewField("value", nil, []*float64{nil, float64Ptr(1)})
84+
85+
require.True(t, f.NilAt(0))
86+
require.False(t, f.At(0) == nil) // Why we have NilAt()
87+
require.False(t, f.NilAt(1))
88+
89+
f = data.NewField("value", nil, []string{"", "foo"})
90+
require.False(t, f.NilAt(1))
91+
require.False(t, f.NilAt(2))
92+
require.False(t, f.NilAt(77))
93+
}
94+
8295
func TestField_String(t *testing.T) {
8396
field := data.NewField("value", nil, make([]*string, 3))
8497

data/field_type_enum.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ func (v *enumVector) At(i int) interface{} {
3636
return (*v)[i]
3737
}
3838

39+
func (v *enumVector) NilAt(_ int) bool {
40+
return false
41+
}
42+
3943
func (v *enumVector) PointerAt(i int) interface{} {
4044
return &(*v)[i]
4145
}
@@ -115,6 +119,10 @@ func (v *nullableEnumVector) At(i int) interface{} {
115119
return (*v)[i]
116120
}
117121

122+
func (v *nullableEnumVector) NilAt(i int) bool {
123+
return (*v)[i] == nil
124+
}
125+
118126
func (v *nullableEnumVector) CopyAt(i int) interface{} {
119127
if (*v)[i] == nil {
120128
var g *EnumItemIndex

data/frame.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,12 @@ func (f *Frame) Rows() int {
239239
return 0
240240
}
241241

242+
// NilAt returns true if the element at fieldIdx and rowIdx is nil.
243+
// It will panic if either fieldIdx or rowIdx are out of range.
244+
func (f *Frame) NilAt(fieldIdx int, rowIdx int) bool {
245+
return f.Fields[fieldIdx].vector.NilAt(rowIdx)
246+
}
247+
242248
// At returns the value of the specified fieldIdx and rowIdx.
243249
// It will panic if either fieldIdx or rowIdx are out of range.
244250
func (f *Frame) At(fieldIdx int, rowIdx int) interface{} {

data/generic_nullable_vector.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ func (v *nullablegenVector) Append(i interface{}) {
3434
*v = append(*v, i.(*gen))
3535
}
3636

37+
func (v *nullablegenVector) NilAt(i int) bool {
38+
return (*v)[i] == nil
39+
}
40+
3741
func (v *nullablegenVector) At(i int) interface{} {
3842
return (*v)[i]
3943
}

data/generic_vector.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ func (v *genVector) Append(i interface{}) {
3131
*v = append(*v, i.(gen))
3232
}
3333

34+
func (v *genVector) NilAt(i int) bool {
35+
return false
36+
}
37+
3438
func (v *genVector) At(i int) interface{} {
3539
return (*v)[i]
3640
}

data/nullable_vector.gen.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ func (v *nullableUint8Vector) Append(i interface{}) {
4343
*v = append(*v, i.(*uint8))
4444
}
4545

46+
func (v *nullableUint8Vector) NilAt(i int) bool {
47+
return (*v)[i] == nil
48+
}
49+
4650
func (v *nullableUint8Vector) At(i int) interface{} {
4751
return (*v)[i]
4852
}
@@ -134,6 +138,10 @@ func (v *nullableUint16Vector) Append(i interface{}) {
134138
*v = append(*v, i.(*uint16))
135139
}
136140

141+
func (v *nullableUint16Vector) NilAt(i int) bool {
142+
return (*v)[i] == nil
143+
}
144+
137145
func (v *nullableUint16Vector) At(i int) interface{} {
138146
return (*v)[i]
139147
}
@@ -225,6 +233,10 @@ func (v *nullableUint32Vector) Append(i interface{}) {
225233
*v = append(*v, i.(*uint32))
226234
}
227235

236+
func (v *nullableUint32Vector) NilAt(i int) bool {
237+
return (*v)[i] == nil
238+
}
239+
228240
func (v *nullableUint32Vector) At(i int) interface{} {
229241
return (*v)[i]
230242
}
@@ -316,6 +328,10 @@ func (v *nullableUint64Vector) Append(i interface{}) {
316328
*v = append(*v, i.(*uint64))
317329
}
318330

331+
func (v *nullableUint64Vector) NilAt(i int) bool {
332+
return (*v)[i] == nil
333+
}
334+
319335
func (v *nullableUint64Vector) At(i int) interface{} {
320336
return (*v)[i]
321337
}
@@ -407,6 +423,10 @@ func (v *nullableInt8Vector) Append(i interface{}) {
407423
*v = append(*v, i.(*int8))
408424
}
409425

426+
func (v *nullableInt8Vector) NilAt(i int) bool {
427+
return (*v)[i] == nil
428+
}
429+
410430
func (v *nullableInt8Vector) At(i int) interface{} {
411431
return (*v)[i]
412432
}
@@ -498,6 +518,10 @@ func (v *nullableInt16Vector) Append(i interface{}) {
498518
*v = append(*v, i.(*int16))
499519
}
500520

521+
func (v *nullableInt16Vector) NilAt(i int) bool {
522+
return (*v)[i] == nil
523+
}
524+
501525
func (v *nullableInt16Vector) At(i int) interface{} {
502526
return (*v)[i]
503527
}
@@ -589,6 +613,10 @@ func (v *nullableInt32Vector) Append(i interface{}) {
589613
*v = append(*v, i.(*int32))
590614
}
591615

616+
func (v *nullableInt32Vector) NilAt(i int) bool {
617+
return (*v)[i] == nil
618+
}
619+
592620
func (v *nullableInt32Vector) At(i int) interface{} {
593621
return (*v)[i]
594622
}
@@ -680,6 +708,10 @@ func (v *nullableInt64Vector) Append(i interface{}) {
680708
*v = append(*v, i.(*int64))
681709
}
682710

711+
func (v *nullableInt64Vector) NilAt(i int) bool {
712+
return (*v)[i] == nil
713+
}
714+
683715
func (v *nullableInt64Vector) At(i int) interface{} {
684716
return (*v)[i]
685717
}
@@ -771,6 +803,10 @@ func (v *nullableFloat32Vector) Append(i interface{}) {
771803
*v = append(*v, i.(*float32))
772804
}
773805

806+
func (v *nullableFloat32Vector) NilAt(i int) bool {
807+
return (*v)[i] == nil
808+
}
809+
774810
func (v *nullableFloat32Vector) At(i int) interface{} {
775811
return (*v)[i]
776812
}
@@ -862,6 +898,10 @@ func (v *nullableFloat64Vector) Append(i interface{}) {
862898
*v = append(*v, i.(*float64))
863899
}
864900

901+
func (v *nullableFloat64Vector) NilAt(i int) bool {
902+
return (*v)[i] == nil
903+
}
904+
865905
func (v *nullableFloat64Vector) At(i int) interface{} {
866906
return (*v)[i]
867907
}
@@ -953,6 +993,10 @@ func (v *nullableStringVector) Append(i interface{}) {
953993
*v = append(*v, i.(*string))
954994
}
955995

996+
func (v *nullableStringVector) NilAt(i int) bool {
997+
return (*v)[i] == nil
998+
}
999+
9561000
func (v *nullableStringVector) At(i int) interface{} {
9571001
return (*v)[i]
9581002
}
@@ -1044,6 +1088,10 @@ func (v *nullableBoolVector) Append(i interface{}) {
10441088
*v = append(*v, i.(*bool))
10451089
}
10461090

1091+
func (v *nullableBoolVector) NilAt(i int) bool {
1092+
return (*v)[i] == nil
1093+
}
1094+
10471095
func (v *nullableBoolVector) At(i int) interface{} {
10481096
return (*v)[i]
10491097
}
@@ -1135,6 +1183,10 @@ func (v *nullableTimeTimeVector) Append(i interface{}) {
11351183
*v = append(*v, i.(*time.Time))
11361184
}
11371185

1186+
func (v *nullableTimeTimeVector) NilAt(i int) bool {
1187+
return (*v)[i] == nil
1188+
}
1189+
11381190
func (v *nullableTimeTimeVector) At(i int) interface{} {
11391191
return (*v)[i]
11401192
}
@@ -1226,6 +1278,10 @@ func (v *nullableJsonRawMessageVector) Append(i interface{}) {
12261278
*v = append(*v, i.(*json.RawMessage))
12271279
}
12281280

1281+
func (v *nullableJsonRawMessageVector) NilAt(i int) bool {
1282+
return (*v)[i] == nil
1283+
}
1284+
12291285
func (v *nullableJsonRawMessageVector) At(i int) interface{} {
12301286
return (*v)[i]
12311287
}

0 commit comments

Comments
 (0)