blob: 54247e3fa119280a14a7f7b6557ab631de4bed72 [file] [log] [blame]
murgatroid99749666e2015-01-12 18:25:58 -08001/*
2 *
3 * Copyright 2014, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
murgatroid9953116ff2015-01-16 14:22:58 -080034var capitalize = require('underscore.string/capitalize');
murgatroid99fd81e702015-01-16 14:22:14 -080035
murgatroid99e5061512015-01-12 18:14:35 -080036/**
murgatroid99cca5ffa2015-01-15 14:06:56 -080037 * Get a function that deserializes a specific type of protobuf.
38 * @param {function()} cls The constructor of the message type to deserialize
39 * @return {function(Buffer):cls} The deserialization function
murgatroid99e5061512015-01-12 18:14:35 -080040 */
murgatroid99cca5ffa2015-01-15 14:06:56 -080041function deserializeCls(cls) {
42 /**
43 * Deserialize a buffer to a message object
44 * @param {Buffer} arg_buf The buffer to deserialize
45 * @return {cls} The resulting object
46 */
47 return function deserialize(arg_buf) {
48 return cls.decode(arg_buf);
49 };
murgatroid99e5061512015-01-12 18:14:35 -080050}
51
murgatroid99cca5ffa2015-01-15 14:06:56 -080052/**
53 * Get a function that serializes objects to a buffer by protobuf class.
54 * @param {function()} Cls The constructor of the message type to serialize
55 * @return {function(Cls):Buffer} The serialization function
56 */
57function serializeCls(Cls) {
58 /**
59 * Serialize an object to a Buffer
60 * @param {Object} arg The object to serialize
61 * @return {Buffer} The serialized object
62 */
63 return function serialize(arg) {
64 return new Buffer(new Cls(arg).encode().toBuffer());
65 };
66}
67
68/**
69 * Get the fully qualified (dotted) name of a ProtoBuf.Reflect value.
70 * @param {ProtoBuf.Reflect.Namespace} value The value to get the name of
71 * @return {string} The fully qualified name of the value
72 */
73function fullyQualifiedName(value) {
74 if (value === null || value === undefined) {
75 return '';
76 }
77 var name = value.name;
murgatroid99fd81e702015-01-16 14:22:14 -080078 if (value.className === 'Service.RPCMethod') {
murgatroid9953116ff2015-01-16 14:22:58 -080079 name = capitalize(name);
murgatroid99fd81e702015-01-16 14:22:14 -080080 }
murgatroid99cca5ffa2015-01-15 14:06:56 -080081 if (value.hasOwnProperty('parent')) {
82 var parent_name = fullyQualifiedName(value.parent);
83 if (parent_name !== '') {
84 name = parent_name + '.' + name;
85 }
86 }
87 return name;
88}
89
90/**
91 * See docs for deserializeCls
92 */
93exports.deserializeCls = deserializeCls;
94
95/**
96 * See docs for serializeCls
97 */
98exports.serializeCls = serializeCls;
99
100/**
101 * See docs for fullyQualifiedName
102 */
103exports.fullyQualifiedName = fullyQualifiedName;