| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package descriptor | 5 package descriptor |
| 6 | 6 |
| 7 import "strings" | 7 import ( |
| 8 » "strings" |
| 9 ) |
| 10 |
| 11 const ( |
| 12 » // NumberFileDescriptorProto_Package is the number of package field |
| 13 » // in FileDescriptorProto message. |
| 14 » NumberFileDescriptorProto_Package = 2 |
| 15 » // NumberFileDescriptorProto_Message is the number of message field |
| 16 » // in FileDescriptorProto message. |
| 17 » NumberFileDescriptorProto_Message = 4 |
| 18 » // NumberFileDescriptorProto_Enum is the number of enum field |
| 19 » // in FileDescriptorProto message. |
| 20 » NumberFileDescriptorProto_Enum = 5 |
| 21 » // NumberFileDescriptorProto_Service is the number of service field |
| 22 » // in FileDescriptorProto message. |
| 23 » NumberFileDescriptorProto_Service = 6 |
| 24 |
| 25 » // NumberServiceDescriptorProto_Method is the number of method field |
| 26 » // in ServiceDescriptorProto message. |
| 27 » NumberServiceDescriptorProto_Method = 2 |
| 28 |
| 29 » // NumberDescriptorProto_Field is the number of field field |
| 30 » // in DescriptorProto message. |
| 31 » NumberDescriptorProto_Field = 2 |
| 32 » // NumberDescriptorProto_NestedType is the number of nested_type field |
| 33 » // in DescriptorProto message. |
| 34 » NumberDescriptorProto_NestedType = 3 |
| 35 » // NumberDescriptorProto_EnumType is the number of enum_type field |
| 36 » // in DescriptorProto message. |
| 37 » NumberDescriptorProto_EnumType = 4 |
| 38 » // NumberDescriptorProto_OneOf is the number of oneof_decl field |
| 39 » // in DescriptorProto message. |
| 40 » NumberDescriptorProto_OneOf = 8 |
| 41 |
| 42 » // NumberEnumDescriptorProto_Value is the number of value field |
| 43 » // in EnumDescriptorProto message. |
| 44 » NumberEnumDescriptorProto_Value = 2 |
| 45 ) |
| 8 | 46 |
| 9 // splitFullName splits package name and service/type name. | 47 // splitFullName splits package name and service/type name. |
| 10 func splitFullName(fullName string) (pkg string, name string) { | 48 func splitFullName(fullName string) (pkg string, name string) { |
| 11 lastDot := strings.LastIndex(fullName, ".") | 49 lastDot := strings.LastIndex(fullName, ".") |
| 12 if lastDot < 0 { | 50 if lastDot < 0 { |
| 13 return "", fullName | 51 return "", fullName |
| 14 } | 52 } |
| 15 return fullName[:lastDot], fullName[lastDot+1:] | 53 return fullName[:lastDot], fullName[lastDot+1:] |
| 16 } | 54 } |
| 17 | 55 |
| 18 //////////////////////////////////////////////////////////////////////////////// | 56 //////////////////////////////////////////////////////////////////////////////// |
| 19 // FileDescriptorSet | 57 // FileDescriptorSet |
| 20 | 58 |
| 21 // FindFile searches for a FileDescriptorProto by name. | 59 // FindFile searches for a FileDescriptorProto by name. |
| 22 func (s *FileDescriptorSet) FindFile(name string) *FileDescriptorProto { | 60 func (s *FileDescriptorSet) FindFile(name string) int { |
| 23 » for _, x := range s.GetFile() { | 61 » for i, x := range s.GetFile() { |
| 24 if x.GetName() == name { | 62 if x.GetName() == name { |
| 25 » » » return x | 63 » » » return i |
| 26 } | 64 } |
| 27 } | 65 } |
| 28 » return nil | 66 » return -1 |
| 29 } | 67 } |
| 30 | 68 |
| 31 // FindMessage searches for a message by full name. | 69 // Resolve searches for an object by full name. obj can be one of |
| 32 func (s *FileDescriptorSet) FindMessage(fullName string) *DescriptorProto { | 70 // *ServiceDescriptorProto, |
| 71 // *MethodDescriptorProto, |
| 72 // *DescriptorProto, |
| 73 // *FieldDescriptorProto, |
| 74 // *DescriptorProto, |
| 75 // *EnumDescriptorProto, |
| 76 // *EnumValueDescriptorProto or |
| 77 // nil |
| 78 // |
| 79 // For path, see comment in SourceCodeInfo message. |
| 80 func (s *FileDescriptorSet) Resolve(fullName string) (file *FileDescriptorProto,
obj interface{}, path []int) { |
| 81 » if fullName == "" { |
| 82 » » return nil, nil, nil |
| 83 » } |
| 84 » pkg, name := splitFullName(fullName) |
| 85 |
| 86 » // Check top-level objects. |
| 87 » for _, f := range s.GetFile() { |
| 88 » » if f.GetPackage() == pkg { |
| 89 » » » if i := f.FindService(name); i != -1 { |
| 90 » » » » return f, f.Service[i], []int{NumberFileDescript
orProto_Service, i} |
| 91 » » » } |
| 92 » » » if i := f.FindMessage(name); i != -1 { |
| 93 » » » » return f, f.MessageType[i], []int{NumberFileDesc
riptorProto_Message, i} |
| 94 » » » } |
| 95 » » » if i := f.FindEnum(name); i != -1 { |
| 96 » » » » return f, f.EnumType[i], []int{NumberFileDescrip
torProto_Enum, i} |
| 97 » » » } |
| 98 » » } |
| 99 » } |
| 100 |
| 101 » // Recurse. |
| 102 » var parent interface{} |
| 103 » file, parent, path = s.Resolve(pkg) |
| 104 » switch parent := parent.(type) { |
| 105 |
| 106 » case *ServiceDescriptorProto: |
| 107 » » if i := parent.FindMethod(name); i != -1 { |
| 108 » » » return file, parent.Method[i], append(path, NumberServic
eDescriptorProto_Method, i) |
| 109 » » } |
| 110 |
| 111 » case *DescriptorProto: |
| 112 » » if i := parent.FindMessage(name); i != -1 { |
| 113 » » » return file, parent.NestedType[i], append(path, NumberDe
scriptorProto_NestedType, i) |
| 114 » » } |
| 115 » » if i := parent.FindEnum(name); i != -1 { |
| 116 » » » return file, parent.EnumType[i], append(path, NumberDesc
riptorProto_EnumType, i) |
| 117 » » } |
| 118 » » if i := parent.FindField(name); i != -1 { |
| 119 » » » return file, parent.Field[i], append(path, NumberDescrip
torProto_Field, i) |
| 120 » » } |
| 121 » » if i := parent.FindOneOf(name); i != -1 { |
| 122 » » » return file, parent.OneofDecl[i], append(path, NumberDes
criptorProto_OneOf, i) |
| 123 » » } |
| 124 |
| 125 » case *EnumDescriptorProto: |
| 126 » » if i := parent.FindValue(name); i != -1 { |
| 127 » » » return file, parent.Value[i], append(path, NumberEnumDes
criptorProto_Value, i) |
| 128 » » } |
| 129 » } |
| 130 |
| 131 » return nil, nil, nil |
| 132 } |
| 133 |
| 134 // FindService searches for a service by full name. |
| 135 func (s *FileDescriptorSet) FindService(fullName string) (file *FileDescriptorPr
oto, serviceIndex int) { |
| 33 pkg, name := splitFullName(fullName) | 136 pkg, name := splitFullName(fullName) |
| 34 for _, f := range s.GetFile() { | 137 for _, f := range s.GetFile() { |
| 35 if f.GetPackage() == pkg { | 138 if f.GetPackage() == pkg { |
| 36 » » » if x := f.FindMessage(name); x != nil { | 139 » » » if i := f.FindService(name); i != -1 { |
| 37 » » » » return x | 140 » » » » return f, i |
| 38 } | 141 } |
| 39 } | 142 } |
| 40 } | 143 } |
| 41 » return nil | 144 » return nil, -1 |
| 42 } | |
| 43 | |
| 44 // FindEnum searches for a enum by full name. | |
| 45 func (s *FileDescriptorSet) FindEnum(fullName string) *EnumDescriptorProto { | |
| 46 » pkg, name := splitFullName(fullName) | |
| 47 » for _, f := range s.GetFile() { | |
| 48 » » if f.GetPackage() == pkg { | |
| 49 » » » if x := f.FindEnum(name); x != nil { | |
| 50 » » » » return x | |
| 51 » » » } | |
| 52 » » } | |
| 53 » } | |
| 54 » return nil | |
| 55 } | |
| 56 | |
| 57 // FindService searches for a service by full name. | |
| 58 func (s *FileDescriptorSet) FindService(fullName string) *ServiceDescriptorProto
{ | |
| 59 » pkg, name := splitFullName(fullName) | |
| 60 » for _, f := range s.GetFile() { | |
| 61 » » if f.GetPackage() == pkg { | |
| 62 » » » if x := f.FindService(name); x != nil { | |
| 63 » » » » return x | |
| 64 » » » } | |
| 65 » » } | |
| 66 » } | |
| 67 » return nil | |
| 68 } | 145 } |
| 69 | 146 |
| 70 //////////////////////////////////////////////////////////////////////////////// | 147 //////////////////////////////////////////////////////////////////////////////// |
| 71 // FileDescriptorProto | 148 // FileDescriptorProto |
| 72 | 149 |
| 73 // FindService searches for a ServiceDescriptorProto by name. | 150 // FindService searches for a ServiceDescriptorProto by name. |
| 74 func (f *FileDescriptorProto) FindService(name string) *ServiceDescriptorProto { | 151 func (f *FileDescriptorProto) FindService(name string) int { |
| 75 » for _, x := range f.GetService() { | 152 » for i, x := range f.GetService() { |
| 76 if x.GetName() == name { | 153 if x.GetName() == name { |
| 77 » » » return x | 154 » » » return i |
| 78 } | 155 } |
| 79 } | 156 } |
| 80 » return nil | 157 » return -1 |
| 81 } | 158 } |
| 82 | 159 |
| 83 // FindMessage searches for a DescriptorProto by name. | 160 // FindMessage searches for a DescriptorProto by name. |
| 84 func (f *FileDescriptorProto) FindMessage(name string) *DescriptorProto { | 161 func (f *FileDescriptorProto) FindMessage(name string) int { |
| 85 » for _, x := range f.GetMessageType() { | 162 » for i, x := range f.GetMessageType() { |
| 86 if x.GetName() == name { | 163 if x.GetName() == name { |
| 87 » » » return x | 164 » » » return i |
| 88 } | 165 } |
| 89 } | 166 } |
| 90 » return nil | 167 » return -1 |
| 91 } | 168 } |
| 92 | 169 |
| 93 // FindEnum searches for a EnumDescriptorProto by name. | 170 // FindEnum searches for an EnumDescriptorProto by name. |
| 94 func (f *FileDescriptorProto) FindEnum(name string) *EnumDescriptorProto { | 171 func (f *FileDescriptorProto) FindEnum(name string) int { |
| 95 » for _, x := range f.GetEnumType() { | 172 » for i, x := range f.GetEnumType() { |
| 96 if x.GetName() == name { | 173 if x.GetName() == name { |
| 97 » » » return x | 174 » » » return i |
| 98 } | 175 } |
| 99 } | 176 } |
| 100 » return nil | 177 » return -1 |
| 101 } | 178 } |
| 102 | 179 |
| 103 //////////////////////////////////////////////////////////////////////////////// | 180 //////////////////////////////////////////////////////////////////////////////// |
| 104 // ServiceDescriptorProto | 181 // ServiceDescriptorProto |
| 105 | 182 |
| 106 // FindMethod searches for a MethodDescriptorProto by name. | 183 // FindMethod searches for a MethodDescriptorProto by name. |
| 107 func (s *ServiceDescriptorProto) FindMethod(name string) *MethodDescriptorProto
{ | 184 func (s *ServiceDescriptorProto) FindMethod(name string) int { |
| 108 » for _, x := range s.GetMethod() { | 185 » for i, x := range s.GetMethod() { |
| 109 if x.GetName() == name { | 186 if x.GetName() == name { |
| 110 » » » return x | 187 » » » return i |
| 111 } | 188 } |
| 112 } | 189 } |
| 113 » return nil | 190 » return -1 |
| 114 } | 191 } |
| 115 | 192 |
| 116 //////////////////////////////////////////////////////////////////////////////// | 193 //////////////////////////////////////////////////////////////////////////////// |
| 117 // DescriptorProto (a message) | 194 // DescriptorProto (a message) |
| 118 | 195 |
| 119 // FindField searches for a FieldDescriptorProto by name. | 196 // FindField searches for a FieldDescriptorProto by name. |
| 120 func (d *DescriptorProto) FindField(name string) *FieldDescriptorProto { | 197 func (d *DescriptorProto) FindField(name string) int { |
| 121 » for _, x := range d.GetField() { | 198 » for i, x := range d.GetField() { |
| 122 if x.GetName() == name { | 199 if x.GetName() == name { |
| 123 » » » return x | 200 » » » return i |
| 124 } | 201 } |
| 125 } | 202 } |
| 126 » return nil | 203 » return -1 |
| 204 } |
| 205 |
| 206 // FindMessage searches for a nested DescriptorProto by name. |
| 207 func (d *DescriptorProto) FindMessage(name string) int { |
| 208 » for i, x := range d.GetNestedType() { |
| 209 » » if x.GetName() == name { |
| 210 » » » return i |
| 211 » » } |
| 212 » } |
| 213 » return -1 |
| 214 } |
| 215 |
| 216 // FindEnum searches for a nested EnumDescriptorProto by name. |
| 217 func (d *DescriptorProto) FindEnum(name string) int { |
| 218 » for i, x := range d.GetEnumType() { |
| 219 » » if x.GetName() == name { |
| 220 » » » return i |
| 221 » » } |
| 222 » } |
| 223 » return -1 |
| 224 } |
| 225 |
| 226 // FindOneOf searches for a nested OneofDescriptorProto by name. |
| 227 func (d *DescriptorProto) FindOneOf(name string) int { |
| 228 » for i, x := range d.GetOneofDecl() { |
| 229 » » if x.GetName() == name { |
| 230 » » » return i |
| 231 » » } |
| 232 » } |
| 233 » return -1 |
| 127 } | 234 } |
| 128 | 235 |
| 129 //////////////////////////////////////////////////////////////////////////////// | 236 //////////////////////////////////////////////////////////////////////////////// |
| 237 // EnumDescriptorProto |
| 238 |
| 239 // FindValue searches for an EnumValueDescriptorProto by name. |
| 240 func (e *EnumDescriptorProto) FindValue(name string) int { |
| 241 for i, x := range e.GetValue() { |
| 242 if x.GetName() == name { |
| 243 return i |
| 244 } |
| 245 } |
| 246 return -1 |
| 247 } |
| 248 |
| 249 //////////////////////////////////////////////////////////////////////////////// |
| 130 // SourceCodeInfo | 250 // SourceCodeInfo |
| 131 | 251 |
| 132 // FindLocation searches for a location by path. | 252 // FindLocation searches for a location by path. |
| 133 func (s *SourceCodeInfo) FindLocation(path []int) *SourceCodeInfo_Location { | 253 func (s *SourceCodeInfo) FindLocation(path []int) *SourceCodeInfo_Location { |
| 134 Outer: | 254 Outer: |
| 135 » for _, l := range s.Location { | 255 » for _, l := range s.GetLocation() { |
| 136 if len(path) != len(l.Path) { | 256 if len(path) != len(l.Path) { |
| 137 continue | 257 continue |
| 138 } | 258 } |
| 139 for i := range path { | 259 for i := range path { |
| 140 if int32(path[i]) != l.Path[i] { | 260 if int32(path[i]) != l.Path[i] { |
| 141 continue Outer | 261 continue Outer |
| 142 } | 262 } |
| 143 } | 263 } |
| 144 return l | 264 return l |
| 145 } | 265 } |
| 146 return nil | 266 return nil |
| 147 } | 267 } |
| OLD | NEW |