From aa2b7a6dfbc7a516e23fe72caaca731e4ea641f1 Mon Sep 17 00:00:00 2001 From: Julian Early Date: Fri, 29 Aug 2025 16:55:46 -0700 Subject: [PATCH] encoding/protobuf: support decoding fully qualified references In a package foo.bar.baz with the message SomeMessage, the message type can be referenced locally as SomeMessage or as the fully qualified foo.bar.baz.SomeMessage. Support the latter form as well, and add tests. Fixes #4035. Closes #4036 as merged as of commit a018b523. Signed-off-by: Julian Early Change-Id: I90c32e35212ca57795abf3b93a8a62e61a069954 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1221581 TryBot-Result: CUEcueckoo Reviewed-by: Matthew Sackman Unity-Result: CUE porcuepine --- encoding/protobuf/parse.go | 6 +++++- encoding/protobuf/protobuf_test.go | 1 + .../testdata/full_references.proto.out.cue | 13 +++++++++++++ .../istio.io/api/other/full_references.proto | 15 +++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 encoding/protobuf/testdata/full_references.proto.out.cue create mode 100644 encoding/protobuf/testdata/istio.io/api/other/full_references.proto diff --git a/encoding/protobuf/parse.go b/encoding/protobuf/parse.go index b962112ad..9e7f914f7 100644 --- a/encoding/protobuf/parse.go +++ b/encoding/protobuf/parse.go @@ -277,7 +277,11 @@ func (p *protoConverter) resolveTopScope(pos scanner.Position, name string, opti if k == -1 { i = len(name) } - if m, ok := p.scope[0][name[:i]]; ok { + curName := name[:i] + if local, ok := strings.CutPrefix(curName, p.protoPkg+"."); ok { + curName = local + } + if m, ok := p.scope[0][curName]; ok { if m.pkg != nil { p.imported[m.pkg.qualifiedImportPath()] = true } diff --git a/encoding/protobuf/protobuf_test.go b/encoding/protobuf/protobuf_test.go index c08696116..ad2aefe03 100644 --- a/encoding/protobuf/protobuf_test.go +++ b/encoding/protobuf/protobuf_test.go @@ -37,6 +37,7 @@ func TestExtractDefinitions(t *testing.T) { "mixer/v1/attributes.proto", "mixer/v1/config/client/client_config.proto", "other/trailcomment.proto", + "other/full_references.proto", } for _, file := range testCases { t.Run(file, func(t *testing.T) { diff --git a/encoding/protobuf/testdata/full_references.proto.out.cue b/encoding/protobuf/testdata/full_references.proto.out.cue new file mode 100644 index 000000000..a1f947941 --- /dev/null +++ b/encoding/protobuf/testdata/full_references.proto.out.cue @@ -0,0 +1,13 @@ +package full_references + +#FullReferenceMsg: { + nestedMsg?: #FullReferenceNestedMsg @protobuf(1,istio.io.api.other.full_references.FullReferenceNestedMsg,name=nested_msg) +} + +#FullReferenceNestedMsg: { + + #FullReferenceDoubleNestedMsg: { + value?: string @protobuf(1,string) + } + nestedMsg?: #FullReferenceNestedMsg.#FullReferenceDoubleNestedMsg @protobuf(1,istio.io.api.other.full_references.FullReferenceNestedMsg.FullReferenceDoubleNestedMsg,name=nested_msg) +} diff --git a/encoding/protobuf/testdata/istio.io/api/other/full_references.proto b/encoding/protobuf/testdata/istio.io/api/other/full_references.proto new file mode 100644 index 000000000..2c81bde5d --- /dev/null +++ b/encoding/protobuf/testdata/istio.io/api/other/full_references.proto @@ -0,0 +1,15 @@ +syntax = "proto3"; + +package istio.io.api.other.full_references; + +message FullReferenceMsg { + istio.io.api.other.full_references.FullReferenceNestedMsg nested_msg = 1; +} + +message FullReferenceNestedMsg { + message FullReferenceDoubleNestedMsg { + string value = 1; + } + + istio.io.api.other.full_references.FullReferenceNestedMsg.FullReferenceDoubleNestedMsg nested_msg = 1; +} -- 2.51.2