11"use strict" ;
2- module . exports = isLegacyStruct ;
2+ module . exports = {
3+ isLegacyStruct,
4+ isLegacyValue
5+ } ;
6+
7+ const valueKeysSet = new Set ( [ "string_value" , "number_value" , "bool_value" , "struct_value" , "list_value" , "null_value" ] ) ;
8+
9+ /**
10+ * Checks if the given payload is in the legacy value format.
11+ *
12+ * @param {object } payload The payload to check for legacy value format
13+ * @returns {boolean } True if the value is in legacy format, false otherwise
14+ */
15+ function isLegacyValue ( payload ) {
16+ return payload && typeof payload === "object" && Object . keys ( payload ) . length === 1 && valueKeysSet . has ( Object . keys ( payload ) [ 0 ] ) ;
17+ }
318
419/**
520 * Identifies where the payload for a struct is in the form of a legacy struct.
@@ -25,15 +40,11 @@ module.exports = isLegacyStruct;
2540 * @returns {boolean } True if the payload is in legacy struct format, false otherwise
2641 */
2742function isLegacyStruct ( payload ) {
28- // Value types in a struct
29- const valueKeysSet = new Set ( [ "string_value" , "number_value" , "bool_value" , "struct_value" , "list_value" , "null_value" ] ) ;
30-
3143 // If object has only one key and that key is "fields" which is an object
3244 if ( payload && Object . keys ( payload ) . length === 1 && payload . fields && typeof payload . fields === "object" ) {
3345 if ( Array . isArray ( payload . fields ) ) {
3446 return payload . fields . every ( field => Object . keys ( field ) . length === 2 &&
35- field . key && field . value && Object . keys ( field . value ) . length === 1
36- && valueKeysSet . has ( Object . keys ( field . value ) [ 0 ] ) ) ;
47+ field . key && isLegacyValue ( field . value ) ) ;
3748 }
3849
3950 // Get all the values of the fields object
@@ -42,10 +53,7 @@ function isLegacyStruct(payload) {
4253 const fieldValues = Object . values ( payload . fields ) ;
4354
4455 // Check if all the fieldValues have only one key and that key is a valid value type
45- if ( fieldValues . every ( fieldValue => {
46- const fieldValueKeys = fieldValue ? Object . keys ( fieldValue ) : [ ] ;
47- return fieldValueKeys . length === 1 && valueKeysSet . has ( fieldValueKeys [ 0 ] ) ;
48- } ) ) {
56+ if ( fieldValues . every ( fieldValue => isLegacyValue ( fieldValue ) ) ) {
4957 return true ;
5058 }
5159 }
0 commit comments