2018-03-06 22:33:18 +00:00
|
|
|
/*
|
|
|
|
*
|
2024-09-02 20:06:42 +00:00
|
|
|
* Copyright 2024 gRPC authors.
|
2018-03-06 22:33:18 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Package proto defines the protobuf codec. Importing this package will
|
|
|
|
// register the codec.
|
|
|
|
package proto
|
|
|
|
|
|
|
|
import (
|
2021-08-31 12:38:09 +00:00
|
|
|
"fmt"
|
|
|
|
|
2018-03-06 22:33:18 +00:00
|
|
|
"google.golang.org/grpc/encoding"
|
2024-09-02 20:06:42 +00:00
|
|
|
"google.golang.org/grpc/mem"
|
2024-02-26 20:50:31 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"google.golang.org/protobuf/protoadapt"
|
2018-03-06 22:33:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Name is the name registered for the proto compressor.
|
|
|
|
const Name = "proto"
|
|
|
|
|
|
|
|
func init() {
|
2024-09-02 20:06:42 +00:00
|
|
|
encoding.RegisterCodecV2(&codecV2{})
|
2018-03-06 22:33:18 +00:00
|
|
|
}
|
|
|
|
|
2024-09-02 20:06:42 +00:00
|
|
|
// codec is a CodecV2 implementation with protobuf. It is the default codec for
|
|
|
|
// gRPC.
|
|
|
|
type codecV2 struct{}
|
2018-03-06 22:33:18 +00:00
|
|
|
|
2024-09-02 20:06:42 +00:00
|
|
|
func (c *codecV2) Marshal(v any) (data mem.BufferSlice, err error) {
|
2024-02-26 20:50:31 +00:00
|
|
|
vv := messageV2Of(v)
|
|
|
|
if vv == nil {
|
2024-09-02 20:06:42 +00:00
|
|
|
return nil, fmt.Errorf("proto: failed to marshal, message is %T, want proto.Message", v)
|
2021-08-31 12:38:09 +00:00
|
|
|
}
|
2024-02-26 20:50:31 +00:00
|
|
|
|
2024-09-02 20:06:42 +00:00
|
|
|
size := proto.Size(vv)
|
|
|
|
if mem.IsBelowBufferPoolingThreshold(size) {
|
|
|
|
buf, err := proto.Marshal(vv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
data = append(data, mem.SliceBuffer(buf))
|
|
|
|
} else {
|
|
|
|
pool := mem.DefaultBufferPool()
|
|
|
|
buf := pool.Get(size)
|
|
|
|
if _, err := (proto.MarshalOptions{}).MarshalAppend((*buf)[:0], vv); err != nil {
|
|
|
|
pool.Put(buf)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
data = append(data, mem.NewBuffer(buf, pool))
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
2018-03-06 22:33:18 +00:00
|
|
|
}
|
|
|
|
|
2024-09-02 20:06:42 +00:00
|
|
|
func (c *codecV2) Unmarshal(data mem.BufferSlice, v any) (err error) {
|
2024-02-26 20:50:31 +00:00
|
|
|
vv := messageV2Of(v)
|
|
|
|
if vv == nil {
|
2021-08-31 12:38:09 +00:00
|
|
|
return fmt.Errorf("failed to unmarshal, message is %T, want proto.Message", v)
|
|
|
|
}
|
2024-02-26 20:50:31 +00:00
|
|
|
|
2024-09-02 20:06:42 +00:00
|
|
|
buf := data.MaterializeToBuffer(mem.DefaultBufferPool())
|
|
|
|
defer buf.Free()
|
|
|
|
// TODO: Upgrade proto.Unmarshal to support mem.BufferSlice. Right now, it's not
|
|
|
|
// really possible without a major overhaul of the proto package, but the
|
|
|
|
// vtprotobuf library may be able to support this.
|
|
|
|
return proto.Unmarshal(buf.ReadOnlyData(), vv)
|
2018-03-06 22:33:18 +00:00
|
|
|
}
|
|
|
|
|
2024-02-26 20:50:31 +00:00
|
|
|
func messageV2Of(v any) proto.Message {
|
|
|
|
switch v := v.(type) {
|
|
|
|
case protoadapt.MessageV1:
|
|
|
|
return protoadapt.MessageV2Of(v)
|
|
|
|
case protoadapt.MessageV2:
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-02 20:06:42 +00:00
|
|
|
func (c *codecV2) Name() string {
|
2018-03-06 22:33:18 +00:00
|
|
|
return Name
|
|
|
|
}
|