-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Other: Moved custom wrappers to its own module instead, also makes th…
…e API easier to use manually, see #677
- Loading branch information
Showing
7 changed files
with
165 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
"use strict"; | ||
|
||
/** | ||
* Wrappers for common types. | ||
* @namespace | ||
*/ | ||
var wrappers = exports; | ||
|
||
var util = require("./util/minimal"); | ||
|
||
/** | ||
* From object converter part of a {@link Wrapper}. | ||
* @typedef WrapperFromObjectConverter | ||
* @type {function} | ||
* @param {Object.<string,*>} object Plain object | ||
* @returns {Message<{}>} | ||
* @this Type | ||
*/ | ||
|
||
/** | ||
* To object converter part of a {@link Wrapper}. | ||
* @typedef WrapperToObjectConverter | ||
* @type {function} | ||
* @param {Message<{}>} message Message instance | ||
* @param {ConversionOptions=} options Conversion options | ||
* @returns {Object.<string,*>} | ||
* @this Type | ||
*/ | ||
|
||
/** | ||
* Common type wrapper part of {@link wrappers}. | ||
* @typedef Wrapper | ||
* @type {Object} | ||
* @property {WrapperFromObjectConverter} [fromObject] From object converter | ||
* @property {WrapperToObjectConverter} [toObject] To object converter | ||
*/ | ||
|
||
/** | ||
* Custom wrapper for Any. | ||
* @type {Wrapper} | ||
*/ | ||
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) | ||
return type.fromObject(object); | ||
} | ||
|
||
return this.fromObject(object); | ||
}, | ||
|
||
toObject: function(message, options) { | ||
|
||
// decode value if requested and unmapped | ||
if (options && options.json && message.type_url && message.value) { | ||
var type = this.lookup(message.type_url); | ||
/* istanbul ignore else */ | ||
if (type) | ||
message = type.decode(message.value); | ||
} | ||
|
||
// wrap value if unmapped | ||
if (!(message instanceof this.ctor)) { | ||
var object = message.toObject(options); | ||
object["@type"] = message.$type.fullName; | ||
return object; | ||
} | ||
|
||
return this.toObject(message, options); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
var tape = require("tape"); | ||
|
||
var protobuf = require(".."); | ||
|
||
var root = protobuf.Root.fromJSON({ | ||
nested: { | ||
Foo: { | ||
fields: { | ||
foo: { | ||
id: 1, | ||
type: "google.protobuf.Any" | ||
} | ||
} | ||
}, | ||
Bar: { | ||
fields: { | ||
bar: { | ||
id: 1, | ||
type: "string" | ||
} | ||
} | ||
} | ||
} | ||
}).addJSON(protobuf.common["google/protobuf/any.proto"].nested).resolveAll(); | ||
|
||
var Any = root.lookupType(".google.protobuf.Any"), | ||
Foo = root.lookupType(".Foo"), | ||
Bar = root.lookupType(".Bar"); | ||
|
||
tape.test("google.protobuf.Any", function(test) { | ||
|
||
var foo = Foo.fromObject({ | ||
foo: { | ||
type_url: ".Bar", | ||
value: [1 << 3 | 2, 1, 97] // value = "a" | ||
} | ||
}); | ||
test.ok(foo.foo instanceof Any.ctor, "should keep explicit Any in fromObject"); | ||
test.same(foo.foo, { type_url: ".Bar", value: [10, 1, 97] }, "should keep explicit Any in fromObject properly"); | ||
|
||
var obj = Foo.toObject(foo); | ||
test.same(obj.foo, { type_url: ".Bar", value: [10, 1, 97] }, "should keep explicit Any in toObject properly"); | ||
|
||
obj = Foo.toObject(foo, { json: true }); | ||
test.same(obj.foo, { "@type": ".Bar", bar: "a" }, "should decode explicitly Any in toObject if requested"); | ||
|
||
foo = Foo.fromObject({ | ||
foo: { | ||
"@type": ".Bar", | ||
bar: "a" | ||
} | ||
}); | ||
test.ok(foo.foo instanceof Bar.ctor, "should unwrap wrapped Bar in fromObject"); | ||
test.same(foo.foo, { bar: "a" }, "should unwrap wrapper Bar in fromObject properly"); | ||
|
||
obj = Foo.toObject(foo); | ||
test.same(obj.foo, { "@type": ".Bar", bar: "a" }, "should wrap Bar in toObject properly"); | ||
|
||
test.end(); | ||
}); |