/*
 *
 * Copyright 2024 gRPC authors.
 *
 * 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 ( ) // Name is the name registered for the proto compressor. const Name = "proto" func () { encoding.RegisterCodecV2(&codecV2{}) } // codec is a CodecV2 implementation with protobuf. It is the default codec for // gRPC. type codecV2 struct{} func ( *codecV2) ( any) ( mem.BufferSlice, error) { := messageV2Of() if == nil { return nil, fmt.Errorf("proto: failed to marshal, message is %T, want proto.Message", ) } := proto.Size() if mem.IsBelowBufferPoolingThreshold() { , := proto.Marshal() if != nil { return nil, } = append(, mem.SliceBuffer()) } else { := mem.DefaultBufferPool() := .Get() if , := (proto.MarshalOptions{}).MarshalAppend((*)[:0], ); != nil { .Put() return nil, } = append(, mem.NewBuffer(, )) } return , nil } func ( *codecV2) ( mem.BufferSlice, any) ( error) { := messageV2Of() if == nil { return fmt.Errorf("failed to unmarshal, message is %T, want proto.Message", ) } := .MaterializeToBuffer(mem.DefaultBufferPool()) defer .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(.ReadOnlyData(), ) } func ( any) proto.Message { switch v := .(type) { case protoadapt.MessageV1: return protoadapt.MessageV2Of() case protoadapt.MessageV2: return } return nil } func ( *codecV2) () string { return Name }