frostfs-api-go/util/proto/encoding/proto.go
Evgenii Stratonikov 5bae7bf6a9
Some checks failed
Tests and linters / Lint (pull_request) Failing after 27s
DCO action / DCO (pull_request) Failing after 51s
Tests and linters / Tests (1.19) (pull_request) Successful in 50s
Tests and linters / Tests (1.20) (pull_request) Successful in 1m3s
Tests and linters / Tests with -race (pull_request) Successful in 1m7s
protogen: Initial implementation
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
2024-08-01 16:10:02 +03:00

46 lines
1.2 KiB
Go

package encoding
import (
"fmt"
"google.golang.org/grpc/encoding"
)
// ProtoCodec is easyproto codec used for code generated by protogen.
// It is binary-level compatible with the standard proto codec, thus uses the same name.
type ProtoCodec struct{}
// ProtoMarshaler is an interface accepted by ProtoCodec.Marshal.
type ProtoMarshaler interface {
MarshalProtobuf([]byte) []byte
}
// ProtoUnmarshaler is an interface accepted by ProtoCodec.Unmarshal.
type ProtoUnmarshaler interface {
UnmarshalProtobuf([]byte) error
}
var _ encoding.Codec = ProtoCodec{}
// Name implements the encoding.Codec interface.
func (ProtoCodec) Name() string { return "proto" }
// Marshal implements the encoding.Codec interface.
func (ProtoCodec) Marshal(v any) ([]byte, error) {
switch v := v.(type) {
case ProtoMarshaler:
return v.MarshalProtobuf(nil), nil
default:
return nil, fmt.Errorf("unknown message type: %T", v)
}
}
// Unmarshal implements the encoding.Codec interface.
func (ProtoCodec) Unmarshal(data []byte, v any) error {
switch v := v.(type) {
case ProtoUnmarshaler:
return v.UnmarshalProtobuf(data)
default:
return fmt.Errorf("unknown message type: %T", v)
}
}