@@ -2,22 +2,108 @@ import { InferenceFields } from "./inferenceFields";
22import { StringDict } from "../../common" ;
33import { BaseField } from "./baseField" ;
44import type { SimpleField } from "./simpleField" ;
5+ import type { ListField } from "./listField" ;
56
67export class ObjectField extends BaseField {
78 readonly fields : InferenceFields ;
89
10+ /**
11+ * Retrieves the simple sub-fields in the object.
12+ *
13+ * @return {Map<string, SimpleField> } A map of field names to their corresponding `SimpleField` instances.
14+ */
915 public get simpleFields ( ) : Map < string , SimpleField > {
1016 const result : Map < string , SimpleField > = new Map ( ) ;
1117 for ( const [ fieldName , fieldValue ] of this . fields ) {
1218 if ( fieldValue . constructor . name === "SimpleField" ) {
1319 result . set ( fieldName , fieldValue as SimpleField ) ;
14- } else {
15- throw new Error ( `The field '${ fieldName } ' is not a SimpleField.` ) ;
1620 }
1721 }
1822 return result ;
1923 }
2024
25+ /**
26+ * Retrieves the list sub-fields in the object.
27+ *
28+ * @return {Map<string, ListField> } A map of field names to their corresponding `ListField` instances.
29+ */
30+ public get listFields ( ) : Map < string , ListField > {
31+ const result : Map < string , ListField > = new Map ( ) ;
32+ for ( const [ fieldName , fieldValue ] of this . fields ) {
33+ if ( fieldValue . constructor . name === "ListField" ) {
34+ result . set ( fieldName , fieldValue as ListField ) ;
35+ }
36+ }
37+ return result ;
38+ }
39+
40+ /**
41+ * Retrieves the object sub-fields in the object.
42+ *
43+ * @return {Map<string, ObjectField> } A map of field names to their corresponding `ObjectField` instances.
44+ */
45+ public get objectFields ( ) : Map < string , ObjectField > {
46+ const result : Map < string , ObjectField > = new Map ( ) ;
47+ for ( const [ fieldName , fieldValue ] of this . fields ) {
48+ if ( fieldValue . constructor . name === "ObjectField" ) {
49+ result . set ( fieldName , fieldValue as ObjectField ) ;
50+ }
51+ }
52+ return result ;
53+ }
54+
55+ /**
56+ * Retrieves a SimpleField by its name if it exists and is of the correct type.
57+ *
58+ * @param {string } fieldName - The name of the field to retrieve.
59+ * @return {SimpleField } The SimpleField instance if it exists and is valid, or undefined if not.
60+ * @throws {Error } If the field does not exist or is not of type SimpleField.
61+ */
62+ public getSimpleField ( fieldName : string ) : SimpleField {
63+ if ( ! this . fields . has ( fieldName ) && ! this . simpleFields . has ( fieldName ) ) {
64+ throw new Error ( `The field '${ fieldName } ' was not found.` ) ;
65+ }
66+ if ( this . fields . get ( fieldName ) ?. constructor . name !== "SimpleField" ) {
67+ throw new Error ( `The field '${ fieldName } ' is not a SimpleField.` ) ;
68+ }
69+ return this . simpleFields . get ( fieldName ) as SimpleField ;
70+ }
71+
72+ /**
73+ * Retrieves a ListField by its name if it exists and is of the correct type.
74+ *
75+ * @param {string } fieldName - The name of the field to retrieve.
76+ * @return {ListField } The ListField instance if it exists and is valid, or undefined if not.
77+ * @throws {Error } If the field does not exist or is not of type ListField.
78+ */
79+ public getListField ( fieldName : string ) : ListField {
80+ if ( ! this . fields . has ( fieldName ) || ! this . listFields . has ( fieldName ) ) {
81+ throw new Error ( `The field '${ fieldName } ' was not found.` ) ;
82+ }
83+ if ( this . fields . get ( fieldName ) ?. constructor . name !== "ListField" ) {
84+ throw new Error ( `The field '${ fieldName } ' is not a ListField.` ) ;
85+ }
86+ return this . listFields . get ( fieldName ) as ListField ;
87+ }
88+
89+
90+ /**
91+ * Retrieves an ObjectField by its name if it exists and is of the correct type.
92+ *
93+ * @param {string } fieldName - The name of the field to retrieve.
94+ * @return {ObjectField } The ObjectField instance if it exists and is valid, or undefined if not.
95+ * @throws {Error } If the field does not exist or is not of type ObjectField.
96+ */
97+ public getObjectField ( fieldName : string ) : ObjectField {
98+ if ( ! this . fields . has ( fieldName ) && ! this . objectFields . has ( fieldName ) ) {
99+ throw new Error ( `The field '${ fieldName } ' was not found.` ) ;
100+ }
101+ if ( this . fields . get ( fieldName ) ?. constructor . name !== "ObjectField" ) {
102+ throw new Error ( `The field '${ fieldName } ' is not an ObjectField.` ) ;
103+ }
104+ return this . objectFields . get ( fieldName ) as ObjectField ;
105+ }
106+
21107 constructor ( serverResponse : StringDict , indentLevel = 0 ) {
22108 super ( serverResponse , indentLevel ) ;
23109
0 commit comments