1- import { DataConnectError } from '../core/error' ;
2- import { BackingDataObject , FDCScalarValue } from './BackingDataObject' ;
1+ import { DataConnectError } from '../api' ;
2+
3+ import { BackingDataObject , BackingDataObjectJson , FDCScalarValue } from './BackingDataObject' ;
34import { CacheProvider } from './CacheProvider' ;
45import { ImpactedQueryRefsAccumulator } from './ImpactedQueryRefsAccumulator' ;
56
@@ -20,7 +21,6 @@ export class StubDataObject {
2021 if ( typeof values === 'undefined' && typeof cacheProvider === 'undefined' && typeof acc === 'undefined' ) {
2122 return ;
2223 }
23- // TODO: validate that all other fields have been passed in
2424 // TODO: validate that all other fields have been passed in.
2525 if ( typeof values !== 'object' || Array . isArray ( values ) ) {
2626 throw new DataConnectError (
@@ -46,11 +46,8 @@ export class StubDataObject {
4646 }
4747 if ( typeof values [ key ] === 'object' ) {
4848 if ( Array . isArray ( values [ key ] ) ) {
49- if ( ! this . objectLists [ key ] ) {
50- this . objectLists [ key ] = [ ] ;
51- }
5249 const objArray : StubDataObject [ ] = [ ] ;
53- const scalarArray : NonNullable < FDCScalarValue > [ ] = [ ] ;
50+ const scalarArray : Array < NonNullable < FDCScalarValue > > = [ ] ;
5451 for ( const value of values [ key ] ) {
5552 if ( typeof value === 'object' ) {
5653 if ( Array . isArray ( value ) ) {
@@ -117,6 +114,7 @@ export class StubDataObject {
117114 resultObject [ key ] = this . backingData [ key ] ;
118115 }
119116 }
117+ // Scalars should never have stubdataobjects
120118 for ( const key in this . scalars ) {
121119 if ( this . scalars . hasOwnProperty ( key ) ) {
122120 resultObject [ key ] = this . scalars [ key ] ;
@@ -129,37 +127,41 @@ export class StubDataObject {
129127 }
130128 for ( const key in this . objectLists ) {
131129 if ( this . objectLists . hasOwnProperty ( key ) ) {
132- resultObject [ key ] = this . objectLists [ key ] . map ( obj => obj . toJson ( ) ) ;
130+ resultObject [ key ] = this . objectLists [ key ] . map ( obj => {
131+ return obj . toJson ( ) ;
132+ } ) ;
133133 }
134134 }
135135 return resultObject ;
136136 }
137- static parseMap ( map : { [ key : string ] : any } ) : typeof map {
137+ static parseMap ( map : { [ key : string ] : StubDataObject | StubDataObject [ ] | FDCScalarValue } , isSdo = false ) : typeof map {
138138 const newMap : typeof map = { } ;
139139 for ( const key in map ) {
140140 if ( map . hasOwnProperty ( key ) ) {
141141 if ( Array . isArray ( map [ key ] ) ) {
142- newMap [ key ] = map [ key ] . map ( value => StubDataObject . fromStorableJson ( value ) ) ;
142+ newMap [ key ] = map [ key ] . map ( value => isSdo ? StubDataObject . fromStorableJson ( value ) : value ) ;
143143 } else {
144- newMap [ key ] = StubDataObject . fromStorableJson ( map [ key ] ) ;
144+ newMap [ key ] = isSdo ? StubDataObject . fromStorableJson ( map [ key ] as StubDataObjectJson ) : map [ key ] ;
145145 }
146146 }
147147 }
148148 return newMap ;
149149 }
150- static fromStorableJson ( obj : any ) : StubDataObject {
150+ static fromStorableJson ( obj : StubDataObjectJson ) : StubDataObject {
151151 const sdo = new StubDataObject ( ) ;
152- // TODO: implement this.
153- sdo . backingData = BackingDataObject . fromStorableJson ( obj . backingData ) ;
152+ if ( obj . backingData ) {
153+ sdo . backingData = BackingDataObject . fromStorableJson ( obj . backingData ) ;
154+ }
154155 sdo . acc = new ImpactedQueryRefsAccumulator ( ) ;
155156 sdo . globalId = obj . globalID ;
156157 sdo . impactedQueryRefs = new Set < string > ( ) ;
157158 sdo . scalars = this . parseMap ( obj . scalars ) ;
158- sdo . references = this . parseMap ( obj . references ) ;
159+ sdo . references = this . parseMap ( obj . references ) as typeof sdo . references ;
160+ sdo . objectLists = this . parseMap ( obj . objectLists , true ) as typeof sdo . objectLists ;
159161 return sdo ;
160162 }
161- getStorableMap ( map : { [ key : string ] : any } ) {
162- const newMap : typeof map = { } ;
163+ getStorableMap ( map : { [ key : string ] : StubDataObject | StubDataObject [ ] } ) : { [ key : string ] : StubDataObjectJson | StubDataObjectJson [ ] } {
164+ const newMap : { [ key : string ] : StubDataObjectJson | StubDataObjectJson [ ] } = { } ;
163165 for ( const key in map ) {
164166 if ( map . hasOwnProperty ( key ) ) {
165167 if ( Array . isArray ( map [ key ] ) ) {
@@ -171,14 +173,26 @@ export class StubDataObject {
171173 }
172174 return newMap ;
173175 }
174- toStorableJson ( ) : object {
175- // TODO: replace all `any` types.
176- const obj : any = { } ;
177- obj . backingData = this . backingData . toStorableJson ( ) ;
178- obj . globalID = this . globalId ;
179- obj . scalars = this . scalars ;
180- obj . references = this . getStorableMap ( this . references ) ;
181- obj . objectLists = this . getStorableMap ( this . objectLists ) ;
176+ toStorableJson ( ) : StubDataObjectJson {
177+ const obj : StubDataObjectJson = {
178+ globalID : this . globalId ,
179+ scalars : this . scalars ,
180+ references : this . getStorableMap ( this . references ) as StubDataObjectJson [ 'references' ] ,
181+ objectLists : this . getStorableMap ( this . objectLists ) as StubDataObjectJson [ 'objectLists' ]
182+ } ;
183+ if ( this . backingData ) {
184+ obj . backingData = this . backingData . toStorableJson ( ) ;
185+ }
182186 return obj ;
183187 }
184188}
189+
190+ export interface StubDataObjectJson {
191+ backingData ?: BackingDataObjectJson ;
192+ globalID : string ;
193+ scalars : { [ key : string ] : FDCScalarValue } ;
194+ references : { [ key : string ] : StubDataObjectJson } ;
195+ objectLists : {
196+ [ key : string ] : StubDataObjectJson [ ] ;
197+ } ;
198+ }
0 commit comments