3
* Copyright 2018 gRPC authors.
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
19
// Package proto defines the protobuf codec. Importing this package will
26
"google.golang.org/grpc/encoding"
27
"google.golang.org/protobuf/proto"
28
"google.golang.org/protobuf/protoadapt"
31
// Name is the name registered for the proto compressor.
35
encoding.RegisterCodec(codec{})
38
// codec is a Codec implementation with protobuf. It is the default codec for gRPC.
41
func (codec) Marshal(v any) ([]byte, error) {
44
return nil, fmt.Errorf("failed to marshal, message is %T, want proto.Message", v)
47
return proto.Marshal(vv)
50
func (codec) Unmarshal(data []byte, v any) error {
53
return fmt.Errorf("failed to unmarshal, message is %T, want proto.Message", v)
56
return proto.Unmarshal(data, vv)
59
func messageV2Of(v any) proto.Message {
60
switch v := v.(type) {
61
case protoadapt.MessageV1:
62
return protoadapt.MessageV2Of(v)
63
case protoadapt.MessageV2:
70
func (codec) Name() string {