Skip to content

Commit 23dcf96

Browse files
✨ add convenience accessors for ObjectFields
1 parent a61360e commit 23dcf96

1 file changed

Lines changed: 52 additions & 2 deletions

File tree

src/parsing/v2/field/objectField.ts

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { InferenceFields } from "./inferenceFields";
22
import { StringDict } from "../../common";
33
import { BaseField } from "./baseField";
44
import type { SimpleField } from "./simpleField";
5+
import type { ListField } from "./listField";
56

67
export class ObjectField extends BaseField {
78
readonly fields: InferenceFields;
@@ -11,13 +12,62 @@ export class ObjectField extends BaseField {
1112
for (const [fieldName, fieldValue] of this.fields) {
1213
if (fieldValue.constructor.name === "SimpleField") {
1314
result.set(fieldName, fieldValue as SimpleField);
14-
} else {
15-
throw new Error(`The field '${fieldName}' is not a SimpleField.`);
1615
}
1716
}
1817
return result;
1918
}
2019

20+
public get listFields(): Map<string, ListField> {
21+
const result: Map<string, ListField> = new Map();
22+
for (const [fieldName, fieldValue] of this.fields) {
23+
if (fieldValue.constructor.name === "ListField") {
24+
result.set(fieldName, fieldValue as ListField);
25+
}
26+
}
27+
return result;
28+
}
29+
30+
public get objectFields(): Map<string, ObjectField> {
31+
const result: Map<string, ObjectField> = new Map();
32+
for (const [fieldName, fieldValue] of this.fields) {
33+
if (fieldValue.constructor.name === "ObjectField") {
34+
result.set(fieldName, fieldValue as ObjectField);
35+
}
36+
}
37+
return result;
38+
}
39+
40+
public getSimpleField(fieldName: string): SimpleField | undefined {
41+
if (!this.fields.has(fieldName)) {
42+
throw new Error(`The field '${fieldName}' was not found.`);
43+
}
44+
if (this.fields.get(fieldName)?.constructor.name !== "SimpleField") {
45+
throw new Error(`The field '${fieldName}' is not a SimpleField.`);
46+
}
47+
return this.simpleFields.get(fieldName);
48+
}
49+
50+
public getListField(fieldName: string): ListField | undefined {
51+
if (!this.fields.has(fieldName)) {
52+
throw new Error(`The field '${fieldName}' was not found.`);
53+
}
54+
if (this.fields.get(fieldName)?.constructor.name !== "ListField") {
55+
throw new Error(`The field '${fieldName}' is not a ListField.`);
56+
}
57+
return this.listFields.get(fieldName);
58+
}
59+
60+
61+
public getObjectField(fieldName: string): ObjectField | undefined {
62+
if (!this.fields.has(fieldName)) {
63+
throw new Error(`The field '${fieldName}' was not found.`);
64+
}
65+
if (this.fields.get(fieldName)?.constructor.name !== "ObjectField") {
66+
throw new Error(`The field '${fieldName}' is not an ObjectField.`);
67+
}
68+
return this.objectFields.get(fieldName);
69+
}
70+
2171
constructor(serverResponse: StringDict, indentLevel = 0) {
2272
super(serverResponse, indentLevel);
2373

0 commit comments

Comments
 (0)