apache / apache/cassandra-gocql-driver
marshal: add support for omitempty tags for UDTs
- Dominant language
- Go
- Stars
- 2.7k
- Forks
- 658
- PR merge metrics
- No merged PRs in 30d
Description
omitempty on cql tags is setting the value to null even when the value exists in the struct.
CQL:
``` cql
CREATE TYPE user (
name text,
phone text,
email text
);
CREATE TABLE test.events_by_userid (
userid text PRIMARY KEY,
user frozen
);
```
Golang:
``` go
type user struct {
Name string `cql:"name"`
Phone string `cql:"phone"`
Email string `cql:"email, omitempty"`
}
testE := user{
Name: "goutham",
Phone: "123456",
}
err = session.Query(`INSERT INTO events_by_userid (userid , user ) VALUES ( ?, ?)`, "goutham", testE).Exec()
```
has
``` cql
userid | user
---------+-------------------------------------------------
goutham | {name: 'goutham', phone: '123456', email: null}
```
But adding a omitempty to phone also sends a null value even if value exists.
``` go
type user struct {
Name string `cql:"name"`
Phone string `cql:"phone,omitempty"`
Email string `cql:"email, omitempty"`
}
testE := user{
Name: "goutham",
Phone: "123456",
}
err = session.Query(`INSERT INTO events_by_userid (userid , user ) VALUES ( ?, ?)`, "goutham", testE).Exec()
```
gives
``` cql
userid | user
---------+---------------------------------------------
goutham | {name: 'goutham', phone: null, email: null}
```
Contributor guide
Research direction
Start by reproducing the two Go UDT examples against the CQL user type and events_by_userid table described in the issue, then trace the UDT marshalling path. Done means omitempty omits empty fields but preserves populated fields such as Phone and does not encode them as null.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100