Skip to content

Commit

Permalink
Fix nested UDT serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
thxph committed Feb 19, 2025
1 parent 3ea85fd commit 3ed594c
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion udt.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ var (
)

type udt struct {
mapper *reflectx.Mapper
field map[string]reflect.Value
value reflect.Value
strict bool
}

func makeUDT(value reflect.Value, mapper *reflectx.Mapper, strict bool) udt {
return udt{
mapper: mapper,
value: value,
field: mapper.FieldMap(value),
strict: strict,
Expand All @@ -40,7 +42,12 @@ func makeUDT(value reflect.Value, mapper *reflectx.Mapper, strict bool) udt {
func (u udt) MarshalUDT(name string, info gocql.TypeInfo) ([]byte, error) {
value, ok := u.field[name]
if ok {
return gocql.Marshal(info, value.Interface())
switch info.(type) {
case gocql.UDTTypeInfo:
return gocql.Marshal(info, makeUDT(value, u.mapper, u.strict))
default:
return gocql.Marshal(info, value.Interface())
}
}
if !u.strict {
return nil, nil
Expand All @@ -51,6 +58,10 @@ func (u udt) MarshalUDT(name string, info gocql.TypeInfo) ([]byte, error) {
func (u udt) UnmarshalUDT(name string, info gocql.TypeInfo, data []byte) error {
value, ok := u.field[name]
if ok {
if value.Addr().Type().Implements(autoUDTInterface) {
return gocql.Unmarshal(info, data, makeUDT(value.Addr(), u.mapper, u.strict))
}

return gocql.Unmarshal(info, data, value.Addr().Interface())
}
if !u.strict {
Expand Down

0 comments on commit 3ed594c

Please sign in to comment.