-
-
Notifications
You must be signed in to change notification settings - Fork 699
/
Copy pathflattenAttributes.ts
225 lines (189 loc) · 5.8 KB
/
flattenAttributes.ts
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { Attributes } from "@opentelemetry/api";
import { debug } from "node:util";
export const NULL_SENTINEL = "$@null((";
export const EMPTY_ARRAY_SENTINEL = "$@empty_array((";
export const CIRCULAR_REFERENCE_SENTINEL = "$@circular((";
export function flattenAttributes(
obj: Record<string, unknown> | Array<unknown> | string | boolean | number | null | undefined,
prefix?: string,
seen: WeakSet<object> = new WeakSet()
): Attributes {
const result: Attributes = {};
// Check if obj is null or undefined
if (obj === undefined) {
return result;
}
if (obj === null) {
result[prefix || ""] = NULL_SENTINEL;
return result;
}
if (Array.isArray(obj) && obj.length === 0) {
result[prefix || ""] = EMPTY_ARRAY_SENTINEL;
return result;
}
if (typeof obj === "string") {
result[prefix || ""] = obj;
return result;
}
if (typeof obj === "number") {
result[prefix || ""] = obj;
return result;
}
if (typeof obj === "boolean") {
result[prefix || ""] = obj;
return result;
}
if (obj instanceof Date) {
result[prefix || ""] = obj.toISOString();
return result;
}
// Check for circular reference
if (obj !== null && typeof obj === "object" && seen.has(obj)) {
result[prefix || ""] = CIRCULAR_REFERENCE_SENTINEL;
return result;
}
// Add object to seen set
if (obj !== null && typeof obj === "object") {
seen.add(obj);
}
for (const [key, value] of Object.entries(obj)) {
const newPrefix = `${prefix ? `${prefix}.` : ""}${Array.isArray(obj) ? `[${key}]` : key}`;
if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
if (typeof value[i] === "object" && value[i] !== null) {
// update null check here as well
Object.assign(result, flattenAttributes(value[i], `${newPrefix}.[${i}]`, seen));
} else {
if (value[i] === null) {
result[`${newPrefix}.[${i}]`] = NULL_SENTINEL;
} else {
result[`${newPrefix}.[${i}]`] = value[i];
}
}
}
if (!value.length) {
result[newPrefix] = EMPTY_ARRAY_SENTINEL;
}
} else if (isRecord(value)) {
// update null check here
Object.assign(result, flattenAttributes(value, newPrefix, seen));
} else {
if (typeof value === "number" || typeof value === "string" || typeof value === "boolean") {
result[newPrefix] = value;
} else if (value === null) {
result[newPrefix] = NULL_SENTINEL;
}
}
}
return result;
}
function isRecord(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === "object" && !Array.isArray(value);
}
export function unflattenAttributes(
obj: Attributes
): Record<string, unknown> | string | number | boolean | null | undefined {
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
return obj;
}
if (
typeof obj === "object" &&
obj !== null &&
Object.keys(obj).length === 1 &&
Object.keys(obj)[0] === ""
) {
return rehydrateEmptyValues(obj[""]) as any;
}
if (Object.keys(obj).length === 0) {
return;
}
const result: Record<string, unknown> = {};
for (const [key, value] of Object.entries(obj)) {
const parts = key.split(".").reduce(
(acc, part) => {
if (part.startsWith("[") && part.endsWith("]")) {
// Handle array indices more precisely
const match = part.match(/^\[(\d+)\]$/);
if (match && match[1]) {
acc.push(parseInt(match[1]));
} else {
// Remove brackets for non-numeric array keys
acc.push(part.slice(1, -1));
}
} else {
acc.push(part);
}
return acc;
},
[] as (string | number)[]
);
let current: any = result;
for (let i = 0; i < parts.length - 1; i++) {
const part = parts[i];
const nextPart = parts[i + 1];
if (!part && part !== 0) {
continue;
}
if (typeof nextPart === "number") {
// Ensure we create an array for numeric indices
current[part] = Array.isArray(current[part]) ? current[part] : [];
} else if (current[part] === undefined) {
// Create an object for non-numeric paths
current[part] = {};
}
current = current[part];
}
const lastPart = parts[parts.length - 1];
if (lastPart !== undefined) {
current[lastPart] = rehydrateEmptyValues(rehydrateCircular(value));
}
}
// Convert the result to an array if all top-level keys are numeric indices
if (Object.keys(result).every((k) => /^\d+$/.test(k))) {
const maxIndex = Math.max(...Object.keys(result).map((k) => parseInt(k)));
const arrayResult = Array(maxIndex + 1);
for (const key in result) {
arrayResult[parseInt(key)] = result[key];
}
return arrayResult as any;
}
return result;
}
function rehydrateCircular(value: any): any {
if (value === CIRCULAR_REFERENCE_SENTINEL) {
return "[Circular Reference]";
}
return value;
}
export function primitiveValueOrflattenedAttributes(
obj: Record<string, unknown> | Array<unknown> | string | boolean | number | undefined,
prefix: string | undefined
): Attributes | string | number | boolean | undefined {
if (
typeof obj === "string" ||
typeof obj === "number" ||
typeof obj === "boolean" ||
obj === null ||
obj === undefined
) {
return obj;
}
const attributes = flattenAttributes(obj, prefix);
if (
prefix !== undefined &&
typeof attributes[prefix] !== "undefined" &&
attributes[prefix] !== null
) {
return attributes[prefix] as unknown as Attributes;
}
return attributes;
}
function rehydrateEmptyValues(value: any): any {
if (value === NULL_SENTINEL) {
return null;
}
if (value === EMPTY_ARRAY_SENTINEL) {
return [];
}
return value;
}