-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for proto.to_any #46
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -23,10 +23,13 @@ import ( | |||||
"reflect" | ||||||
"sort" | ||||||
|
||||||
gogo_proto "github.com/gogo/protobuf/proto" | ||||||
gogo_types "github.com/gogo/protobuf/types" | ||||||
"github.com/golang/protobuf/jsonpb" | ||||||
"github.com/golang/protobuf/proto" | ||||||
"github.com/golang/protobuf/ptypes" | ||||||
"go.starlark.net/starlark" | ||||||
yaml "gopkg.in/yaml.v2" | ||||||
"gopkg.in/yaml.v2" | ||||||
) | ||||||
|
||||||
// UNSTABLE extension point for configuring how protobuf messages are loaded. | ||||||
|
@@ -53,6 +56,7 @@ func NewProtoModule(registry ProtoRegistry) *ProtoModule { | |||||
"merge": starlark.NewBuiltin("proto.merge", fnProtoMerge), | ||||||
"set_defaults": starlark.NewBuiltin("proto.set_defaults", fnProtoSetDefaults), | ||||||
"to_json": starlark.NewBuiltin("proto.to_json", fnProtoToJson), | ||||||
"to_any": starlark.NewBuiltin("proto.to_any", fnProtoToAny), | ||||||
"to_text": starlark.NewBuiltin("proto.to_text", fnProtoToText), | ||||||
"to_yaml": starlark.NewBuiltin("proto.to_yaml", fnProtoToYaml), | ||||||
}, | ||||||
|
@@ -234,6 +238,33 @@ func fnProtoToJson(t *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple | |||||
return starlark.String(jsonData), nil | ||||||
} | ||||||
|
||||||
// Implementation of the `proto.to_any()` built-in function. Returns a | ||||||
// skyProtoMessage with an `Any` proto.Message in it. | ||||||
func fnProtoToAny(t *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { | ||||||
var msg *skyProtoMessage | ||||||
err := wantSingleProtoMessage("proto.to_any", args, kwargs, &msg) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
||||||
// Disambiguate between golang and gogo encoded proto messages. | ||||||
var any proto.Message | ||||||
if "" != proto.MessageName(msg.msg) { | ||||||
// Returns a golang any.Any type. | ||||||
any, err = ptypes.MarshalAny(msg.msg) | ||||||
} else if "" != gogo_proto.MessageName(msg.msg) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably only go here if msg's package has a Previously @jmillikin-stripe also insisted on gogo dependencies going into separate package to avoid hard dependency within core implementation. |
||||||
// Returns a gogo types.Any type. | ||||||
any, err = gogo_types.MarshalAny(msg.msg) | ||||||
} else { | ||||||
return nil, fmt.Errorf("%s: could not get message name for %s", "proto.to_any", msg.Type()) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
||||||
return NewSkyProtoMessage(any), nil | ||||||
} | ||||||
|
||||||
// Implementation of the `proto.to_yaml()` built-in function. Returns the | ||||||
// YAML-formatted content of a protobuf message. | ||||||
func fnProtoToYaml(t *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.