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

Align codec interface to exclusively use packers #3359

Merged
Merged
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ var (
ErrMaxSliceLenExceeded = errors.New("max slice length exceeded")
ErrDoesNotImplementInterface = errors.New("does not implement interface")
ErrUnexportedField = errors.New("unexported field")
ErrExtraSpace = errors.New("trailing buffer space")
ErrMarshalZeroLength = errors.New("can't marshal zero length value")
ErrUnmarshalZeroLength = errors.New("can't unmarshal zero length value")
)

// Codec marshals and unmarshals
type Codec interface {
MarshalInto(interface{}, *wrappers.Packer) error
Unmarshal([]byte, interface{}) error
UnmarshalFrom(*wrappers.Packer, interface{}) error

// Returns the size, in bytes, of [value] when it's marshaled
Expand Down
15 changes: 14 additions & 1 deletion codec/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
ErrCantPackVersion = errors.New("couldn't pack codec version")
ErrCantUnpackVersion = errors.New("couldn't unpack codec version")
ErrDuplicatedVersion = errors.New("duplicated codec version")
ErrExtraSpace = errors.New("trailing buffer space")
)

var _ Manager = (*manager)(nil)
Expand Down Expand Up @@ -157,5 +158,17 @@ func (m *manager) Unmarshal(bytes []byte, dest interface{}) (uint16, error) {
if !exists {
return version, ErrUnknownVersion
}
return version, c.Unmarshal(p.Bytes[p.Offset:], dest)

if err := c.UnmarshalFrom(&p, dest); err != nil {
return version, err
}
if p.Offset != len(bytes) {
return version, fmt.Errorf("%w: read %d provided %d",
ErrExtraSpace,
p.Offset,
len(bytes),
)
}

return version, nil
}
19 changes: 0 additions & 19 deletions codec/reflectcodec/type_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,25 +496,6 @@ func (c *genericCodec) marshal(
}
}

// Unmarshal unmarshals [bytes] into [dest], where [dest] must be a pointer or
// interface
func (c *genericCodec) Unmarshal(bytes []byte, dest interface{}) error {
p := wrappers.Packer{
Bytes: bytes,
}
if err := c.UnmarshalFrom(&p, dest); err != nil {
return err
}
if p.Offset != len(bytes) {
return fmt.Errorf("%w: read %d provided %d",
codec.ErrExtraSpace,
p.Offset,
len(bytes),
)
}
return nil
}

// UnmarshalFrom unmarshals [p.Bytes] into [dest], where [dest] must be a pointer or
// interface
func (c *genericCodec) UnmarshalFrom(p *wrappers.Packer, dest interface{}) error {
Expand Down
Loading