worryg0d commented on code in PR #1942:
URL:
https://github.com/apache/cassandra-gocql-driver/pull/1942#discussion_r3577291743
##########
types.go:
##########
@@ -593,27 +600,88 @@ func (r *RegisteredTypes) Copy() *RegisteredTypes {
copy := &RegisteredTypes{}
copy.init()
+ copy.encodeNilMapAsNull = r.encodeNilMapAsNull
+ copy.warnOnNilMap = r.warnOnNilMap
+ // Adding default types to the copy so collection type codecs will have
a pointer to the copy instead of the original.
+ copy.addDefaultTypes()
for typ, t := range r.byType {
- copy.byType[typ] = t
+ if _, exists := copy.byType[typ]; !exists {
+ copy.byType[typ] = t
+ }
}
for typ, t := range r.simples {
- copy.simples[typ] = t
+ if _, exists := copy.simples[typ]; !exists {
+ copy.simples[typ] = t
+ }
}
for name, typ := range r.byString {
- copy.byString[name] = typ
+ if _, exists := copy.byString[name]; !exists {
+ copy.byString[name] = typ
+ }
}
for name, t := range r.custom {
- copy.custom[name] = t
+ if _, exists := copy.custom[name]; !exists {
+ copy.custom[name] = t
+ }
}
return copy
}
+// WithNullableUDTs creates a shallow copy of the RegisteredTypes with the
nullable UDTs flag set to the given value.
+//
+// If enabled, UDTs will be encoded as null CQL values when map is marshaled.
+//
+// When disabled, UDTs will be encoded as initialized UDT values with all its
fields set to NULL when passed map[string]any{} is nil.
+//
+// For example, there are following UDT and table definitions:
+//
+// ```cql
+// CREATE TYPE my_udt (field_a text, field_b int);
+// CREATE TABLE my_table (id int PRIMARY KEY, value frozen<my_udt>);
+// ```
+//
+// If this option is disabled, the following code will insert a UDT object
with both fields set to NULL:
+//
+// ```go
+// var nilMap map[string]interface{} = nil
+// session.Query("INSERT INTO my_table (id, value) VALUES (?, ?)", 1,
nilMap).Exec()
+// ```
+//
+// The table my_table will contain:
+//
+// id | value
+// 1 | {field_a: null, field_b: null}
+// ---+-------------------------------
+//
+// If this option is enabled, the same code will insert a NULL value:
+//
+// id | value
+// 1 | null
+// ---+-------------------------------
+//
+// When this method is called, it disables the warning message when nil map is
passed to UDT marshal regardless of the value of the flag.
+//
+// Default: false
+func (r *RegisteredTypes) WithNullableUDTs(enabled bool) *RegisteredTypes {
+ copy := r.Copy()
+ copy.encodeNilMapAsNull = enabled
+ copy.warnOnNilMap = false
+ return copy
+}
+
+func (r *RegisteredTypes) withLogger(logger StructuredLogger) *RegisteredTypes
{
Review Comment:
Yeah, it makes sense
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]