OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 Copyright 2016 The Chromium Authors. All rights reserved. |
| 3 Use of this source code is governed by a BSD-style license that can be |
| 4 found in the LICENSE file. |
| 5 --> |
| 6 |
| 7 <!-- |
| 8 rpcExplorer.descUtil exposes helper methods to work with protobuf |
| 9 descriptor messages. |
| 10 Primarily it implements name and comments resolution in a FileDescriptorSet. |
| 11 --> |
| 12 <script> |
| 13 'use strict'; |
| 14 |
| 15 var rpcExplorer = (function(rpcExplorer) { |
| 16 |
| 17 rpcExplorer.descUtil = { |
| 18 |
| 19 /** |
| 20 * for all descriptors in the file annotate resolves |
| 21 * SourceLocationInfo.Location message and assigns it to the |
| 22 * source_code_info property of the descriptor. |
| 23 * Prerequisite reading: |
| 24 * https://github.com/luci/luci-go/blob/ea240d0/common/proto/google/descri
ptor/descriptor.proto#L713 |
| 25 * @param {FileDescriptorProto} file |
| 26 */ |
| 27 annotate: function(file) { |
| 28 if (!file.source_code_info) { |
| 29 return; |
| 30 } |
| 31 |
| 32 // First, build a map { path -> location message }. |
| 33 function key(path) { |
| 34 var key = ''; |
| 35 for (var i = 0; i < path.length; i++) { |
| 36 key += path[i] + '.'; |
| 37 } |
| 38 return key; |
| 39 } |
| 40 var locationMap = {}; |
| 41 for (var i = 0; i < file.source_code_info.location.length; i++) { |
| 42 var loc = file.source_code_info.location[i]; |
| 43 if (loc.path) { |
| 44 locationMap[key(loc.path)] = loc; |
| 45 } |
| 46 } |
| 47 |
| 48 // Now join all descriptors in file with the map. |
| 49 var path = []; |
| 50 |
| 51 function annotateList(list, field, fn) { |
| 52 if (!list) { |
| 53 return; |
| 54 } |
| 55 path.push(field, 0); |
| 56 for (var i = 0; i < list.length; i++) { |
| 57 path[path.length - 1] = i; |
| 58 list[i].source_code_info = locationMap[key(path)]; |
| 59 if (fn) { |
| 60 fn(list[i]); |
| 61 } |
| 62 } |
| 63 path.pop(); |
| 64 path.pop(); |
| 65 } |
| 66 |
| 67 // The magic numbers below are message field numbers defined in |
| 68 // descriptor.proto. |
| 69 |
| 70 function annotateMessage(msg) { |
| 71 annotateList(msg.field, 2); |
| 72 annotateList(msg.nested_type, 3, annotateMessage); |
| 73 annotateList(msg.enum_type, 4, annotateEnum); |
| 74 } |
| 75 |
| 76 function annotateEnum(e) { |
| 77 annotateList(e.value, 2); |
| 78 } |
| 79 |
| 80 function annotateService(svc) { |
| 81 annotateList(svc.method, 2); |
| 82 } |
| 83 |
| 84 annotateList(file.message_type, 4, annotateMessage); |
| 85 annotateList(file.enum_type, 5, annotateEnum); |
| 86 annotateList(file.service, 6, annotateService); |
| 87 }, |
| 88 |
| 89 /** |
| 90 * Annotates a FileDescriptorSet. |
| 91 */ |
| 92 annotateSet: function(fileSet) { |
| 93 for (var i = 0; i < fileSet.file.length; i++) { |
| 94 this.annotate(fileSet.file[i]); |
| 95 } |
| 96 }, |
| 97 |
| 98 splitFullName: function(fullName) { |
| 99 var lastDot = fullName.lastIndexOf('.'); |
| 100 if (lastDot === -1) { |
| 101 return { |
| 102 pkg: '', |
| 103 name: fullName |
| 104 }; |
| 105 } |
| 106 |
| 107 return { |
| 108 pkg: fullName.substr(0, lastDot), |
| 109 name: fullName.substr(lastDot + 1) |
| 110 }; |
| 111 }, |
| 112 |
| 113 /** |
| 114 * Resolves services, methods, messages, fields, enums and enum values. |
| 115 */ |
| 116 resolve: function(desc, name) { |
| 117 if (!desc || !name) { |
| 118 return null; |
| 119 } |
| 120 name = this.splitFullName(name); |
| 121 |
| 122 var self = this; |
| 123 |
| 124 // searches in each list. |
| 125 function checkLists(lists) { |
| 126 if (!lists) { |
| 127 return null; |
| 128 } |
| 129 for (var type in lists) { |
| 130 var desc = self.findByName(lists[type], name.name); |
| 131 if (desc) { |
| 132 return {type: type, desc: desc }; |
| 133 } |
| 134 } |
| 135 return null; |
| 136 } |
| 137 |
| 138 // Check top-level descriptors. |
| 139 for (var i = 0; i < desc.file.length; i++) { |
| 140 var file = desc.file[i]; |
| 141 if (file['package'] != name.pkg) { |
| 142 continue |
| 143 } |
| 144 |
| 145 var result = checkLists({ |
| 146 service: file.service, |
| 147 messageType: file.message_type, |
| 148 enumType: file.enum_type |
| 149 }); |
| 150 if (result) { |
| 151 return result; |
| 152 } |
| 153 } |
| 154 |
| 155 // Possibly the entity we are resolving is not top-level and |
| 156 // name.name references an object inside an object referenced by |
| 157 // name.pkg. Try to resolve name.pkg. |
| 158 var parent = this.resolve(desc, name.pkg); |
| 159 if (!parent) { |
| 160 return null; |
| 161 } |
| 162 switch (parent.type) { |
| 163 case 'service': |
| 164 return checkLists({ method: parent.desc.method }); |
| 165 |
| 166 case 'messageType': |
| 167 return checkLists({ |
| 168 field: parent.desc.field, |
| 169 messageType: parent.desc.nested_type, |
| 170 enumType: parent.desc.enum_type |
| 171 }); |
| 172 |
| 173 case 'enumType': |
| 174 return checkLists({ |
| 175 enumValue: parent.desc.value, |
| 176 }); |
| 177 |
| 178 default: |
| 179 return null; |
| 180 } |
| 181 }, |
| 182 |
| 183 findByName: function(array, name) { |
| 184 if (!array) { |
| 185 return null; |
| 186 } |
| 187 for (var i = 0; i < array.length; i++) { |
| 188 if (array[i].name === name) { |
| 189 return array[i]; |
| 190 } |
| 191 } |
| 192 return null; |
| 193 }, |
| 194 |
| 195 trimPrefixDot: function(name) { |
| 196 if (typeof name === 'string' && name.charAt(0) === '.') { |
| 197 name = name.substr(1); |
| 198 } |
| 199 return name; |
| 200 } |
| 201 }; |
| 202 |
| 203 return rpcExplorer; |
| 204 }(rpcExplorer || {})); |
| 205 </script> |
OLD | NEW |