mirror of
https://git.mirrors.martin98.com/https://github.com/ceph/ceph-csi.git
synced 2025-10-20 16:21:07 +08:00
![dependabot[bot]](/assets/img/avatar_default.png)
Bumps the golang-dependencies group with 1 update: [golang.org/x/crypto](https://github.com/golang/crypto). Updates `golang.org/x/crypto` from 0.16.0 to 0.17.0 - [Commits](https://github.com/golang/crypto/compare/v0.16.0...v0.17.0) --- updated-dependencies: - dependency-name: golang.org/x/crypto dependency-type: direct:production update-type: version-update:semver-minor dependency-group: golang-dependencies ... Signed-off-by: dependabot[bot] <support@github.com>
61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
package runtime
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
// ProtoMarshaller is a Marshaller which marshals/unmarshals into/from serialize proto bytes
|
|
type ProtoMarshaller struct{}
|
|
|
|
// ContentType always returns "application/octet-stream".
|
|
func (*ProtoMarshaller) ContentType(_ interface{}) string {
|
|
return "application/octet-stream"
|
|
}
|
|
|
|
// Marshal marshals "value" into Proto
|
|
func (*ProtoMarshaller) Marshal(value interface{}) ([]byte, error) {
|
|
message, ok := value.(proto.Message)
|
|
if !ok {
|
|
return nil, errors.New("unable to marshal non proto field")
|
|
}
|
|
return proto.Marshal(message)
|
|
}
|
|
|
|
// Unmarshal unmarshals proto "data" into "value"
|
|
func (*ProtoMarshaller) Unmarshal(data []byte, value interface{}) error {
|
|
message, ok := value.(proto.Message)
|
|
if !ok {
|
|
return errors.New("unable to unmarshal non proto field")
|
|
}
|
|
return proto.Unmarshal(data, message)
|
|
}
|
|
|
|
// NewDecoder returns a Decoder which reads proto stream from "reader".
|
|
func (marshaller *ProtoMarshaller) NewDecoder(reader io.Reader) Decoder {
|
|
return DecoderFunc(func(value interface{}) error {
|
|
buffer, err := io.ReadAll(reader)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return marshaller.Unmarshal(buffer, value)
|
|
})
|
|
}
|
|
|
|
// NewEncoder returns an Encoder which writes proto stream into "writer".
|
|
func (marshaller *ProtoMarshaller) NewEncoder(writer io.Writer) Encoder {
|
|
return EncoderFunc(func(value interface{}) error {
|
|
buffer, err := marshaller.Marshal(value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err := writer.Write(buffer); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|