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