Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nested UDTs #259

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 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
value reflect.Value
field map[string]reflect.Value
unsafe bool
}

func makeUDT(value reflect.Value, mapper *reflectx.Mapper, unsafe bool) udt {
return udt{
mapper: mapper,
value: value,
field: mapper.FieldMap(value),
unsafe: unsafe,
Expand All @@ -43,7 +45,13 @@ func (u udt) MarshalUDT(name string, info gocql.TypeInfo) ([]byte, error) {
return nil, fmt.Errorf("missing name %q in %s", name, u.value.Type())
}

return gocql.Marshal(info, value.Interface())
switch info.(type) {
case gocql.UDTTypeInfo:
return gocql.Marshal(info, makeUDT(value, u.mapper, u.unsafe))
default:
return gocql.Marshal(info, value.Interface())
}

}

func (u udt) UnmarshalUDT(name string, info gocql.TypeInfo, data []byte) error {
Expand All @@ -52,6 +60,10 @@ func (u udt) UnmarshalUDT(name string, info gocql.TypeInfo, data []byte) error {
return fmt.Errorf("missing name %q in %s", name, u.value.Type())
}

if value.Addr().Type().Implements(autoUDTInterface) {
return gocql.Unmarshal(info, data, makeUDT(value.Addr(), u.mapper, u.unsafe))
}

return gocql.Unmarshal(info, data, value.Addr().Interface())
}

Expand Down