forked from TrueCloudLab/frostfs-sdk-go
[#276] Merge repo with frostfs-api-go
Signed-off-by: Pavel Pogodaev <p.pogodaev@yadro.com>
This commit is contained in:
parent
5361f0eceb
commit
6ce73790ea
337 changed files with 66666 additions and 283 deletions
22
api/util/proto/encoding/compat.go
Normal file
22
api/util/proto/encoding/compat.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package encoding
|
||||
|
||||
import (
|
||||
_ "google.golang.org/grpc/encoding/proto" // Ensure default codec is registered before our one.
|
||||
"google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/protoadapt"
|
||||
)
|
||||
|
||||
// messageV2Of converts v to a proto.Message.
|
||||
// This is needed for this library to continue working in presence of external gRPC packages,
|
||||
// such as opentelemetry gRPC exporter.
|
||||
// Copied from https://github.com/grpc/grpc-go/blob/e524655becd8d4c7ba9e8687faef456e495e341e/encoding/proto/proto.go#L59.
|
||||
func messageV2Of(v any) proto.Message {
|
||||
switch v := v.(type) {
|
||||
case protoadapt.MessageV1:
|
||||
return protoadapt.MessageV2Of(v)
|
||||
case protoadapt.MessageV2:
|
||||
return v
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
48
api/util/proto/encoding/json.go
Normal file
48
api/util/proto/encoding/json.go
Normal file
|
@ -0,0 +1,48 @@
|
|||
package encoding
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"google.golang.org/grpc/encoding"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
)
|
||||
|
||||
// JSONCodec is easyjson codec used for code generated by protogen.
|
||||
// It is binary-level compatible with the standard protojson format, thus uses the same name.
|
||||
type JSONCodec struct{}
|
||||
|
||||
var _ encoding.Codec = JSONCodec{}
|
||||
|
||||
func init() {
|
||||
encoding.RegisterCodec(JSONCodec{})
|
||||
}
|
||||
|
||||
// Name implements the encoding.Codec interface.
|
||||
func (JSONCodec) Name() string { return "json" }
|
||||
|
||||
// Marshal implements the encoding.Codec interface.
|
||||
func (JSONCodec) Marshal(v any) ([]byte, error) {
|
||||
switch v := v.(type) {
|
||||
case json.Marshaler:
|
||||
return json.Marshal(v)
|
||||
default:
|
||||
if v := messageV2Of(v); v != nil {
|
||||
return protojson.Marshal(v)
|
||||
}
|
||||
return nil, fmt.Errorf("failed to marshal, message is %T, want proto.Message", v)
|
||||
}
|
||||
}
|
||||
|
||||
// Unmarshal implements the encoding.Codec interface.
|
||||
func (JSONCodec) Unmarshal(data []byte, v any) error {
|
||||
switch v := v.(type) {
|
||||
case json.Unmarshaler:
|
||||
return json.Unmarshal(data, v)
|
||||
default:
|
||||
if v := messageV2Of(v); v != nil {
|
||||
return protojson.Unmarshal(data, v)
|
||||
}
|
||||
return fmt.Errorf("failed to unmarshal, message is %T, want proto.Message", v)
|
||||
}
|
||||
}
|
57
api/util/proto/encoding/proto.go
Normal file
57
api/util/proto/encoding/proto.go
Normal file
|
@ -0,0 +1,57 @@
|
|||
package encoding
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"google.golang.org/grpc/encoding"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
// ProtoCodec is easyproto codec used for code generated by protogen.
|
||||
// It is binary-level compatible with the standard proto codec, thus uses the same name.
|
||||
type ProtoCodec struct{}
|
||||
|
||||
// ProtoMarshaler is an interface accepted by ProtoCodec.Marshal.
|
||||
type ProtoMarshaler interface {
|
||||
MarshalProtobuf([]byte) []byte
|
||||
}
|
||||
|
||||
// ProtoUnmarshaler is an interface accepted by ProtoCodec.Unmarshal.
|
||||
type ProtoUnmarshaler interface {
|
||||
UnmarshalProtobuf([]byte) error
|
||||
}
|
||||
|
||||
var _ encoding.Codec = ProtoCodec{}
|
||||
|
||||
func init() {
|
||||
encoding.RegisterCodec(ProtoCodec{})
|
||||
}
|
||||
|
||||
// Name implements the encoding.Codec interface.
|
||||
func (ProtoCodec) Name() string { return "proto" }
|
||||
|
||||
// Marshal implements the encoding.Codec interface.
|
||||
func (ProtoCodec) Marshal(v any) ([]byte, error) {
|
||||
switch v := v.(type) {
|
||||
case ProtoMarshaler:
|
||||
return v.MarshalProtobuf(nil), nil
|
||||
default:
|
||||
if v := messageV2Of(v); v != nil {
|
||||
return proto.Marshal(v)
|
||||
}
|
||||
return nil, fmt.Errorf("failed to marshal, message is %T, want proto.Message", v)
|
||||
}
|
||||
}
|
||||
|
||||
// Unmarshal implements the encoding.Codec interface.
|
||||
func (ProtoCodec) Unmarshal(data []byte, v any) error {
|
||||
switch v := v.(type) {
|
||||
case ProtoUnmarshaler:
|
||||
return v.UnmarshalProtobuf(data)
|
||||
default:
|
||||
if v := messageV2Of(v); v != nil {
|
||||
return proto.Unmarshal(data, v)
|
||||
}
|
||||
return fmt.Errorf("failed to unmarshal, message is %T, want proto.Message", v)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue