Skip to content
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

Protobuf API V2: finish implementation and switchover skycfg to using new impl #93

Merged
merged 8 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

# gazelle:prefix github.com/stripe/skycfg
# gazelle:build_file_name BUILD,BUILD.bazel
Expand All @@ -17,11 +17,24 @@ go_library(
"//go/protomodule",
"//go/urlmodule",
"//go/yamlmodule",
"//internal/go/skycfg",
"@com_github_golang_protobuf//proto:go_default_library",
"@org_golang_google_protobuf//proto",
"@net_starlark_go//starlark",
"@net_starlark_go//starlarkjson",
"@net_starlark_go//starlarkstruct",
"@org_golang_google_protobuf//reflect/protoreflect",
"@org_golang_google_protobuf//reflect/protoregistry",
],
)

go_test(
name = "skycfg_test",
size = "small",
srcs = ["skycfg_test.go"],
embed = [":skycfg"],
deps = [
"//internal/testdata/test_proto:test_proto_go_proto",
"@org_golang_google_protobuf//proto",
"@net_starlark_go//starlark",
"@org_golang_google_protobuf//types/known/wrapperspb",
],
)
4 changes: 1 addition & 3 deletions go/protomodule/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ go_library(
importpath = "github.com/stripe/skycfg/go/protomodule",
visibility = ["//visibility:public"],
deps = [
"//internal/go/skycfg",
"@com_github_golang_protobuf//proto:go_default_library",
"@net_starlark_go//starlark",
"@net_starlark_go//starlarkstruct",
"@net_starlark_go//syntax",
Expand All @@ -36,11 +34,11 @@ go_test(
name = "protomodule_test",
srcs = [
"protomodule_test.go",
"protomodule_message_test.go",
],
embed = [":protomodule"],
deps = [
"//:skycfg",
"//internal/go/skycfg",
"//internal/testdata/test_proto:test_proto_go_proto",
"@net_starlark_go//resolve",
"@net_starlark_go//starlark",
Expand Down
64 changes: 31 additions & 33 deletions go/protomodule/protomodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@ package protomodule
import (
"fmt"

proto_v1 "github.com/golang/protobuf/proto"
"go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
any_pb "google.golang.org/protobuf/types/known/anypb"

impl "github.com/stripe/skycfg/internal/go/skycfg"
)

// NewModule returns a Starlark module of Protobuf-related functions.
Expand Down Expand Up @@ -61,6 +59,7 @@ func NewModule(registry *protoregistry.Types) *starlarkstruct.Module {
"encode_json": encodeJSON(registry),
"encode_text": encodeText(registry),
"merge": starlarkMerge,
"package": starlarkPackageFn(registry),
"set_defaults": starlarkSetDefaults,
},
}
Expand All @@ -72,15 +71,16 @@ var starlarkClear = starlark.NewBuiltin("proto.clear", func(
args starlark.Tuple,
kwargs []starlark.Tuple,
) (starlark.Value, error) {
protoMsg, skyProtoMsg, err := wantSingleProtoMessage(fn, args, kwargs)
_, skyProtoMsg, err := wantSingleProtoMessage(fn, args, kwargs)
if err != nil {
return nil, err
}
if err := skyProtoMsg.CheckMutable("clear"); err != nil {

err = skyProtoMsg.Clear()
if err != nil {
return nil, err
}
proto.Reset(protoMsg)
skyProtoMsg.ResetAttrCache()

return skyProtoMsg, nil
})

Expand All @@ -94,7 +94,7 @@ var starlarkClone = starlark.NewBuiltin("proto.clone", func(
if err != nil {
return nil, err
}
return impl.NewSkyProtoMessage(proto_v1.MessageV1(proto.Clone(msg))), nil
return NewMessage(proto.Clone(msg))
})

func decodeAny(registry *protoregistry.Types) starlark.Callable {
Expand All @@ -119,7 +119,7 @@ func decodeAny(registry *protoregistry.Types) starlark.Callable {
if err != nil {
return nil, err
}
return impl.NewSkyProtoMessage(proto_v1.MessageV1(decoded)), nil
return NewMessage(decoded)
})
}

Expand Down Expand Up @@ -147,7 +147,7 @@ func decodeJSON(registry *protoregistry.Types) starlark.Callable {
if err := unmarshal.Unmarshal([]byte(value), decoded); err != nil {
return nil, err
}
return impl.NewSkyProtoMessage(proto_v1.MessageV1(decoded)), nil
return NewMessage(decoded)
})
}

Expand Down Expand Up @@ -175,7 +175,7 @@ func decodeText(registry *protoregistry.Types) starlark.Callable {
if err := unmarshal.Unmarshal([]byte(value), decoded); err != nil {
return nil, err
}
return impl.NewSkyProtoMessage(proto_v1.MessageV1(decoded)), nil
return NewMessage(decoded)
})
}

Expand All @@ -195,7 +195,8 @@ var starlarkEncodeAny = starlark.NewBuiltin("proto.encode_any", func(
}); err != nil {
return nil, err
}
return impl.NewSkyProtoMessage(proto_v1.MessageV1(any)), nil

return NewMessage(any)
})

func encodeJSON(registry *protoregistry.Types) starlark.Callable {
Expand Down Expand Up @@ -275,24 +276,18 @@ var starlarkMerge = starlark.NewBuiltin("proto.merge", func(
if err := starlark.UnpackPositionalArgs(fn.Name(), args, kwargs, 2, &val1, &val2); err != nil {
return nil, err
}
dstMsg, ok := impl.ToProtoMessage(val1)
if !ok {
return nil, fmt.Errorf("%s: for parameter 1: got %s, want proto.Message", fn.Name(), val1.Type())
}
srcMsg, ok := impl.ToProtoMessage(val2)
if !ok {
return nil, fmt.Errorf("%s: for parameter 2: got %s, want proto.Message", fn.Name(), val2.Type())
}
dst := val1.(skyProtoMessage)
src := val2.(skyProtoMessage)

dst := val1.(*protoMessage)
src := val2.(*protoMessage)
if src.Type() != dst.Type() {
return nil, fmt.Errorf("%s: types are not the same: got %s and %s", fn.Name(), src.Type(), dst.Type())
}
if err := dst.CheckMutable("merge into"); err != nil {

err := dst.Merge(src)
if err != nil {
return nil, err
}
proto.Merge(proto_v1.MessageV2(dstMsg), proto_v1.MessageV2(srcMsg))
dst.ResetAttrCache()

return dst, nil
})

Expand All @@ -302,26 +297,29 @@ var starlarkSetDefaults = starlark.NewBuiltin("proto.set_defaults", func(
args starlark.Tuple,
kwargs []starlark.Tuple,
) (starlark.Value, error) {
protoMsg, skyProtoMsg, err := wantSingleProtoMessage(fn, args, kwargs)
_, skyProtoMsg, err := wantSingleProtoMessage(fn, args, kwargs)
if err != nil {
return nil, err
}
if err := skyProtoMsg.CheckMutable("set field defaults of"); err != nil {

err = skyProtoMsg.SetDefaults()
if err != nil {
return nil, err
}
proto_v1.SetDefaults(proto_v1.MessageV1(protoMsg))
skyProtoMsg.ResetAttrCache()

return skyProtoMsg, nil
})

type skyProtoMessageType interface {
NewMessage() proto.Message
NewMessage() protoreflect.ProtoMessage
}

type skyProtoMessage interface {
starlark.Value
MarshalJSON() ([]byte, error)
ResetAttrCache()
Clear() error
Merge(*protoMessage) error
SetDefaults() error
CheckMutable(string) error
}

Expand All @@ -334,9 +332,9 @@ func wantSingleProtoMessage(
if err := starlark.UnpackPositionalArgs(fn.Name(), args, kwargs, 1, &val); err != nil {
return nil, nil, err
}
gotMsg, ok := impl.ToProtoMessage(val)
gotMsg, ok := AsProtoMessage(val)
if !ok {
return nil, nil, fmt.Errorf("%s: for parameter 1: got %s, want proto.Message", fn.Name(), val.Type())
}
return proto_v1.MessageV2(gotMsg), val.(skyProtoMessage), nil
return gotMsg, val.(skyProtoMessage), nil
}
Loading