[#77] protogen: Initial implementation

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2024-04-25 15:13:10 +03:00
parent a28ceb251a
commit adb7c602d7
115 changed files with 36376 additions and 20397 deletions

View 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)
}
}