User Defined Types (UDTs)

ScyllaDB/Cassandra UDTs map to Go structs. gocqlx simplifies this mapping significantly.

The gocqlx.UDT Interface

To make a struct usable as a UDT, simply embed the gocqlx.UDT marker interface. You do not need to implement custom MarshalCQL or UnmarshalCQL methods manually.

Example

Schema:

CREATE TYPE address (street text, city text, zip int);
CREATE TABLE users (id uuid PRIMARY KEY, addr address);

Go Code:

type Address struct {
    gocqlx.UDT  // Embed this marker
    Street string
    City   string
    Zip    int
}

type User struct {
    ID   gocql.UUID
    Addr Address
}

func main() {
    u := User{
        ID: gocql.TimeUUID(),
        Addr: Address{
            Street: "123 Go St",
            City:   "ScyllaDB",
            Zip:    90210,
        },
    }

    // BindStruct handles the UDT automatically
    q := session.Query("INSERT INTO users (id, addr) VALUES (:id, :addr)", 
        []string{"id", "addr"})
    q.BindStruct(u)
    q.Exec()
}

If you cannot modify the struct (e.g., it comes from a third-party library), you can create a wrapper struct that embeds gocqlx.UDT and the target struct.