Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / protobufjs / src / message.js
1 "use strict";
2 module.exports = Message;
3
4 var util = require("./util/minimal");
5
6 /**
7  * Constructs a new message instance.
8  * @classdesc Abstract runtime message.
9  * @constructor
10  * @param {Properties<T>} [properties] Properties to set
11  * @template T extends object = object
12  */
13 function Message(properties) {
14     // not used internally
15     if (properties)
16         for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)
17             this[keys[i]] = properties[keys[i]];
18 }
19
20 /**
21  * Reference to the reflected type.
22  * @name Message.$type
23  * @type {Type}
24  * @readonly
25  */
26
27 /**
28  * Reference to the reflected type.
29  * @name Message#$type
30  * @type {Type}
31  * @readonly
32  */
33
34 /*eslint-disable valid-jsdoc*/
35
36 /**
37  * Creates a new message of this type using the specified properties.
38  * @param {Object.<string,*>} [properties] Properties to set
39  * @returns {Message<T>} Message instance
40  * @template T extends Message<T>
41  * @this Constructor<T>
42  */
43 Message.create = function create(properties) {
44     return this.$type.create(properties);
45 };
46
47 /**
48  * Encodes a message of this type.
49  * @param {T|Object.<string,*>} message Message to encode
50  * @param {Writer} [writer] Writer to use
51  * @returns {Writer} Writer
52  * @template T extends Message<T>
53  * @this Constructor<T>
54  */
55 Message.encode = function encode(message, writer) {
56     return this.$type.encode(message, writer);
57 };
58
59 /**
60  * Encodes a message of this type preceeded by its length as a varint.
61  * @param {T|Object.<string,*>} message Message to encode
62  * @param {Writer} [writer] Writer to use
63  * @returns {Writer} Writer
64  * @template T extends Message<T>
65  * @this Constructor<T>
66  */
67 Message.encodeDelimited = function encodeDelimited(message, writer) {
68     return this.$type.encodeDelimited(message, writer);
69 };
70
71 /**
72  * Decodes a message of this type.
73  * @name Message.decode
74  * @function
75  * @param {Reader|Uint8Array} reader Reader or buffer to decode
76  * @returns {T} Decoded message
77  * @template T extends Message<T>
78  * @this Constructor<T>
79  */
80 Message.decode = function decode(reader) {
81     return this.$type.decode(reader);
82 };
83
84 /**
85  * Decodes a message of this type preceeded by its length as a varint.
86  * @name Message.decodeDelimited
87  * @function
88  * @param {Reader|Uint8Array} reader Reader or buffer to decode
89  * @returns {T} Decoded message
90  * @template T extends Message<T>
91  * @this Constructor<T>
92  */
93 Message.decodeDelimited = function decodeDelimited(reader) {
94     return this.$type.decodeDelimited(reader);
95 };
96
97 /**
98  * Verifies a message of this type.
99  * @name Message.verify
100  * @function
101  * @param {Object.<string,*>} message Plain object to verify
102  * @returns {string|null} `null` if valid, otherwise the reason why it is not
103  */
104 Message.verify = function verify(message) {
105     return this.$type.verify(message);
106 };
107
108 /**
109  * Creates a new message of this type from a plain object. Also converts values to their respective internal types.
110  * @param {Object.<string,*>} object Plain object
111  * @returns {T} Message instance
112  * @template T extends Message<T>
113  * @this Constructor<T>
114  */
115 Message.fromObject = function fromObject(object) {
116     return this.$type.fromObject(object);
117 };
118
119 /**
120  * Creates a plain object from a message of this type. Also converts values to other types if specified.
121  * @param {T} message Message instance
122  * @param {IConversionOptions} [options] Conversion options
123  * @returns {Object.<string,*>} Plain object
124  * @template T extends Message<T>
125  * @this Constructor<T>
126  */
127 Message.toObject = function toObject(message, options) {
128     return this.$type.toObject(message, options);
129 };
130
131 /**
132  * Converts this message to JSON.
133  * @returns {Object.<string,*>} JSON object
134  */
135 Message.prototype.toJSON = function toJSON() {
136     return this.$type.toObject(this, util.toJSONOptions);
137 };
138
139 /*eslint-enable valid-jsdoc*/