wrappers.js
2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"use strict";
/**
* Wrappers for common types.
* @type {Object.<string,IWrapper>}
* @const
*/
var wrappers = exports;
var Message = require("./message");
/**
* From object converter part of an {@link IWrapper}.
* @typedef WrapperFromObjectConverter
* @type {function}
* @param {Object.<string,*>} object Plain object
* @returns {Message<{}>} Message instance
* @this Type
*/
/**
* To object converter part of an {@link IWrapper}.
* @typedef WrapperToObjectConverter
* @type {function}
* @param {Message<{}>} message Message instance
* @param {IConversionOptions} [options] Conversion options
* @returns {Object.<string,*>} Plain object
* @this Type
*/
/**
* Common type wrapper part of {@link wrappers}.
* @interface IWrapper
* @property {WrapperFromObjectConverter} [fromObject] From object converter
* @property {WrapperToObjectConverter} [toObject] To object converter
*/
// Custom wrapper for Any
wrappers[".google.protobuf.Any"] = {
fromObject: function(object) {
// unwrap value type if mapped
if (object && object["@type"]) {
var type = this.lookup(object["@type"]);
/* istanbul ignore else */
if (type) {
// type_url does not accept leading "."
var type_url = object["@type"].charAt(0) === "." ?
object["@type"].substr(1) : object["@type"];
// type_url prefix is optional, but path seperator is required
return this.create({
type_url: "/" + type_url,
value: type.encode(type.fromObject(object)).finish()
});
}
}
return this.fromObject(object);
},
toObject: function(message, options) {
// decode value if requested and unmapped
if (options && options.json && message.type_url && message.value) {
// Only use fully qualified type name after the last '/'
var name = message.type_url.substring(message.type_url.lastIndexOf("/") + 1);
var type = this.lookup(name);
/* istanbul ignore else */
if (type)
message = type.decode(message.value);
}
// wrap value if unmapped
if (!(message instanceof this.ctor) && message instanceof Message) {
var object = message.$type.toObject(message, options);
object["@type"] = message.$type.fullName;
return object;
}
return this.toObject(message, options);
}
};