Skip to content

Commit

Permalink
Implement message type, merge, autoboxing
Browse files Browse the repository at this point in the history
  • Loading branch information
seena-stripe committed Aug 17, 2021
1 parent 3a578da commit 4089d9c
Show file tree
Hide file tree
Showing 4 changed files with 613 additions and 4 deletions.
13 changes: 12 additions & 1 deletion go/protomodule/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "protomodule",
srcs = [
"merge.go",
"protomodule.go",
"protomodule_enum.go",
"protomodule_list.go",
"protomodule_map.go",
"protomodule_message.go",
"protomodule_message_type.go",
"protomodule_package.go",
"type_conversions.go",
Expand All @@ -24,19 +26,28 @@ go_library(
"@org_golang_google_protobuf//proto",
"@org_golang_google_protobuf//reflect/protoreflect",
"@org_golang_google_protobuf//reflect/protoregistry",
"@org_golang_google_protobuf//types/dynamicpb",
"@org_golang_google_protobuf//types/known/anypb",
"@org_golang_google_protobuf//types/known/wrapperspb",
],
)

go_test(
name = "protomodule_test",
srcs = ["protomodule_test.go"],
srcs = [
"protomodule_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",
"@net_starlark_go//starlarkstruct",
"@net_starlark_go//syntax",
"@org_golang_google_protobuf//reflect/protoregistry",
"@org_golang_google_protobuf//types/descriptorpb",
"@org_golang_google_protobuf//types/known/anypb",
],
)
97 changes: 97 additions & 0 deletions go/protomodule/merge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2020 The Skycfg Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

package protomodule

import (
"fmt"

"go.starlark.net/starlark"
)

// Implements proto.merge merging src into dst, returning merged value
func mergeField(dst, src starlark.Value) (starlark.Value, error) {
if dst == nil {
return src, nil
}
if src == nil {
return dst, nil
}

if dst.Type() != src.Type() {
return nil, mergeError(dst, src)
}

switch dst := dst.(type) {
case *protoRepeated:
if src, ok := src.(*protoRepeated); ok {
newList := newProtoRepeated(dst.fieldDesc)

err := newList.Extend(dst)
if err != nil {
return nil, err
}

err = newList.Extend(src)
if err != nil {
return nil, err
}

return newList, nil
}
return nil, mergeError(dst, src)
case *protoMap:
if src, ok := src.(*protoMap); ok {
newMap := newProtoMap(dst.mapKey, dst.mapValue)

for _, item := range dst.Items() {
err := newMap.SetKey(item[0], item[1])
if err != nil {
return nil, err
}
}

for _, item := range src.Items() {
err := newMap.SetKey(item[0], item[1])
if err != nil {
return nil, err
}
}

return newMap, nil
}
return nil, mergeError(dst, src)
case *protoMessage:
if src, ok := src.(*protoMessage); ok {
newMessage, err := NewMessage(dst.msg)
if err != nil {
return nil, err
}

newMessage.Merge(dst)
newMessage.Merge(src)

return newMessage, nil
}
return nil, mergeError(dst, src)
default:
return src, nil
}
}

func mergeError(dst, src starlark.Value) error {
return fmt.Errorf("MergeError: Cannot merge protobufs of different types: Merge(%s, %s)", dst.Type(), src.Type())
}
Loading

0 comments on commit 4089d9c

Please sign in to comment.