Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"google-auth-library": "^9.15.1",
"google-gax": "^5.0.1",
"long": "^4.0.0",
"map-obj": "^4.0.0",
"stream-json": "^1.8.0"
},
"devDependencies": {
Expand Down
52 changes: 35 additions & 17 deletions src/parserRest.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* JSON Rest parsing
*/

import mapObject from "map-obj";
import { parse } from "circ-json";

import { toSnakeCase } from "./utils";
Expand All @@ -25,23 +20,46 @@ export const decamelizeKeys = (input: any) => {
return input;
}

const makeMapper = (parentPath?: string) => (key: string, value: any) => {
key = cachedDecamelize(key);
return transform(input);

if (isObject(value)) {
const path = parentPath === undefined ? key : `${parentPath}.${key}`;
function transform(value: any, parentPath?: string): any {
if (!isObject(value)) {
return value;
}

// @ts-ignore
value = mapObject(value, makeMapper(path));
} else {
value = cachedValueParser(key, parentPath, value);
if (Array.isArray(value)) {
const length = value.length;
const result = new Array(length);

for (let i = 0; i < length; i += 1) {
const item = value[i];
result[i] = isObject(item) ? transformObject(item, parentPath) : item;
Comment on lines +30 to +36

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Parse primitive array elements via cachedValueParser

The new array handling returns primitive items unchanged (result[i] = … : item), so repeated scalar fields never flow through cachedValueParser. Any array of enums/INT64 values (e.g., metrics.interaction_event_types or other repeated primitives) will now be left as raw strings instead of being converted to their numeric/typed form because the type lookup is skipped entirely for each element. The previous implementation passed every array element through cachedValueParser, so this is a regression in parsing repeated primitive fields.

Useful? React with 👍 / 👎.

}

return result;
}

return [key, value];
};
return transformObject(value, parentPath);
}

// @ts-ignore
return mapObject(input, makeMapper());
function transformObject(obj: Record<string, any>, parentPath?: string) {
const output: Record<string, any> = {};

for (const key of Object.keys(obj)) {
const rawValue = obj[key];
const newKey = cachedDecamelize(key);

if (isObject(rawValue)) {
const nextParentPath =
parentPath === undefined ? newKey : `${parentPath}.${newKey}`;
output[newKey] = transform(rawValue, nextParentPath);
} else {
output[newKey] = cachedValueParser(newKey, parentPath, rawValue);
}
}

return output;
}
};

const cachedDecamelize = (key: string) => {
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3040,11 +3040,6 @@ makeerror@1.0.12:
dependencies:
tmpl "1.0.5"

map-obj@^4.0.0:
version "4.3.0"
resolved "https://registry.npmjs.org/map-obj/-/map-obj-4.3.0.tgz"
integrity sha512-hdN1wVrZbb29eBGiGjJbeP8JbKjq1urkHJ/LIP/NY48MZ1QVXUsQBV1G1zvYFHn1XE06cwjBsOI2K3Ulnj1YXQ==

math-intrinsics@^1.1.0:
version "1.1.0"
resolved "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz"
Expand Down
Loading