@@ -50,9 +50,10 @@ import (
5050
5151func TestBasicCRUD_JSONText (t * testing.T ) {
5252 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"`
5657 }
5758
5859 DB .Migrator ().DropTable (& JsonRecord {})
@@ -61,9 +62,11 @@ func TestBasicCRUD_JSONText(t *testing.T) {
6162 }
6263
6364 // INSERT
65+ json := datatypes .JSON ([]byte (`{"env":"prod","owner":"team-x"}` ))
6466 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 ,
6770 }
6871 if err := DB .Create (& rec ).Error ; err != nil {
6972 t .Fatalf ("create failed: %v" , err )
@@ -73,20 +76,23 @@ func TestBasicCRUD_JSONText(t *testing.T) {
7376 }
7477
7578 // UPDATE (with RETURNING)
79+ updateJson := datatypes .JSON ([]byte (`{"env":"staging","owner":"team-y","flag":true}` ))
7680 var ret JsonRecord
7781 if err := DB .
7882 Clauses (clause.Returning {
7983 Columns : []clause.Column {
8084 {Name : "record_id" },
8185 {Name : "name" },
8286 {Name : "properties" },
87+ {Name : "propertiesPtr" },
8388 },
8489 }).
8590 Model (& ret ).
8691 Where ("\" record_id\" = ?" , rec .ID ).
8792 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 ,
9096 }).Error ; err != nil {
9197 t .Fatalf ("update returning failed: %v" , err )
9298 }
@@ -103,6 +109,7 @@ func TestBasicCRUD_JSONText(t *testing.T) {
103109 {Name : "record_id" },
104110 {Name : "name" },
105111 {Name : "properties" },
112+ {Name : "propertiesPtr" },
106113 },
107114 }).
108115 Delete (& deleted ).Error ; err != nil {
@@ -122,20 +129,23 @@ func TestBasicCRUD_JSONText(t *testing.T) {
122129
123130func TestBasicCRUD_RawMessage (t * testing.T ) {
124131 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"`
128136 }
129137
130138 DB .Migrator ().DropTable (& RawRecord {})
131139 if err := DB .AutoMigrate (& RawRecord {}); err != nil {
132140 t .Fatalf ("migrate failed: %v" , err )
133141 }
134142
143+ rawMsg := json .RawMessage (`{"a":1,"b":"x"}` )
135144 // INSERT
136145 rec := RawRecord {
137- Name : "raw-json" ,
138- Properties : json .RawMessage (`{"a":1,"b":"x"}` ),
146+ Name : "raw-json" ,
147+ Properties : rawMsg ,
148+ PropertiesPtr : & rawMsg ,
139149 }
140150 if err := DB .Create (& rec ).Error ; err != nil {
141151 t .Fatalf ("create failed: %v" , err )
@@ -145,24 +155,30 @@ func TestBasicCRUD_RawMessage(t *testing.T) {
145155 }
146156
147157 // UPDATE (with RETURNING)
158+ upatedRawMsg := json .RawMessage (`{"a":2,"c":true}` )
148159 var ret RawRecord
149160 if err := DB .
150161 Clauses (clause.Returning {
151162 Columns : []clause.Column {
152163 {Name : "record_id" },
153164 {Name : "name" },
154165 {Name : "properties" },
166+ {Name : "propertiesPtr" },
155167 },
156168 }).
157169 Model (& ret ).
158170 Where ("\" record_id\" = ?" , rec .ID ).
159171 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 ,
162175 }).Error ; err != nil {
163176 t .Fatalf ("update returning failed: %v" , err )
164177 }
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 ) {
166182 t .Fatalf ("unexpected returning row: %#v" , ret )
167183 }
168184
@@ -175,6 +191,7 @@ func TestBasicCRUD_RawMessage(t *testing.T) {
175191 {Name : "record_id" },
176192 {Name : "name" },
177193 {Name : "properties" },
194+ {Name : "propertiesPtr" },
178195 },
179196 }).
180197 Delete (& deleted ).Error ; err != nil {
0 commit comments