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

Add package description and codecs #89

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 20 additions & 0 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,24 @@ type JSONCodec struct {
var _ StableCodec = JSONCodec{}
var _ RESTCodec = JSONCodec{}

// Name returns the name of this codec. It always returns "json".
func (j JSONCodec) Name() string {
return CodecJSON
}

// IsBinary returns false, indicating that JSON is a text format.
func (j JSONCodec) IsBinary() bool {
return false
}

// Marshal serializes the given value to bytes. If the given value does
// not implement proto.Message, an error is returned.
func (j JSONCodec) MarshalAppend(base []byte, msg proto.Message) ([]byte, error) {
return j.MarshalOptions.MarshalAppend(base, msg)
}

// MarshalAppendStable is the same as MarshalAppend except that the
// bytes produced must be deterministic and stable.
func (j JSONCodec) MarshalAppendStable(base []byte, msg proto.Message) ([]byte, error) {
data, err := j.MarshalOptions.MarshalAppend(base, msg)
if err != nil {
Expand All @@ -135,6 +141,8 @@ func (j JSONCodec) MarshalAppendStable(base []byte, msg proto.Message) ([]byte,
return jsonStabilize(data)
}

// MarshalAppendField marshals just the given field of the given message to
// bytes, and appends it to the given base byte slice.
func (j JSONCodec) MarshalAppendField(base []byte, msg proto.Message, field protoreflect.FieldDescriptor) ([]byte, error) {
if field.Message() != nil && field.Cardinality() != protoreflect.Repeated {
return j.MarshalAppend(base, msg.ProtoReflect().Get(field).Message().Interface())
Expand Down Expand Up @@ -193,6 +201,8 @@ func (j JSONCodec) MarshalAppendField(base []byte, msg proto.Message, field prot
return nil, fmt.Errorf("JSON does not contain key %s", fieldName)
}

// UnmarshalField unmarshals the given data into the given field of the given
// message.
func (j JSONCodec) UnmarshalField(data []byte, msg proto.Message, field protoreflect.FieldDescriptor) error {
if field.Message() != nil && field.Cardinality() != protoreflect.Repeated {
return j.Unmarshal(data, msg.ProtoReflect().Mutable(field).Message().Interface())
Expand All @@ -213,6 +223,8 @@ func (j JSONCodec) UnmarshalField(data []byte, msg proto.Message, field protoref
return j.Unmarshal(buf.Bytes(), msg)
}

// Unmarshal de-serializes the given bytes into the given value. If the
// given value does not implement proto.Message, an error is returned.
func (j JSONCodec) Unmarshal(bytes []byte, msg proto.Message) error {
return j.UnmarshalOptions.Unmarshal(bytes, msg)
}
Expand All @@ -235,24 +247,32 @@ type protoCodec struct {

var _ StableCodec = protoCodec{}

// Name returns the name of this codec. It always returns "proto".
func (p protoCodec) Name() string {
return CodecProto
}

// IsBinary returns true, indicating that Protobuf is a binary format.
func (p protoCodec) IsBinary() bool {
return true
}

// Marshal serializes the given value to bytes. If the given value does
// not implement proto.Message, an error is returned.
func (p protoCodec) MarshalAppend(base []byte, msg proto.Message) ([]byte, error) {
return proto.MarshalOptions{}.MarshalAppend(base, msg)
}

// MarshalAppendStable is the same as MarshalAppend except that the
// bytes produced must be deterministic and stable.
func (p protoCodec) MarshalAppendStable(base []byte, msg proto.Message) ([]byte, error) {
opts := p.MarshalOptions
opts.Deterministic = true
return opts.MarshalAppend(base, msg)
}

// Unmarshal de-serializes the given bytes into the given value. If the
// given value does not implement proto.Message, an error is returned.
func (p protoCodec) Unmarshal(bytes []byte, msg proto.Message) error {
return p.UnmarshalOptions.Unmarshal(bytes, msg)
}
Expand Down
2 changes: 2 additions & 0 deletions vanguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package vanguard is an RPC transcoder for Connect, gRPC, gRPC-Web and REST
// protocols.
package vanguard

import (
Expand Down
1 change: 1 addition & 0 deletions vanguardgrpc/vanguardgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package vanguardgrpc is a vanguard option that wraps a gRPC server.
package vanguardgrpc

import (
Expand Down