From 23dcf962e7a08b6d69a9e009b3bc41510fdc6d5b Mon Sep 17 00:00:00 2001 From: sebastianMindee <130448732+sebastianMindee@users.noreply.github.com> Date: Wed, 11 Feb 2026 10:39:17 +0100 Subject: [PATCH 1/2] :sparkles: add convenience accessors for ObjectFields --- src/parsing/v2/field/objectField.ts | 54 +++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/src/parsing/v2/field/objectField.ts b/src/parsing/v2/field/objectField.ts index 20dc0e6af..6cfc6a776 100644 --- a/src/parsing/v2/field/objectField.ts +++ b/src/parsing/v2/field/objectField.ts @@ -2,6 +2,7 @@ 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; @@ -11,13 +12,62 @@ export class ObjectField extends BaseField { 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; } + 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; + } + + 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; + } + + public getSimpleField(fieldName: string): SimpleField | undefined { + if (!this.fields.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); + } + + public getListField(fieldName: string): ListField | undefined { + if (!this.fields.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); + } + + + public getObjectField(fieldName: string): ObjectField | undefined { + if (!this.fields.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); + } + constructor(serverResponse: StringDict, indentLevel = 0) { super(serverResponse, indentLevel); From 4d72803b76523ef53320d8d983248e19433b93ec Mon Sep 17 00:00:00 2001 From: sebastianMindee <130448732+sebastianMindee@users.noreply.github.com> Date: Wed, 11 Feb 2026 11:23:44 +0100 Subject: [PATCH 2/2] add docstrings + fix typing issues --- src/parsing/v2/field/objectField.ts | 54 ++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/src/parsing/v2/field/objectField.ts b/src/parsing/v2/field/objectField.ts index 6cfc6a776..d94c69697 100644 --- a/src/parsing/v2/field/objectField.ts +++ b/src/parsing/v2/field/objectField.ts @@ -7,6 +7,11 @@ 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) { @@ -17,6 +22,11 @@ export class ObjectField extends BaseField { 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) { @@ -27,6 +37,11 @@ export class ObjectField extends BaseField { 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) { @@ -37,35 +52,56 @@ export class ObjectField extends BaseField { return result; } - public getSimpleField(fieldName: string): SimpleField | undefined { - if (!this.fields.has(fieldName)) { + /** + * 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); + return this.simpleFields.get(fieldName) as SimpleField; } - public getListField(fieldName: string): ListField | undefined { - if (!this.fields.has(fieldName)) { + /** + * 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); + return this.listFields.get(fieldName) as ListField; } - public getObjectField(fieldName: string): ObjectField | undefined { - if (!this.fields.has(fieldName)) { + /** + * 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); + return this.objectFields.get(fieldName) as ObjectField; } constructor(serverResponse: StringDict, indentLevel = 0) {