Skip to content

Commit d7adb09

Browse files
authored
Merge pull request #230 from code0-tech/add-struct-helpers
Add toAllowedValue helper
2 parents 8da5d9d + ec043f2 commit d7adb09

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

build/ts/helpers/shared.struct_helper.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,30 @@ import {Value} from "../pb/shared.struct_pb.js";
22

33
type AllowedValue = null | number | string | boolean | Array<AllowedValue> | object;
44

5+
export function toAllowedValue(value: Value): AllowedValue {
6+
switch (value.kind.oneofKind) {
7+
case "nullValue":
8+
return null;
9+
case "numberValue":
10+
return value.kind.numberValue;
11+
case "stringValue":
12+
return value.kind.stringValue;
13+
case "boolValue":
14+
return value.kind.boolValue;
15+
case "listValue":
16+
return value.kind.listValue.values.map(toAllowedValue);
17+
case "structValue":
18+
const obj: {[key: string]: AllowedValue} = {};
19+
for (const [k, v] of Object.entries(value.kind.structValue.fields)) {
20+
obj[k] = toAllowedValue(v);
21+
}
22+
return obj;
23+
default:
24+
throw new Error(`Unsupported Value kind: ${value.kind.oneofKind}`);
25+
}
26+
}
27+
28+
529
export function constructValue(value: AllowedValue): Value {
630
if (value === null) {
731
return {kind: {oneofKind: "nullValue", nullValue: 0}};
@@ -37,4 +61,4 @@ export function constructValue(value: AllowedValue): Value {
3761
};
3862
}
3963
throw new Error(`Unsupported value type: ${typeof value}`);
40-
}
64+
}

0 commit comments

Comments
 (0)