Documentation
¶
Overview ¶
Package protocodec holds the protobuf codecs for servicekit/http connections.
They live here rather than in servicekit/http so that a JSON-only WebSocket or SSE server doesn't link google.golang.org/protobuf (issue 17). Both satisfy gohttp.Codec[I, O] and plug into BaseConn and BaseSSEConn the same way JSONCodec does:
conn := &gohttp.BaseConn[*pb.Action, *pb.State]{
Codec: &protocodec.ProtoJSONCodec[*pb.Action, *pb.State]{
NewInput: func() *pb.Action { return &pb.Action{} },
},
}
Until v0.2.0 these were gohttp.ProtoJSONCodec and gohttp.BinaryProtoCodec. Type aliases can't stay behind in servicekit/http, since an alias would import this package and pull protobuf back in, so callers change the import path.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BinaryProtoCodec ¶
type BinaryProtoCodec[I proto.Message, O proto.Message] struct { // NewInput is an optional factory function to create new input instances. // When provided, this is used for optimal performance. NewInput func() I // Input is an exemplar instance used as fallback when NewInput is nil. Input I }
BinaryProtoCodec handles encoding/decoding of protobuf messages using binary format. This provides maximum efficiency for high-throughput scenarios.
For best performance, provide NewInput factory function. If not provided, the codec falls back to reflection using the Input exemplar (slower).
func (*BinaryProtoCodec[I, O]) Decode ¶
func (c *BinaryProtoCodec[I, O]) Decode(data []byte, msgType gohttp.MessageType) (I, error)
Decode unmarshals binary protobuf data into a new message instance.
func (*BinaryProtoCodec[I, O]) Encode ¶
func (c *BinaryProtoCodec[I, O]) Encode(msg O) ([]byte, gohttp.MessageType, error)
Encode marshals a protobuf message to binary bytes.
type ProtoJSONCodec ¶
type ProtoJSONCodec[I proto.Message, O proto.Message] struct { // NewInput is an optional factory function to create new input instances. // When provided, this is used for optimal performance. // Example: func() *pb.PlayerAction { return &pb.PlayerAction{} } NewInput func() I // Input is an exemplar instance used as fallback when NewInput is nil. // The codec uses reflection to create new instances of the same type. // Example: &pb.PlayerAction{} Input I // MarshalOptions configures protojson marshaling behavior. MarshalOptions protojson.MarshalOptions // UnmarshalOptions configures protojson unmarshaling behavior. UnmarshalOptions protojson.UnmarshalOptions }
ProtoJSONCodec handles encoding/decoding of protobuf messages using JSON format. This provides human-readable wire format while maintaining proto type safety.
For best performance, provide NewInput factory function. If not provided, the codec falls back to reflection using the Input exemplar (slower).
func (*ProtoJSONCodec[I, O]) Decode ¶
func (c *ProtoJSONCodec[I, O]) Decode(data []byte, msgType gohttp.MessageType) (I, error)
Decode unmarshals JSON data into a new protobuf message instance.
func (*ProtoJSONCodec[I, O]) Encode ¶
func (c *ProtoJSONCodec[I, O]) Encode(msg O) ([]byte, gohttp.MessageType, error)
Encode marshals a protobuf message to JSON bytes.