murgatroid99 | 749666e | 2015-01-12 18:25:58 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
murgatroid99 | 53116ff | 2015-01-16 14:22:58 -0800 | [diff] [blame] | 34 | var capitalize = require('underscore.string/capitalize'); |
murgatroid99 | fd81e70 | 2015-01-16 14:22:14 -0800 | [diff] [blame] | 35 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 36 | /** |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 37 | * 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 |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 40 | */ |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 41 | function 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 | }; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 50 | } |
| 51 | |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 52 | /** |
| 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 | */ |
| 57 | function 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 | */ |
| 73 | function fullyQualifiedName(value) { |
| 74 | if (value === null || value === undefined) { |
| 75 | return ''; |
| 76 | } |
| 77 | var name = value.name; |
murgatroid99 | fd81e70 | 2015-01-16 14:22:14 -0800 | [diff] [blame] | 78 | if (value.className === 'Service.RPCMethod') { |
murgatroid99 | 53116ff | 2015-01-16 14:22:58 -0800 | [diff] [blame] | 79 | name = capitalize(name); |
murgatroid99 | fd81e70 | 2015-01-16 14:22:14 -0800 | [diff] [blame] | 80 | } |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 81 | 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 | */ |
| 93 | exports.deserializeCls = deserializeCls; |
| 94 | |
| 95 | /** |
| 96 | * See docs for serializeCls |
| 97 | */ |
| 98 | exports.serializeCls = serializeCls; |
| 99 | |
| 100 | /** |
| 101 | * See docs for fullyQualifiedName |
| 102 | */ |
| 103 | exports.fullyQualifiedName = fullyQualifiedName; |