-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add protobuf datacodec This adds the ability to send and receive protobuf encoded data within the envelope. Signed-off-by: Kevin Conway <kevinconway@invisionapp.com> * Move pb.Any handling to protobuf format This refactors the original protobuf datacodec so that it is generally useful as a codec within other formats. Previously, the coded wrapped and unwrapped every value in an Any container because this is required when the protobuf codec and format are used together. Now, the format handles wrapping the protobuf encoded binary data in the Any type. Signed-off-by: Kevin Conway <kevinconway@invisionapp.com> * Add init to register protobuf codec Signed-off-by: Kevin Conway <kevinconway@invisionapp.com> Co-authored-by: kconwayinvision <58523435+kconwayinvision@users.noreply.github.com>
- Loading branch information
1 parent
44de529
commit 79226a9
Showing
3 changed files
with
106 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package format | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"google.golang.org/protobuf/proto" | ||
|
||
"github.com/cloudevents/sdk-go/v2/event/datacodec" | ||
) | ||
|
||
const ( | ||
// ContentTypeProtobuf indicates that the data attribute is a protobuf | ||
// message. | ||
ContentTypeProtobuf = "application/protobuf" | ||
) | ||
|
||
func init() { | ||
datacodec.AddDecoder(ContentTypeProtobuf, DecodeData) | ||
datacodec.AddEncoder(ContentTypeProtobuf, EncodeData) | ||
} | ||
|
||
// DecodeData converts an encoded protobuf message back into the message (out). | ||
// The message must be a type compatible with whatever was given to EncodeData. | ||
func DecodeData(ctx context.Context, in []byte, out interface{}) error { | ||
outmsg, ok := out.(proto.Message) | ||
if !ok { | ||
return fmt.Errorf("can only decode protobuf into proto.Message. got %T", out) | ||
} | ||
if err := proto.Unmarshal(in, outmsg); err != nil { | ||
return fmt.Errorf("failed to unmarshal message: %s", err) | ||
} | ||
return nil | ||
} | ||
|
||
// EncodeData a protobuf message to bytes. | ||
// | ||
// Like the official datacodec implementations, this one returns the given value | ||
// as-is if it is already a byte slice. | ||
func EncodeData(ctx context.Context, in interface{}) ([]byte, error) { | ||
if b, ok := in.([]byte); ok { | ||
return b, nil | ||
} | ||
var pbmsg proto.Message | ||
var ok bool | ||
if pbmsg, ok = in.(proto.Message); !ok { | ||
return nil, fmt.Errorf("protobuf encoding only works with protobuf messages. got %T", in) | ||
} | ||
return proto.Marshal(pbmsg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters