Commit b12251f 1 parent dd14b38 commit b12251f Copy full SHA for b12251f
File tree 2 files changed +24
-8
lines changed
2 files changed +24
-8
lines changed Original file line number Diff line number Diff line change @@ -11,12 +11,15 @@ import (
11
11
type Value struct {
12
12
Type string `json:"type"`
13
13
Value any `json:"value,omitempty"`
14
- Base64 string `json:"base64,omitempty"`
14
+ Base64 * string `json:"base64,omitempty"`
15
15
}
16
16
17
17
func (v Value ) ToValue (columnType * string ) any {
18
18
if v .Type == "blob" {
19
- bytes , err := base64 .StdEncoding .WithPadding (base64 .NoPadding ).DecodeString (v .Base64 )
19
+ if v .Base64 == nil {
20
+ return nil
21
+ }
22
+ bytes , err := base64 .StdEncoding .WithPadding (base64 .NoPadding ).DecodeString (* v .Base64 )
20
23
if err != nil {
21
24
return nil
22
25
}
@@ -65,7 +68,8 @@ func ToValue(v any) (Value, error) {
65
68
res .Value = text
66
69
} else if blob , ok := v .([]byte ); ok {
67
70
res .Type = "blob"
68
- res .Base64 = base64 .StdEncoding .WithPadding (base64 .NoPadding ).EncodeToString (blob )
71
+ b64 := base64 .StdEncoding .WithPadding (base64 .NoPadding ).EncodeToString (blob )
72
+ res .Base64 = & b64
69
73
} else if float , ok := v .(float64 ); ok {
70
74
res .Type = "float"
71
75
res .Value = float
Original file line number Diff line number Diff line change @@ -7,6 +7,10 @@ import (
7
7
"testing"
8
8
"time"
9
9
)
10
+
11
+ func toPtr [T any ](v T ) * T {
12
+ return & v
13
+ }
10
14
11
15
func TestValueToValue (t * testing.T ) {
12
16
tests := []struct {
@@ -43,10 +47,18 @@ func TestValueToValue(t *testing.T) {
43
47
name : "bytes" ,
44
48
value : Value {
45
49
Type : "blob" ,
46
- Base64 : "YmFy" ,
50
+ Base64 : toPtr ( "YmFy" ) ,
47
51
},
48
52
want : []byte ("bar" ),
49
53
},
54
+ {
55
+ name : "bytes" ,
56
+ value : Value {
57
+ Type : "blob" ,
58
+ Base64 : toPtr ("" ),
59
+ },
60
+ want : []byte {},
61
+ },
50
62
{
51
63
name : "float" ,
52
64
value : Value {
@@ -111,10 +123,10 @@ func TestToValue(t *testing.T) {
111
123
},
112
124
{
113
125
name : "bytes" ,
114
- value : []byte ( "bar" ) ,
126
+ value : []byte {} ,
115
127
want : Value {
116
128
Type : "blob" ,
117
- Base64 : "YmFy" ,
129
+ Base64 : toPtr ( "" ) ,
118
130
},
119
131
},
120
132
{
@@ -195,7 +207,7 @@ func TestMarshal(t *testing.T) {
195
207
name : "bytes" ,
196
208
value : Value {
197
209
Type : "blob" ,
198
- Base64 : "YmFy" ,
210
+ Base64 : toPtr ( "YmFy" ) ,
199
211
},
200
212
marshaled : `{"type":"blob","base64":"YmFy"}` ,
201
213
},
@@ -263,7 +275,7 @@ func TestUnmarshal(t *testing.T) {
263
275
name : "bytes" ,
264
276
value : Value {
265
277
Type : "blob" ,
266
- Base64 : "YmFy" ,
278
+ Base64 : toPtr ( "YmFy" ) ,
267
279
},
268
280
marshaled : `{"type":"blob","base64":"YmFy"}` ,
269
281
},
You can’t perform that action at this time.
0 commit comments