Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ function genValuePartial_fromObject(gen, field, fieldIndex, prop) {
} gen
("}");
} else
if (field.resolvedType.fullName === ".google.protobuf.Duration" || field.resolvedType.fullName === ".google.protobuf.Timestamp") { gen
if (field.resolvedType.fullName === ".google.protobuf.Duration" || field.resolvedType.fullName === ".google.protobuf.Timestamp"
|| field.resolvedType.fullName === ".google.protobuf.Value"
) { gen
("m%s=types[%i].fromObject(d%s)", prop, fieldIndex, prop);
} else gen
("if(typeof d%s!==\"object\")", prop)
Expand Down
26 changes: 19 additions & 7 deletions src/util/is-legacy-struct.js → src/util/is-legacy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
"use strict";
module.exports = isLegacyStruct;
module.exports = {
isLegacyStruct,
isLegacyValue
};


/**
* Checks if the given payload is in the legacy value format.
*
* @param {object} payload The payload to check for legacy value format
* @returns {boolean} True if the value is in legacy format, false otherwise
*/
function isLegacyValue(payload) {
const valueKeysSet = new Set(["string_value", "number_value", "bool_value", "struct_value", "list_value", "null_value"]);

return payload && typeof payload === "object" && Object.keys(payload).length === 1 && valueKeysSet.has(Object.keys(payload)[0]);
}

/**
* Identifies where the payload for a struct is in the form of a legacy struct.
Expand Down Expand Up @@ -32,8 +48,7 @@ function isLegacyStruct(payload) {
if (payload && Object.keys(payload).length === 1 && payload.fields && typeof payload.fields === "object") {
if (Array.isArray(payload.fields)) {
return payload.fields.every(field => Object.keys(field).length === 2 &&
field.key && field.value && Object.keys(field.value).length === 1
&& valueKeysSet.has(Object.keys(field.value)[0]));
field.key && isLegacyValue(field.value));
}

// Get all the values of the fields object
Expand All @@ -42,10 +57,7 @@ function isLegacyStruct(payload) {
const fieldValues = Object.values(payload.fields);

// Check if all the fieldValues have only one key and that key is a valid value type
if (fieldValues.every(fieldValue => {
const fieldValueKeys = fieldValue ? Object.keys(fieldValue) : [];
return fieldValueKeys.length === 1 && valueKeysSet.has(fieldValueKeys[0]);
})) {
if (fieldValues.every(fieldValue => isLegacyValue(fieldValue))) {
return true;
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/wrappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
var wrappers = exports;

var Message = require("./message");
var isLegacyStruct = require("./util/is-legacy-struct");
var { isLegacyValue, isLegacyStruct } = require("./util/is-legacy");
var util = require("./util");

/**
Expand Down Expand Up @@ -157,6 +157,10 @@ wrappers[".google.protobuf.Value"] = {
// If already a Value instance, return as is
if (object instanceof this.ctor) return object;

if (isLegacyValue(object)) {
return this.create(object);
}

// Handle different types and convert to appropriate Value field
if (object === null || object === undefined) {
return this.create({ null_value: 0 });
Expand Down