diff --git a/src/parsing/v2/field/objectField.ts b/src/parsing/v2/field/objectField.ts index 20dc0e6af..d94c69697 100644 --- a/src/parsing/v2/field/objectField.ts +++ b/src/parsing/v2/field/objectField.ts @@ -2,22 +2,108 @@ import { InferenceFields } from "./inferenceFields"; import { StringDict } from "../../common"; import { BaseField } from "./baseField"; import type { SimpleField } from "./simpleField"; +import type { ListField } from "./listField"; export class ObjectField extends BaseField { readonly fields: InferenceFields; + /** + * Retrieves the simple sub-fields in the object. + * + * @return {Map} A map of field names to their corresponding `SimpleField` instances. + */ public get simpleFields(): Map { const result: Map = new Map(); for (const [fieldName, fieldValue] of this.fields) { if (fieldValue.constructor.name === "SimpleField") { result.set(fieldName, fieldValue as SimpleField); - } else { - throw new Error(`The field '${fieldName}' is not a SimpleField.`); } } return result; } + /** + * Retrieves the list sub-fields in the object. + * + * @return {Map} A map of field names to their corresponding `ListField` instances. + */ + public get listFields(): Map { + const result: Map = new Map(); + for (const [fieldName, fieldValue] of this.fields) { + if (fieldValue.constructor.name === "ListField") { + result.set(fieldName, fieldValue as ListField); + } + } + return result; + } + + /** + * Retrieves the object sub-fields in the object. + * + * @return {Map} A map of field names to their corresponding `ObjectField` instances. + */ + public get objectFields(): Map { + const result: Map = new Map(); + for (const [fieldName, fieldValue] of this.fields) { + if (fieldValue.constructor.name === "ObjectField") { + result.set(fieldName, fieldValue as ObjectField); + } + } + return result; + } + + /** + * Retrieves a SimpleField by its name if it exists and is of the correct type. + * + * @param {string} fieldName - The name of the field to retrieve. + * @return {SimpleField} The SimpleField instance if it exists and is valid, or undefined if not. + * @throws {Error} If the field does not exist or is not of type SimpleField. + */ + public getSimpleField(fieldName: string): SimpleField { + if (!this.fields.has(fieldName) && !this.simpleFields.has(fieldName)) { + throw new Error(`The field '${fieldName}' was not found.`); + } + if (this.fields.get(fieldName)?.constructor.name !== "SimpleField") { + throw new Error(`The field '${fieldName}' is not a SimpleField.`); + } + return this.simpleFields.get(fieldName) as SimpleField; + } + + /** + * Retrieves a ListField by its name if it exists and is of the correct type. + * + * @param {string} fieldName - The name of the field to retrieve. + * @return {ListField} The ListField instance if it exists and is valid, or undefined if not. + * @throws {Error} If the field does not exist or is not of type ListField. + */ + public getListField(fieldName: string): ListField { + if (!this.fields.has(fieldName) || !this.listFields.has(fieldName)) { + throw new Error(`The field '${fieldName}' was not found.`); + } + if (this.fields.get(fieldName)?.constructor.name !== "ListField") { + throw new Error(`The field '${fieldName}' is not a ListField.`); + } + return this.listFields.get(fieldName) as ListField; + } + + + /** + * Retrieves an ObjectField by its name if it exists and is of the correct type. + * + * @param {string} fieldName - The name of the field to retrieve. + * @return {ObjectField} The ObjectField instance if it exists and is valid, or undefined if not. + * @throws {Error} If the field does not exist or is not of type ObjectField. + */ + public getObjectField(fieldName: string): ObjectField { + if (!this.fields.has(fieldName) && !this.objectFields.has(fieldName)) { + throw new Error(`The field '${fieldName}' was not found.`); + } + if (this.fields.get(fieldName)?.constructor.name !== "ObjectField") { + throw new Error(`The field '${fieldName}' is not an ObjectField.`); + } + return this.objectFields.get(fieldName) as ObjectField; + } + constructor(serverResponse: StringDict, indentLevel = 0) { super(serverResponse, indentLevel);