Skip to content

Commit defe275

Browse files
author
Hein
committed
feat(sql): ✨ Improve base64 handling in SqlNull type
* Refactor base64 encoding and decoding checks for []byte types. * Simplify type assertions using if statements instead of switch cases.
1 parent f772534 commit defe275

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

pkg/spectypes/sql_types.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ func (n *SqlNull[T]) Scan(value any) error {
6464
// Check if T is []byte, and decode base64 if applicable
6565
// Do this BEFORE trying sql.Null to ensure base64 is handled
6666
var zero T
67-
switch any(zero).(type) {
68-
case []byte:
67+
if _, ok := any(zero).([]byte); ok {
6968
// For []byte types, try to decode from base64
7069
var strVal string
7170
switch v := value.(type) {
@@ -182,10 +181,9 @@ func (n SqlNull[T]) MarshalJSON() ([]byte, error) {
182181
}
183182

184183
// Check if T is []byte, and encode to base64
185-
switch v := any(n.Val).(type) {
186-
case []byte:
184+
if _, ok := any(n.Val).([]byte); ok {
187185
// Encode []byte as base64
188-
encoded := base64.StdEncoding.EncodeToString(v)
186+
encoded := base64.StdEncoding.EncodeToString(any(n.Val).([]byte))
189187
return json.Marshal(encoded)
190188
}
191189

@@ -202,8 +200,7 @@ func (n *SqlNull[T]) UnmarshalJSON(b []byte) error {
202200

203201
// Check if T is []byte, and decode from base64
204202
var val T
205-
switch any(val).(type) {
206-
case []byte:
203+
if _, ok := any(val).([]byte); ok {
207204
// Unmarshal as string first (JSON representation)
208205
var s string
209206
if err := json.Unmarshal(b, &s); err == nil {

0 commit comments

Comments
 (0)