worryg0d commented on code in PR #1942:
URL: 
https://github.com/apache/cassandra-gocql-driver/pull/1942#discussion_r3095354639


##########
cluster.go:
##########
@@ -458,6 +465,38 @@ type SchemaListenersConfig struct {
        AggregateChangeListener AggregateChangeListener
 }
 
+// Holds configuration of encoding / decoding behavior of the driver.
+type EncodingConfig struct {
+       // Turns on the encoding nil maps as initialized UDTs for UDTs.
+       // This is a lagacy behavior and it is enabled by default for backward 
compatibility, but it will be disabled in the next major version
+       // If this is enabled, then nil maps will be encoded as an initilizied 
UDT object in which each field is set to NULL.
+       //
+       // 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>);
+       // ```
+       //
+       // 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}
+       // ---+-------------------------------
+       //
+       // Default: true
+       EncodeNilMapAsInitilizedUDT bool
+       // Supresses the warning that is emitted when 
EncodeNilMapAsInitilizedUDT is enabled.
+       SuppressEncodeNilMapAsInitilizedUDTWarning bool

Review Comment:
   To me, this is at least not something I would expect when dealing with 
nil-values. For example, std `json` lib encodes nil values as null and vice 
versa. But, I found out one more thing: driver behavior is inconsistent between 
struct-based and map-based UDT encoding.
   
   
   For example, the following code will have two different behaviors that 
depend on what is wrapped the data var:
   ```go
   
   func insertPerson(session *gocql.Session, id int, name string, data any) 
error {
        const insertQuery = `
        INSERT INTO gocqltest.persons (id, name, address) VALUES (?, ?, ?);
        `
   
        err := session.Query(insertQuery, id, name, addressMapData).Exec()
        if err != nil {
                return fmt.Errorf("failed to insert person: %w", err)
        }
   
        return nil
   }
   ```
   
   1. Encode as non-null when data holds `map[string]any(nil)`
   2. Encode as null when data holds nil ptr struct. I guess this actually 
related to all ptr types: 
https://github.com/apache/cassandra-gocql-driver/blob/trunk/marshal.go#L75-L77 



-- 
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]

Reply via email to