Skip to content

Commit 0e31e7c

Browse files
go/protomodule: add go1.23 iter support (#140)
Add `(*protoMap).Entries` and `(*protoList).Elements`, which return `iter.Seq2` and `iter.Seq` respectively, to mirror google/starlark-go#551.
1 parent 5de623b commit 0e31e7c

File tree

8 files changed

+100
-11
lines changed

8 files changed

+100
-11
lines changed

build/go_dependencies.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ def go_dependencies():
5252
go_repository(
5353
name = "net_starlark_go",
5454
importpath = "go.starlark.net",
55-
sum = "h1:7QkXlIVjYdSsKKSGnM0jQdw/2w9W5qcFDGTc00zKqgI=",
56-
version = "v0.0.0-20230925163745-10651d5192ab",
55+
sum = "h1:APah0oANPHA7m/z/1Ngcccc+BEO/dmLcEfrzHAQQY6w=",
56+
version = "v0.0.0-20240517230649-3792562d0b7f",
5757
)
5858
go_repository(
5959
name = "org_golang_google_protobuf",

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.22
44

55
require (
66
github.com/spaolacci/murmur3 v1.1.0
7-
go.starlark.net v0.0.0-20230925163745-10651d5192ab
7+
go.starlark.net v0.0.0-20240517230649-3792562d0b7f
88
google.golang.org/protobuf v1.33.0
99
gopkg.in/yaml.v2 v2.2.1
1010
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
22
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
33
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
44
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
5-
go.starlark.net v0.0.0-20230925163745-10651d5192ab h1:7QkXlIVjYdSsKKSGnM0jQdw/2w9W5qcFDGTc00zKqgI=
6-
go.starlark.net v0.0.0-20230925163745-10651d5192ab/go.mod h1:LcLNIzVOMp4oV+uusnpk+VU+SzXaJakUuBjoCSWH5dM=
5+
go.starlark.net v0.0.0-20240517230649-3792562d0b7f h1:APah0oANPHA7m/z/1Ngcccc+BEO/dmLcEfrzHAQQY6w=
6+
go.starlark.net v0.0.0-20240517230649-3792562d0b7f/go.mod h1:YKMCv9b1WrfWmeqdV5MAuEHWsu5iC+fe6kYl2sQjdI8=
77
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
88
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
99
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=

go/protomodule/BUILD

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
33
go_library(
44
name = "protomodule",
55
srcs = [
6+
"iter.go",
67
"merge.go",
78
"protomodule.go",
89
"protomodule_enum.go",
@@ -33,13 +34,13 @@ go_library(
3334
go_test(
3435
name = "protomodule_test",
3536
srcs = [
37+
"iter_test.go",
3638
"protomodule_message_test.go",
3739
"protomodule_test.go",
3840
],
3941
embed = [":protomodule"],
4042
deps = [
4143
"//internal/test_proto:test_proto_go_proto",
42-
"@net_starlark_go//resolve",
4344
"@net_starlark_go//starlark",
4445
"@net_starlark_go//starlarkstruct",
4546
"@net_starlark_go//syntax",

go/protomodule/iter.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2025 The Skycfg Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
// SPDX-License-Identifier: Apache-2.0
16+
17+
//go:build go1.23
18+
19+
package protomodule
20+
21+
import (
22+
"iter"
23+
24+
"go.starlark.net/starlark"
25+
)
26+
27+
func (m *protoMap) Entries() iter.Seq2[starlark.Value, starlark.Value] { return m.dict.Entries() }
28+
29+
func (r *protoRepeated) Elements() iter.Seq[starlark.Value] { return r.list.Elements() }

go/protomodule/iter_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2025 The Skycfg Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
// SPDX-License-Identifier: Apache-2.0
16+
17+
//go:build go1.23
18+
19+
package protomodule
20+
21+
import (
22+
"maps"
23+
"reflect"
24+
"slices"
25+
"testing"
26+
27+
"go.starlark.net/starlark"
28+
29+
pb "github.com/stripe/skycfg/internal/test_proto"
30+
)
31+
32+
func TestListType_Elements(t *testing.T) {
33+
msg := (&pb.MessageV3{}).ProtoReflect().Descriptor()
34+
listFieldDesc := msg.Fields().ByName("r_string")
35+
36+
rep := newProtoRepeated(listFieldDesc)
37+
rep.Append(starlark.String("a"))
38+
rep.Append(starlark.String("b"))
39+
40+
got := slices.Collect(rep.Elements())
41+
want := []starlark.Value{starlark.String("a"), starlark.String("b")}
42+
if !reflect.DeepEqual(want, got) {
43+
t.Fatalf("wanted: %v\ngot : %v", want, got)
44+
}
45+
}
46+
47+
func TestMapType_Entries(t *testing.T) {
48+
msg := (&pb.MessageV3{}).ProtoReflect().Descriptor()
49+
mapFieldDesc := msg.Fields().ByName("map_string")
50+
51+
pmap := newProtoMap(mapFieldDesc.MapKey(), mapFieldDesc.MapValue())
52+
pmap.SetKey(starlark.String("a"), starlark.String("1"))
53+
pmap.SetKey(starlark.String("b"), starlark.String("2"))
54+
55+
got := maps.Collect(pmap.Entries())
56+
want := map[starlark.Value]starlark.Value{
57+
starlark.String("a"): starlark.String("1"),
58+
starlark.String("b"): starlark.String("2"),
59+
}
60+
if !reflect.DeepEqual(want, got) {
61+
t.Fatalf("wanted: %v\ngot : %v", want, got)
62+
}
63+
}

go/protomodule/protomodule_map.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type protoMap struct {
4343

4444
var _ starlark.Value = (*protoMap)(nil)
4545
var _ starlark.Iterable = (*protoMap)(nil)
46+
var _ starlark.IterableMapping = (*protoMap)(nil)
4647
var _ starlark.Sequence = (*protoMap)(nil)
4748
var _ starlark.HasAttrs = (*protoMap)(nil)
4849
var _ starlark.HasSetKey = (*protoMap)(nil)

go/protomodule/protomodule_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"strings"
2222
"testing"
2323

24-
"go.starlark.net/resolve"
2524
"go.starlark.net/starlark"
2625
"go.starlark.net/starlarkstruct"
2726
"go.starlark.net/syntax"
@@ -34,10 +33,6 @@ import (
3433
pb "github.com/stripe/skycfg/internal/test_proto"
3534
)
3635

37-
func init() {
38-
resolve.AllowFloat = true
39-
}
40-
4136
func newRegistry() *protoregistry.Types {
4237
registry := &protoregistry.Types{}
4338
registry.RegisterMessage((&pb.MessageV2{}).ProtoReflect().Type())

0 commit comments

Comments
 (0)