Skip to content

Commit f23a5f5

Browse files
committed
Fix nested UDT serialization
1 parent 3ea85fd commit f23a5f5

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

udt.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ var (
2424
)
2525

2626
type udt struct {
27+
mapper *reflectx.Mapper
2728
field map[string]reflect.Value
2829
value reflect.Value
2930
strict bool
3031
}
3132

3233
func makeUDT(value reflect.Value, mapper *reflectx.Mapper, strict bool) udt {
3334
return udt{
35+
mapper: mapper,
3436
value: value,
3537
field: mapper.FieldMap(value),
3638
strict: strict,
@@ -40,7 +42,12 @@ func makeUDT(value reflect.Value, mapper *reflectx.Mapper, strict bool) udt {
4042
func (u udt) MarshalUDT(name string, info gocql.TypeInfo) ([]byte, error) {
4143
value, ok := u.field[name]
4244
if ok {
43-
return gocql.Marshal(info, value.Interface())
45+
switch info.(type) {
46+
case gocql.UDTTypeInfo:
47+
return gocql.Marshal(info, makeUDT(value, u.mapper, u.strict))
48+
default:
49+
return gocql.Marshal(info, value.Interface())
50+
}
4451
}
4552
if !u.strict {
4653
return nil, nil
@@ -57,6 +64,12 @@ func (u udt) UnmarshalUDT(name string, info gocql.TypeInfo, data []byte) error {
5764
return nil
5865
}
5966
return fmt.Errorf("missing name %q in %s", name, u.value.Type())
67+
68+
if value.Addr().Type().Implements(autoUDTInterface) {
69+
return gocql.Unmarshal(info, data, makeUDT(value.Addr(), u.mapper, u.strict))
70+
}
71+
72+
return gocql.Unmarshal(info, data, value.Addr().Interface())
6073
}
6174

6275
// udtWrapValue adds UDT wrapper if needed.

0 commit comments

Comments
 (0)