Skip to content

Commit b12251f

Browse files
committed
base64 should be a pointer to allow empty blobs
Signed-off-by: Alexis Guerville <[email protected]>
1 parent dd14b38 commit b12251f

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

libsql/internal/hrana/value.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ import (
1111
type Value struct {
1212
Type string `json:"type"`
1313
Value any `json:"value,omitempty"`
14-
Base64 string `json:"base64,omitempty"`
14+
Base64 *string `json:"base64,omitempty"`
1515
}
1616

1717
func (v Value) ToValue(columnType *string) any {
1818
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)
2023
if err != nil {
2124
return nil
2225
}
@@ -65,7 +68,8 @@ func ToValue(v any) (Value, error) {
6568
res.Value = text
6669
} else if blob, ok := v.([]byte); ok {
6770
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
6973
} else if float, ok := v.(float64); ok {
7074
res.Type = "float"
7175
res.Value = float

libsql/internal/hrana/value_test.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import (
77
"testing"
88
"time"
99
)
10+
11+
func toPtr[T any](v T) *T {
12+
return &v
13+
}
1014

1115
func TestValueToValue(t *testing.T) {
1216
tests := []struct {
@@ -43,10 +47,18 @@ func TestValueToValue(t *testing.T) {
4347
name: "bytes",
4448
value: Value{
4549
Type: "blob",
46-
Base64: "YmFy",
50+
Base64: toPtr("YmFy"),
4751
},
4852
want: []byte("bar"),
4953
},
54+
{
55+
name: "bytes",
56+
value: Value{
57+
Type: "blob",
58+
Base64: toPtr(""),
59+
},
60+
want: []byte{},
61+
},
5062
{
5163
name: "float",
5264
value: Value{
@@ -111,10 +123,10 @@ func TestToValue(t *testing.T) {
111123
},
112124
{
113125
name: "bytes",
114-
value: []byte("bar"),
126+
value: []byte{},
115127
want: Value{
116128
Type: "blob",
117-
Base64: "YmFy",
129+
Base64: toPtr(""),
118130
},
119131
},
120132
{
@@ -195,7 +207,7 @@ func TestMarshal(t *testing.T) {
195207
name: "bytes",
196208
value: Value{
197209
Type: "blob",
198-
Base64: "YmFy",
210+
Base64: toPtr("YmFy"),
199211
},
200212
marshaled: `{"type":"blob","base64":"YmFy"}`,
201213
},
@@ -263,7 +275,7 @@ func TestUnmarshal(t *testing.T) {
263275
name: "bytes",
264276
value: Value{
265277
Type: "blob",
266-
Base64: "YmFy",
278+
Base64: toPtr("YmFy"),
267279
},
268280
marshaled: `{"type":"blob","base64":"YmFy"}`,
269281
},

0 commit comments

Comments
 (0)