@@ -50,9 +50,10 @@ import (
50
50
51
51
func TestBasicCRUD_JSONText (t * testing.T ) {
52
52
type JsonRecord struct {
53
- ID uint `gorm:"primaryKey;autoIncrement;column:record_id"`
54
- Name string `gorm:"column:name"`
55
- Properties datatypes.JSON `gorm:"column:properties"`
53
+ ID uint `gorm:"primaryKey;autoIncrement;column:record_id"`
54
+ Name string `gorm:"column:name"`
55
+ Properties datatypes.JSON `gorm:"column:properties"`
56
+ PropertiesPtr * datatypes.JSON `gorm:"column:propertiesPtr"`
56
57
}
57
58
58
59
DB .Migrator ().DropTable (& JsonRecord {})
@@ -61,9 +62,11 @@ func TestBasicCRUD_JSONText(t *testing.T) {
61
62
}
62
63
63
64
// INSERT
65
+ json := datatypes .JSON ([]byte (`{"env":"prod","owner":"team-x"}` ))
64
66
rec := JsonRecord {
65
- Name : "json-text" ,
66
- Properties : datatypes .JSON ([]byte (`{"env":"prod","owner":"team-x"}` )),
67
+ Name : "json-text" ,
68
+ Properties : json ,
69
+ PropertiesPtr : & json ,
67
70
}
68
71
if err := DB .Create (& rec ).Error ; err != nil {
69
72
t .Fatalf ("create failed: %v" , err )
@@ -73,20 +76,23 @@ func TestBasicCRUD_JSONText(t *testing.T) {
73
76
}
74
77
75
78
// UPDATE (with RETURNING)
79
+ updateJson := datatypes .JSON ([]byte (`{"env":"staging","owner":"team-y","flag":true}` ))
76
80
var ret JsonRecord
77
81
if err := DB .
78
82
Clauses (clause.Returning {
79
83
Columns : []clause.Column {
80
84
{Name : "record_id" },
81
85
{Name : "name" },
82
86
{Name : "properties" },
87
+ {Name : "propertiesPtr" },
83
88
},
84
89
}).
85
90
Model (& ret ).
86
91
Where ("\" record_id\" = ?" , rec .ID ).
87
92
Updates (map [string ]any {
88
- "name" : "json-text-upd" ,
89
- "properties" : datatypes .JSON ([]byte (`{"env":"staging","owner":"team-y","flag":true}` )),
93
+ "name" : "json-text-upd" ,
94
+ "properties" : updateJson ,
95
+ "propertiesPtr" : & updateJson ,
90
96
}).Error ; err != nil {
91
97
t .Fatalf ("update returning failed: %v" , err )
92
98
}
@@ -103,6 +109,7 @@ func TestBasicCRUD_JSONText(t *testing.T) {
103
109
{Name : "record_id" },
104
110
{Name : "name" },
105
111
{Name : "properties" },
112
+ {Name : "propertiesPtr" },
106
113
},
107
114
}).
108
115
Delete (& deleted ).Error ; err != nil {
@@ -122,20 +129,23 @@ func TestBasicCRUD_JSONText(t *testing.T) {
122
129
123
130
func TestBasicCRUD_RawMessage (t * testing.T ) {
124
131
type RawRecord struct {
125
- ID uint `gorm:"primaryKey;autoIncrement;column:record_id"`
126
- Name string `gorm:"column:name"`
127
- Properties json.RawMessage `gorm:"column:properties"`
132
+ ID uint `gorm:"primaryKey;autoIncrement;column:record_id"`
133
+ Name string `gorm:"column:name"`
134
+ Properties json.RawMessage `gorm:"column:properties"`
135
+ PropertiesPtr * json.RawMessage `gorm:"column:propertiesPtr"`
128
136
}
129
137
130
138
DB .Migrator ().DropTable (& RawRecord {})
131
139
if err := DB .AutoMigrate (& RawRecord {}); err != nil {
132
140
t .Fatalf ("migrate failed: %v" , err )
133
141
}
134
142
143
+ rawMsg := json .RawMessage (`{"a":1,"b":"x"}` )
135
144
// INSERT
136
145
rec := RawRecord {
137
- Name : "raw-json" ,
138
- Properties : json .RawMessage (`{"a":1,"b":"x"}` ),
146
+ Name : "raw-json" ,
147
+ Properties : rawMsg ,
148
+ PropertiesPtr : & rawMsg ,
139
149
}
140
150
if err := DB .Create (& rec ).Error ; err != nil {
141
151
t .Fatalf ("create failed: %v" , err )
@@ -145,24 +155,30 @@ func TestBasicCRUD_RawMessage(t *testing.T) {
145
155
}
146
156
147
157
// UPDATE (with RETURNING)
158
+ upatedRawMsg := json .RawMessage (`{"a":2,"c":true}` )
148
159
var ret RawRecord
149
160
if err := DB .
150
161
Clauses (clause.Returning {
151
162
Columns : []clause.Column {
152
163
{Name : "record_id" },
153
164
{Name : "name" },
154
165
{Name : "properties" },
166
+ {Name : "propertiesPtr" },
155
167
},
156
168
}).
157
169
Model (& ret ).
158
170
Where ("\" record_id\" = ?" , rec .ID ).
159
171
Updates (map [string ]any {
160
- "name" : "raw-json-upd" ,
161
- "properties" : json .RawMessage (`{"a":2,"c":true}` ),
172
+ "name" : "raw-json-upd" ,
173
+ "properties" : upatedRawMsg ,
174
+ "propertiesPtr" : & upatedRawMsg ,
162
175
}).Error ; err != nil {
163
176
t .Fatalf ("update returning failed: %v" , err )
164
177
}
165
- if ret .ID != rec .ID || ret .Name != "raw-json-upd" || len (ret .Properties ) == 0 {
178
+ if ret .ID != rec .ID ||
179
+ ret .Name != "raw-json-upd" ||
180
+ len (ret .Properties ) == 0 ||
181
+ ret .PropertiesPtr == nil || (ret .PropertiesPtr != nil && len (* ret .PropertiesPtr ) == 0 ) {
166
182
t .Fatalf ("unexpected returning row: %#v" , ret )
167
183
}
168
184
@@ -175,6 +191,7 @@ func TestBasicCRUD_RawMessage(t *testing.T) {
175
191
{Name : "record_id" },
176
192
{Name : "name" },
177
193
{Name : "properties" },
194
+ {Name : "propertiesPtr" },
178
195
},
179
196
}).
180
197
Delete (& deleted ).Error ; err != nil {
0 commit comments