| 1 | // Copyright 2025 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package mainscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/cmd/protoc-gen-go`/ |
| 6 | |
| 7 | import ( |
| 8 | "slicesscip-go gomod github.com/golang/go/src go1.23 slices/" |
| 9 | "stringsscip-go gomod github.com/golang/go/src go1.23 strings/" |
| 10 | "testingscip-go gomod github.com/golang/go/src go1.23 testing/" |
| 11 | |
| 12 | visibilitypbscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/cmd/protoc-gen-go/testdata/visibility`/ "google.golang.org/protobuf/cmd/protoc-gen-go/testdata/visibilityscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/cmd/protoc-gen-go/testdata/visibility`/" |
| 13 | "google.golang.org/protobuf/reflect/protodescscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/reflect/protodesc`/" |
| 14 | "google.golang.org/protobuf/types/descriptorpbscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/" |
| 15 | ) |
| 16 | |
| 17 | func TestVisibilityscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/cmd/protoc-gen-go`/TestVisibility().(t *testingscip-go gomod github.com/golang/go/src go1.23 testing/.Tscip-go gomod github.com/golang/go/src go1.23 testing/T#) { |
| 18 | // Verify that the visibility modifiers aren't lost when converting to descriptor protos. |
| 19 | fd := visibilitypbscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/cmd/protoc-gen-go/testdata/visibility`/.File_cmd_protoc_gen_go_testdata_visibility_visibility_protoscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/cmd/protoc-gen-go/testdata/visibility`/File_cmd_protoc_gen_go_testdata_visibility_visibility_proto. |
| 20 | fdProto := protodescscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/reflect/protodesc`/.ToFileDescriptorProtoscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/reflect/protodesc`/ToFileDescriptorProto().(fd) |
| 21 | |
| 22 | type testCase struct { |
| 23 | name string |
| 24 | visibility descriptorpbscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/.SymbolVisibilityscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/SymbolVisibility# |
| 25 | } |
| 26 | testCases := []testCase{ |
| 27 | { |
| 28 | name: "DefaultMessage", |
| 29 | visibility: descriptorpbscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/.SymbolVisibility_VISIBILITY_UNSETscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/SymbolVisibility_VISIBILITY_UNSET., |
| 30 | }, |
| 31 | { |
| 32 | name: "ExportMessage", |
| 33 | visibility: descriptorpbscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/.SymbolVisibility_VISIBILITY_EXPORTscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/SymbolVisibility_VISIBILITY_EXPORT., |
| 34 | }, |
| 35 | { |
| 36 | name: "LocalMessage", |
| 37 | visibility: descriptorpbscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/.SymbolVisibility_VISIBILITY_LOCALscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/SymbolVisibility_VISIBILITY_LOCAL., |
| 38 | }, |
| 39 | { |
| 40 | name: "DefaultEnum", |
| 41 | visibility: descriptorpbscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/.SymbolVisibility_VISIBILITY_UNSETscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/SymbolVisibility_VISIBILITY_UNSET., |
| 42 | }, |
| 43 | { |
| 44 | name: "ExportEnum", |
| 45 | visibility: descriptorpbscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/.SymbolVisibility_VISIBILITY_EXPORTscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/SymbolVisibility_VISIBILITY_EXPORT., |
| 46 | }, |
| 47 | { |
| 48 | name: "LocalEnum", |
| 49 | visibility: descriptorpbscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/.SymbolVisibility_VISIBILITY_LOCALscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/SymbolVisibility_VISIBILITY_LOCAL., |
| 50 | }, |
| 51 | } |
| 52 | // Instead of the boilerplate of handwritten cases for the nested types, we generate them |
| 53 | // algorithmically: |
| 54 | baseTestCases := slicesscip-go gomod github.com/golang/go/src go1.23 slices/.Clonescip-go gomod github.com/golang/go/src go1.23 slices/Clone().(testCases) |
| 55 | for _, topLevelCase := range baseTestCases { |
| 56 | if stringsscip-go gomod github.com/golang/go/src go1.23 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.23 strings/HasSuffix().(topLevelCase.name, "Enum") { |
| 57 | continue // only messages have nested types |
| 58 | } |
| 59 | for _, nestedCase := range baseTestCases { |
| 60 | nestedCase.name = topLevelCase.name + ".Nested" + nestedCase.name |
| 61 | testCases = append(testCases, nestedCase) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | for _, tc := range testCases { |
| 66 | t.Runscip-go gomod github.com/golang/go/src go1.23 testing/T#Run().(tc.name, func(t *testingscip-go gomod github.com/golang/go/src go1.23 testing/.Tscip-go gomod github.com/golang/go/src go1.23 testing/T#) { |
| 67 | path := stringsscip-go gomod github.com/golang/go/src go1.23 strings/.Splitscip-go gomod github.com/golang/go/src go1.23 strings/Split().(tc.name, ".") |
| 68 | decl, visPtr := findByNamescip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/cmd/protoc-gen-go`/findByName().(fdProto.MessageTypescip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/FileDescriptorProto#MessageType., fdProto.EnumTypescip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/FileDescriptorProto#EnumType., path) |
| 69 | |
| 70 | if decl.GetVisibilityscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/cmd/protoc-gen-go`/hasVisibility#GetVisibility.() != tc.visibility { |
| 71 | t.Errorfscip-go gomod github.com/golang/go/src go1.23 testing/common#Errorf().("expected %v to have %v visibility but instead got %v", decl.GetNamescip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/cmd/protoc-gen-go`/hasVisibility#GetName.(), tc.visibility, decl.GetVisibilityscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/cmd/protoc-gen-go`/hasVisibility#GetVisibility.()) |
| 72 | } |
| 73 | if tc.visibility == descriptorpbscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/.SymbolVisibility_VISIBILITY_UNSETscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/SymbolVisibility_VISIBILITY_UNSET. { |
| 74 | // For this one, we go one step further and expect the raw field to be a nil pointer. |
| 75 | if visPtr != nil { |
| 76 | t.Errorfscip-go gomod github.com/golang/go/src go1.23 testing/common#Errorf().("expected %v to have no visibility but instead got %v", decl.GetNamescip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/cmd/protoc-gen-go`/hasVisibility#GetName.(), visPtr) |
| 77 | } |
| 78 | } |
| 79 | }) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func findByNamescip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/cmd/protoc-gen-go`/findByName().(msgs []*descriptorpbscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/.DescriptorProtoscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/DescriptorProto#, enums []*descriptorpbscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/.EnumDescriptorProtoscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/EnumDescriptorProto#, path []string) (hasVisibilityscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/cmd/protoc-gen-go`/hasVisibility#, *descriptorpbscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/.SymbolVisibilityscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/SymbolVisibility#) { |
| 84 | name := path[0] |
| 85 | if len(path) > 1 { |
| 86 | // Looking for nested type. |
| 87 | for _, msg := range msgs { |
| 88 | if msg.GetNamescip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/DescriptorProto#GetName().() == name { |
| 89 | return findByNamescip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/cmd/protoc-gen-go`/findByName().(msg.NestedTypescip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/DescriptorProto#NestedType., msg.EnumTypescip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/DescriptorProto#EnumType., path[1:]) |
| 90 | } |
| 91 | } |
| 92 | return nil, nil |
| 93 | } |
| 94 | |
| 95 | if stringsscip-go gomod github.com/golang/go/src go1.23 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.23 strings/HasSuffix().(name, "Enum") { |
| 96 | for _, enum := range enums { |
| 97 | if enum.GetNamescip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/EnumDescriptorProto#GetName().() == name { |
| 98 | return enum, enum.Visibilityscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/EnumDescriptorProto#Visibility. |
| 99 | } |
| 100 | } |
| 101 | return nil, nil |
| 102 | } |
| 103 | |
| 104 | for _, msg := range msgs { |
| 105 | if msg.GetNamescip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/DescriptorProto#GetName().() == name { |
| 106 | return msg, msg.Visibilityscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/DescriptorProto#Visibility. |
| 107 | } |
| 108 | } |
| 109 | return nil, nil |
| 110 | } |
| 111 | |
| 112 | type hasVisibilityscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/cmd/protoc-gen-go`/hasVisibility# interface { |
| 113 | GetNamescip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/cmd/protoc-gen-go`/hasVisibility#GetName.() string |
| 114 | GetVisibilityscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/cmd/protoc-gen-go`/hasVisibility#GetVisibility.() descriptorpbscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/.SymbolVisibilityscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/types/descriptorpb`/SymbolVisibility# |
| 115 | } |