Skip to content

Commit cb4fcda

Browse files
authored
Copy slice in newgenVectorWithValues (#450)
1 parent e779767 commit cb4fcda

File tree

5 files changed

+142
-76
lines changed

5 files changed

+142
-76
lines changed

data/field_test.go

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,69 @@ import (
66
"time"
77

88
"github.com/grafana/grafana-plugin-sdk-go/data"
9+
"github.com/stretchr/testify/require"
910
)
1011

1112
func TestField(t *testing.T) {
12-
f := data.NewField("value", nil, []float64{1.0, 2.0, 3.0})
13-
14-
if f.Len() != 3 {
15-
t.Fatal("unexpected length")
16-
}
13+
t.Run("should create new field with expected values", func(t *testing.T) {
14+
f := data.NewField("value", nil, []float64{1.0, 2.0, 3.0})
15+
16+
if f.Len() != 3 {
17+
t.Fatal("unexpected length")
18+
}
19+
20+
require.Equal(t, 1.0, f.At(0))
21+
require.Equal(t, 2.0, f.At(1))
22+
require.Equal(t, 3.0, f.At(2))
23+
})
24+
25+
t.Run("field values should not change if source slice is modified", func(t *testing.T) {
26+
values := []float64{1.0, 2.0, 3.0}
27+
f := data.NewField("value", nil, values)
28+
values[1] = 3.0
29+
require.Equal(t, 2.0, f.At(1))
30+
})
1731
}
1832

19-
func TestField_Float64(t *testing.T) {
20-
field := data.NewField("value", nil, make([]*float64, 3))
21-
22-
want := 2.0
23-
field.Set(1, &want)
24-
25-
if field.Len() != 3 {
26-
t.Fatal("unexpected length")
27-
}
28-
29-
got := field.At(1).(*float64)
30-
31-
if *got != want {
32-
t.Errorf("%+v", *got)
33-
}
33+
func TestField_NullableFloat64(t *testing.T) {
34+
t.Run("should create new nullable float64 field with expected values", func(t *testing.T) {
35+
val := 2.0
36+
f := data.NewField("value", nil, []*float64{nil, &val, nil})
37+
38+
if f.Len() != 3 {
39+
t.Fatal("unexpected length")
40+
}
41+
42+
require.Nil(t, f.At(0))
43+
require.Equal(t, &val, f.At(1).(*float64))
44+
require.Nil(t, f.At(2))
45+
})
46+
47+
t.Run("field values should not change if source slice is modified", func(t *testing.T) {
48+
val := 2.0
49+
values := []*float64{nil, &val, nil}
50+
f := data.NewField("value", nil, values)
51+
newVal := 4.0
52+
values[1] = &newVal
53+
require.Equal(t, &val, f.At(1))
54+
})
55+
56+
t.Run("should set a value", func(t *testing.T) {
57+
field := data.NewField("value", nil, make([]*float64, 3))
58+
59+
want := 2.0
60+
field.Set(1, &want)
61+
62+
if field.Len() != 3 {
63+
t.Fatal("unexpected length")
64+
}
65+
66+
got := field.At(1).(*float64)
67+
68+
if *got != want {
69+
t.Errorf("%+v", *got)
70+
}
71+
})
3472
}
3573

3674
func TestField_String(t *testing.T) {

data/generic_nullable_vector.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ func newNullablegenVector(n int) *nullablegenVector {
88
}
99

1010
func newNullablegenVectorWithValues(s []*gen) *nullablegenVector {
11-
v := nullablegenVector(s)
12-
return &v
11+
v := make([]*gen, len(s))
12+
copy(v, s)
13+
return (*nullablegenVector)(&v)
1314
}
1415

1516
func (v *nullablegenVector) Set(idx int, i interface{}) {

data/generic_vector.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ func newgenVector(n int) *genVector {
1414
}
1515

1616
func newgenVectorWithValues(s []gen) *genVector {
17-
v := genVector(s)
18-
return &v
17+
v := make([]gen, len(s))
18+
copy(v, s)
19+
return (*genVector)(&v)
1920
}
2021

2122
func (v *genVector) Set(idx int, i interface{}) {

data/nullable_vector.gen.go

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ func newNullableUint8Vector(n int) *nullableUint8Vector {
1414
}
1515

1616
func newNullableUint8VectorWithValues(s []*uint8) *nullableUint8Vector {
17-
v := nullableUint8Vector(s)
18-
return &v
17+
v := make([]*uint8, len(s))
18+
copy(v, s)
19+
return (*nullableUint8Vector)(&v)
1920
}
2021

2122
func (v *nullableUint8Vector) Set(idx int, i interface{}) {
@@ -104,8 +105,9 @@ func newNullableUint16Vector(n int) *nullableUint16Vector {
104105
}
105106

106107
func newNullableUint16VectorWithValues(s []*uint16) *nullableUint16Vector {
107-
v := nullableUint16Vector(s)
108-
return &v
108+
v := make([]*uint16, len(s))
109+
copy(v, s)
110+
return (*nullableUint16Vector)(&v)
109111
}
110112

111113
func (v *nullableUint16Vector) Set(idx int, i interface{}) {
@@ -194,8 +196,9 @@ func newNullableUint32Vector(n int) *nullableUint32Vector {
194196
}
195197

196198
func newNullableUint32VectorWithValues(s []*uint32) *nullableUint32Vector {
197-
v := nullableUint32Vector(s)
198-
return &v
199+
v := make([]*uint32, len(s))
200+
copy(v, s)
201+
return (*nullableUint32Vector)(&v)
199202
}
200203

201204
func (v *nullableUint32Vector) Set(idx int, i interface{}) {
@@ -284,8 +287,9 @@ func newNullableUint64Vector(n int) *nullableUint64Vector {
284287
}
285288

286289
func newNullableUint64VectorWithValues(s []*uint64) *nullableUint64Vector {
287-
v := nullableUint64Vector(s)
288-
return &v
290+
v := make([]*uint64, len(s))
291+
copy(v, s)
292+
return (*nullableUint64Vector)(&v)
289293
}
290294

291295
func (v *nullableUint64Vector) Set(idx int, i interface{}) {
@@ -374,8 +378,9 @@ func newNullableInt8Vector(n int) *nullableInt8Vector {
374378
}
375379

376380
func newNullableInt8VectorWithValues(s []*int8) *nullableInt8Vector {
377-
v := nullableInt8Vector(s)
378-
return &v
381+
v := make([]*int8, len(s))
382+
copy(v, s)
383+
return (*nullableInt8Vector)(&v)
379384
}
380385

381386
func (v *nullableInt8Vector) Set(idx int, i interface{}) {
@@ -464,8 +469,9 @@ func newNullableInt16Vector(n int) *nullableInt16Vector {
464469
}
465470

466471
func newNullableInt16VectorWithValues(s []*int16) *nullableInt16Vector {
467-
v := nullableInt16Vector(s)
468-
return &v
472+
v := make([]*int16, len(s))
473+
copy(v, s)
474+
return (*nullableInt16Vector)(&v)
469475
}
470476

471477
func (v *nullableInt16Vector) Set(idx int, i interface{}) {
@@ -554,8 +560,9 @@ func newNullableInt32Vector(n int) *nullableInt32Vector {
554560
}
555561

556562
func newNullableInt32VectorWithValues(s []*int32) *nullableInt32Vector {
557-
v := nullableInt32Vector(s)
558-
return &v
563+
v := make([]*int32, len(s))
564+
copy(v, s)
565+
return (*nullableInt32Vector)(&v)
559566
}
560567

561568
func (v *nullableInt32Vector) Set(idx int, i interface{}) {
@@ -644,8 +651,9 @@ func newNullableInt64Vector(n int) *nullableInt64Vector {
644651
}
645652

646653
func newNullableInt64VectorWithValues(s []*int64) *nullableInt64Vector {
647-
v := nullableInt64Vector(s)
648-
return &v
654+
v := make([]*int64, len(s))
655+
copy(v, s)
656+
return (*nullableInt64Vector)(&v)
649657
}
650658

651659
func (v *nullableInt64Vector) Set(idx int, i interface{}) {
@@ -734,8 +742,9 @@ func newNullableFloat32Vector(n int) *nullableFloat32Vector {
734742
}
735743

736744
func newNullableFloat32VectorWithValues(s []*float32) *nullableFloat32Vector {
737-
v := nullableFloat32Vector(s)
738-
return &v
745+
v := make([]*float32, len(s))
746+
copy(v, s)
747+
return (*nullableFloat32Vector)(&v)
739748
}
740749

741750
func (v *nullableFloat32Vector) Set(idx int, i interface{}) {
@@ -824,8 +833,9 @@ func newNullableFloat64Vector(n int) *nullableFloat64Vector {
824833
}
825834

826835
func newNullableFloat64VectorWithValues(s []*float64) *nullableFloat64Vector {
827-
v := nullableFloat64Vector(s)
828-
return &v
836+
v := make([]*float64, len(s))
837+
copy(v, s)
838+
return (*nullableFloat64Vector)(&v)
829839
}
830840

831841
func (v *nullableFloat64Vector) Set(idx int, i interface{}) {
@@ -914,8 +924,9 @@ func newNullableStringVector(n int) *nullableStringVector {
914924
}
915925

916926
func newNullableStringVectorWithValues(s []*string) *nullableStringVector {
917-
v := nullableStringVector(s)
918-
return &v
927+
v := make([]*string, len(s))
928+
copy(v, s)
929+
return (*nullableStringVector)(&v)
919930
}
920931

921932
func (v *nullableStringVector) Set(idx int, i interface{}) {
@@ -1004,8 +1015,9 @@ func newNullableBoolVector(n int) *nullableBoolVector {
10041015
}
10051016

10061017
func newNullableBoolVectorWithValues(s []*bool) *nullableBoolVector {
1007-
v := nullableBoolVector(s)
1008-
return &v
1018+
v := make([]*bool, len(s))
1019+
copy(v, s)
1020+
return (*nullableBoolVector)(&v)
10091021
}
10101022

10111023
func (v *nullableBoolVector) Set(idx int, i interface{}) {
@@ -1094,8 +1106,9 @@ func newNullableTimeTimeVector(n int) *nullableTimeTimeVector {
10941106
}
10951107

10961108
func newNullableTimeTimeVectorWithValues(s []*time.Time) *nullableTimeTimeVector {
1097-
v := nullableTimeTimeVector(s)
1098-
return &v
1109+
v := make([]*time.Time, len(s))
1110+
copy(v, s)
1111+
return (*nullableTimeTimeVector)(&v)
10991112
}
11001113

11011114
func (v *nullableTimeTimeVector) Set(idx int, i interface{}) {

0 commit comments

Comments
 (0)