| 1 | // Copyright 2018 Google LLC |
| 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 | package pb |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | |
| 20 | "google.golang.org/protobuf/reflect/protoreflect" |
| 21 | |
| 22 | dynamicpb "google.golang.org/protobuf/types/dynamicpb" |
| 23 | ) |
| 24 | |
| 25 | // newFileDescription returns a FileDescription instance with a complete listing of all the message |
| 26 | // types and enum values, as well as a map of extensions declared within any scope in the file. |
| 27 | func newFileDescription(fileDesc protoreflect.FileDescriptor, pbdb *Db) (*FileDescription, extensionMap) { |
| 28 | metadata := collectFileMetadata(fileDesc) |
| 29 | enums := make(map[string]*EnumValueDescription) |
| 30 | for name, enumVal := range metadata.enumValues { |
| 31 | enums[name] = newEnumValueDescription(name, enumVal) |
| 32 | } |
| 33 | types := make(map[string]*TypeDescription) |
| 34 | for name, msgType := range metadata.msgTypes { |
| 35 | types[name] = newTypeDescription(name, msgType, pbdb.extensions) |
| 36 | } |
| 37 | fileExtMap := make(extensionMap) |
| 38 | for typeName, extensions := range metadata.msgExtensionMap { |
| 39 | messageExtMap, found := fileExtMap[typeName] |
| 40 | if !found { |
| 41 | messageExtMap = make(map[string]*FieldDescription) |
| 42 | } |
| 43 | for _, ext := range extensions { |
| 44 | extDesc := dynamicpb.NewExtensionType(ext).TypeDescriptor() |
| 45 | messageExtMap[string(ext.FullName())] = newFieldDescription(extDesc) |
| 46 | } |
| 47 | fileExtMap[typeName] = messageExtMap |
| 48 | } |
| 49 | return &FileDescription{ |
| 50 | name: fileDesc.Path(), |
| 51 | types: types, |
| 52 | enums: enums, |
| 53 | }, fileExtMap |
| 54 | } |
| 55 | |
| 56 | // FileDescription holds a map of all types and enum values declared within a proto file. |
| 57 | type FileDescription struct { |
| 58 | name string |
| 59 | types map[string]*TypeDescription |
| 60 | enums map[string]*EnumValueDescription |
| 61 | } |
| 62 | |
| 63 | // Copy creates a copy of the FileDescription with updated Db references within its types. |
| 64 | func (fd *FileDescription) Copy(pbdb *Db) *FileDescription { |
| 65 | typesCopy := make(map[string]*TypeDescription, len(fd.types)) |
| 66 | for k, v := range fd.types { |
| 67 | typesCopy[k] = v.Copy(pbdb) |
| 68 | } |
| 69 | return &FileDescription{ |
| 70 | name: fd.name, |
| 71 | types: typesCopy, |
| 72 | enums: fd.enums, |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // GetName returns the fully qualified file path for the file. |
| 77 | func (fd *FileDescription) GetName() string { |
| 78 | return fd.name |
| 79 | } |
| 80 | |
| 81 | // GetEnumDescription returns an EnumDescription for a qualified enum value |
| 82 | // name declared within the .proto file. |
| 83 | func (fd *FileDescription) GetEnumDescription(enumName string) (*EnumValueDescription, bool) { |
| 84 | ed, found := fd.enums[sanitizeProtoName(enumName)] |
| 85 | return ed, found |
| 86 | } |
| 87 | |
| 88 | // GetEnumNames returns the string names of all enum values in the file. |
| 89 | func (fd *FileDescription) GetEnumNames() []string { |
| 90 | enumNames := make([]string, len(fd.enums)) |
| 91 | i := 0 |
| 92 | for _, e := range fd.enums { |
| 93 | enumNames[i] = e.Name() |
| 94 | i++ |
| 95 | } |
| 96 | return enumNames |
| 97 | } |
| 98 | |
| 99 | // GetTypeDescription returns a TypeDescription for a qualified protobuf message type name |
| 100 | // declared within the .proto file. |
| 101 | func (fd *FileDescription) GetTypeDescription(typeName string) (*TypeDescription, bool) { |
| 102 | td, found := fd.types[sanitizeProtoName(typeName)] |
| 103 | return td, found |
| 104 | } |
| 105 | |
| 106 | // GetTypeNames returns the list of all type names contained within the file. |
| 107 | func (fd *FileDescription) GetTypeNames() []string { |
| 108 | typeNames := make([]string, len(fd.types)) |
| 109 | i := 0 |
| 110 | for _, t := range fd.types { |
| 111 | typeNames[i] = t.Name() |
| 112 | i++ |
| 113 | } |
| 114 | return typeNames |
| 115 | } |
| 116 | |
| 117 | // sanitizeProtoName strips the leading '.' from the proto message name. |
| 118 | func sanitizeProtoName(name string) string { |
| 119 | if name != "" && name[0] == '.' { |
| 120 | return name[1:] |
| 121 | } |
| 122 | return name |
| 123 | } |
| 124 | |
| 125 | // fileMetadata is a flattened view of message types and enum values within a file descriptor. |
| 126 | type fileMetadata struct { |
| 127 | // msgTypes maps from fully-qualified message name to descriptor. |
| 128 | msgTypes map[string]protoreflect.MessageDescriptor |
| 129 | // enumValues maps from fully-qualified enum value to enum value descriptor. |
| 130 | enumValues map[string]protoreflect.EnumValueDescriptor |
| 131 | // msgExtensionMap maps from the protobuf message name being extended to a set of extensions |
| 132 | // for the type. |
| 133 | msgExtensionMap map[string][]protoreflect.ExtensionDescriptor |
| 134 | |
| 135 | // TODO: support enum type definitions for use in future type-check enhancements. |
| 136 | } |
| 137 | |
| 138 | // collectFileMetadata traverses the proto file object graph to collect message types and enum |
| 139 | // values and index them by their fully qualified names. |
| 140 | func collectFileMetadata(fileDesc protoreflect.FileDescriptor) *fileMetadata { |
| 141 | msgTypes := make(map[string]protoreflect.MessageDescriptor) |
| 142 | enumValues := make(map[string]protoreflect.EnumValueDescriptor) |
| 143 | msgExtensionMap := make(map[string][]protoreflect.ExtensionDescriptor) |
| 144 | collectMsgTypes(fileDesc.Messages(), msgTypes, enumValues, msgExtensionMap) |
| 145 | collectEnumValues(fileDesc.Enums(), enumValues) |
| 146 | collectExtensions(fileDesc.Extensions(), msgExtensionMap) |
| 147 | return &fileMetadata{ |
| 148 | msgTypes: msgTypes, |
| 149 | enumValues: enumValues, |
| 150 | msgExtensionMap: msgExtensionMap, |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // collectMsgTypes recursively collects messages, nested messages, and nested enums into a map of |
| 155 | // fully qualified protobuf names to descriptors. |
| 156 | func collectMsgTypes(msgTypes protoreflect.MessageDescriptors, |
| 157 | msgTypeMap map[string]protoreflect.MessageDescriptor, |
| 158 | enumValueMap map[string]protoreflect.EnumValueDescriptor, |
| 159 | msgExtensionMap map[string][]protoreflect.ExtensionDescriptor) { |
| 160 | for i := 0; i < msgTypes.Len(); i++ { |
| 161 | msgType := msgTypes.Get(i) |
| 162 | msgTypeMap[string(msgType.FullName())] = msgType |
| 163 | nestedMsgTypes := msgType.Messages() |
| 164 | if nestedMsgTypes.Len() != 0 { |
| 165 | collectMsgTypes(nestedMsgTypes, msgTypeMap, enumValueMap, msgExtensionMap) |
| 166 | } |
| 167 | nestedEnumTypes := msgType.Enums() |
| 168 | if nestedEnumTypes.Len() != 0 { |
| 169 | collectEnumValues(nestedEnumTypes, enumValueMap) |
| 170 | } |
| 171 | nestedExtensions := msgType.Extensions() |
| 172 | if nestedExtensions.Len() != 0 { |
| 173 | collectExtensions(nestedExtensions, msgExtensionMap) |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // collectEnumValues accumulates the enum values within an enum declaration. |
| 179 | func collectEnumValues(enumTypes protoreflect.EnumDescriptors, enumValueMap map[string]protoreflect.EnumValueDescriptor) { |
| 180 | for i := 0; i < enumTypes.Len(); i++ { |
| 181 | enumType := enumTypes.Get(i) |
| 182 | enumTypeValues := enumType.Values() |
| 183 | for j := 0; j < enumTypeValues.Len(); j++ { |
| 184 | enumValue := enumTypeValues.Get(j) |
| 185 | enumValueName := fmt.Sprintf("%s.%s", string(enumType.FullName()), string(enumValue.Name())) |
| 186 | enumValueMap[enumValueName] = enumValue |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | func collectExtensions(extensions protoreflect.ExtensionDescriptors, msgExtensionMap map[string][]protoreflect.ExtensionDescriptor) { |
| 192 | for i := 0; i < extensions.Len(); i++ { |
| 193 | ext := extensions.Get(i) |
| 194 | extendsMsg := string(ext.ContainingMessage().FullName()) |
| 195 | msgExts, found := msgExtensionMap[extendsMsg] |
| 196 | if !found { |
| 197 | msgExts = []protoreflect.ExtensionDescriptor{} |
| 198 | } |
| 199 | msgExts = append(msgExts, ext) |
| 200 | msgExtensionMap[extendsMsg] = msgExts |
| 201 | } |
| 202 | } |