Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / node_modules / protobufjs / cli / pbjs / sources / binary.js
1 /*\r
2  Copyright 2013 Daniel Wirtz <dcode@dcode.io>\r
3 \r
4  Licensed under the Apache License, Version 2.0 (the "License");\r
5  you may not use this file except in compliance with the License.\r
6  You may obtain a copy of the License at\r
7 \r
8  http://www.apache.org/licenses/LICENSE-2.0\r
9 \r
10  Unless required by applicable law or agreed to in writing, software\r
11  distributed under the License is distributed on an "AS IS" BASIS,\r
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  See the License for the specific language governing permissions and\r
14  limitations under the License.\r
15  */\r
16 var description = "Binary descriptor set";\r
17 \r
18 var ProtoBuf = require(__dirname+"/../../../index.js"),\r
19     util = require(__dirname+"/../util.js"),\r
20     node_path = require("path"),\r
21     fs = require("fs");\r
22 \r
23 /**\r
24  * pbjs source: Binary descriptor\r
25  * @exports pbjs/sources/binary\r
26  * @function\r
27  * @param {!Array.<string>} filenames Source files\r
28  * @param {!Object.<string,*>=} options Options\r
29  * @returns {!ProtoBuf.Builder}\r
30  */\r
31 var binary = module.exports = function(filenames, options) {\r
32     options = options || [];\r
33     var builder = ProtoBuf.newBuilder(util.getBuilderOptions(options, "using")),\r
34         loaded = [];\r
35     filenames.forEach(function(filename) {\r
36         var data = binary.load(filename, options, loaded);\r
37         builder["import"](data, filename);\r
38     });\r
39     builder.resolveAll();\r
40     return builder;\r
41 };\r
42 \r
43 /**\r
44  * Module description.\r
45  * @type {string}\r
46  */\r
47 binary.description = description;\r
48 \r
49 binary.exclude = true; // Unfinished\r
50 \r
51 /**\r
52  * Loads a binary descriptor.\r
53  * @param {string} filename Source file\r
54  * @param {!Object.<string,*>} options Options\r
55  * @param {!Array.<string>=} loaded An array of already loaded filenames\r
56  * @returns {*} JSON descriptor\r
57  */\r
58 binary.load = function(filename, options, loaded) {\r
59     filename = node_path.resolve(filename);\r
60     loaded = loaded || [];\r
61     if (loaded.indexOf(filename) >= 0)\r
62         return {};\r
63     var data = fs.readFileSync(filename);\r
64     loaded.push(filename);\r
65     var builder = ProtoBuf.loadProtoFile(node_path.join("..", "..", "..", "src", "google", "protobuf", "descriptor.proto")),\r
66         FileDescriptorSet = builder.build("google.protobuf.FileDescriptorSet");\r
67     var fds = FileDescriptorSet.decode(data),\r
68         imports = [];\r
69     var json = {\r
70         "package": null,\r
71         "imports": imports\r
72     };\r
73     fds.file.forEach(function(fdp) {\r
74         imports.push(buildFileDescriptorProto(fdp));\r
75     });\r
76     return json;\r
77 };\r
78 \r
79 function buildFileDescriptorProto(fdp) {\r
80     var pkg = fdp.package,\r
81         messages = [],\r
82         enums = [],\r
83         services = [],\r
84         extensions = [],\r
85         options = {},\r
86         imports = [];\r
87     fdp.message_type.forEach(function(dp) {\r
88         messages.push(buildMessageDescriptorProto(dp));\r
89     });\r
90     fdp.enum_type.forEach(function(edp) {\r
91         enums.push(buildEnumDescriptorProto(edp));\r
92     });\r
93     fdp.service.forEach(function(sdp) {\r
94         enums.push(buildServiceDescriptorProto(sdp));\r
95     });\r
96     fdp.extension.forEach(function(fdp) {\r
97         extensions.push(buildFieldDescriptorProtoAsExtension(fdp));\r
98     });\r
99     fdp.options.forEach(function(fo) {\r
100         // TODO\r
101     });\r
102     fdp.dependency.forEach(function(filename) {\r
103         // TODO\r
104     });\r
105     return {\r
106         "package": pkg,\r
107         "messages": messages,\r
108         "enums": enums,\r
109         "services": services,\r
110         "extensions": extensions,\r
111         "options": options,\r
112         "imports": imports\r
113     };\r
114 }\r
115 \r
116 function buildMessageDescriptorProto(mdp) {\r
117 \r
118 }\r
119 \r
120 function buildEnumDescriptorProto(edp) {\r
121 \r
122 }\r
123 \r
124 function buildServiceDescriptorProto(sdp) {\r
125 \r
126 }\r
127 \r
128 function buildFieldDescriptorProtoAsExtension(fdp) {\r
129 \r
130 }