|
1 | 1 | package goqux
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "database/sql/driver" |
| 5 | + "encoding/json" |
4 | 6 | "testing"
|
5 | 7 |
|
6 | 8 | "github.com/stretchr/testify/assert"
|
7 | 9 | )
|
8 | 10 |
|
| 11 | +type structField struct { |
| 12 | + V map[string]interface{} `json:"v"` |
| 13 | +} |
| 14 | + |
| 15 | +func (s structField) Value() (driver.Value, error) { |
| 16 | + return json.Marshal(s) |
| 17 | +} |
| 18 | + |
| 19 | +type structValuerArray []structField |
| 20 | + |
| 21 | +func (s structValuerArray) Value() (driver.Value, error) { |
| 22 | + return json.Marshal(s) |
| 23 | +} |
| 24 | + |
9 | 25 | func TestSQLValuer_Value(t *testing.T) {
|
10 | 26 | tableTestValues := []struct {
|
11 | 27 | name string
|
@@ -65,6 +81,61 @@ func TestSQLValuer_Value(t *testing.T) {
|
65 | 81 | },
|
66 | 82 | expected: "{\"test\"}",
|
67 | 83 | },
|
| 84 | + { |
| 85 | + name: "struct_implements_valuer", |
| 86 | + value: structField{ |
| 87 | + V: map[string]interface{}{ |
| 88 | + "test": "test", |
| 89 | + }, |
| 90 | + }, |
| 91 | + expected: []byte(`{"v":{"test":"test"}}`), |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "valuer_pointer", |
| 95 | + value: &structField{ |
| 96 | + V: map[string]interface{}{ |
| 97 | + "test": "test", |
| 98 | + }, |
| 99 | + }, |
| 100 | + expected: []byte(`{"v":{"test":"test"}}`), |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "valuer_nil_pointer", |
| 104 | + value: (*structField)(nil), |
| 105 | + expected: nil, |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "empty_struct", |
| 109 | + value: structField{}, |
| 110 | + expected: []byte(`{"v":null}`), |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "empty_struct_pointer", |
| 114 | + value: &structField{}, |
| 115 | + expected: []byte(`{"v":null}`), |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "valuer_array_struct", |
| 119 | + value: structValuerArray{ |
| 120 | + { |
| 121 | + V: map[string]interface{}{ |
| 122 | + "test": "test", |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + expected: []byte(`[{"v":{"test":"test"}}]`), |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "valuer_array_pointer", |
| 130 | + value: &structValuerArray{ |
| 131 | + { |
| 132 | + V: map[string]interface{}{ |
| 133 | + "test": "test", |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + expected: []byte(`[{"v":{"test":"test"}}]`), |
| 138 | + }, |
68 | 139 | }
|
69 | 140 | for _, tt := range tableTestValues {
|
70 | 141 | t.Run(tt.name, func(t *testing.T) {
|
|
0 commit comments