11import { DocumentConventions } from "../../Conventions/DocumentConventions" ;
22import { throwError } from "../../../Exceptions" ;
33import { TypeUtil } from "../../../Utility/TypeUtil" ;
4- import { CompareExchangeResultClass } from "../../../Types" ;
4+ import { CompareExchangeResultClass , ServerCasing , ServerResponse } from "../../../Types" ;
55import { COMPARE_EXCHANGE } from "../../../Constants" ;
66import { DocumentType } from "../../DocumentAbstractions" ;
7+ import { ObjectUtil } from "../../../Utility/ObjectUtil" ;
78
89export interface CompareExchangeResultResponse {
910 index : number ;
@@ -20,39 +21,15 @@ export class CompareExchangeResult<T> {
2021 public successful : boolean ;
2122
2223 public static parseFromObject < T > (
23- { index , value , successful } : CompareExchangeResultResponse ,
24+ response : ServerCasing < ServerResponse < CompareExchangeResultResponse > > ,
2425 conventions : DocumentConventions ,
2526 clazz ?: CompareExchangeResultClass < T > ) : CompareExchangeResult < T > {
26- if ( ! index ) {
27+ if ( ! response . Index ) {
2728 throwError ( "InvalidOperationException" , "Response is invalid. Index is missing" ) ;
2829 }
2930
30- const val = value . object || null ;
31- return CompareExchangeResult . _create ( val , index , successful , conventions , clazz ) ;
32- }
33-
34- public static parseFromString < T > (
35- responseString : string ,
36- conventions : DocumentConventions ,
37- clazz ?: CompareExchangeResultClass < T > ) : CompareExchangeResult < T > {
38-
39- const response = JSON . parse ( responseString ) ;
40-
41- const index = response [ "Index" ] ;
42- if ( ! index ) {
43- throwError ( "InvalidOperationException" , "Response is invalid. Index is missing" ) ;
44- }
45-
46- const successful = response [ "Successful" ] ;
47- const raw = response [ "Value" ] ;
48-
49- let val = null ;
50-
51- if ( raw ) {
52- val = raw [ COMPARE_EXCHANGE . OBJECT_FIELD_NAME ] ;
53- }
54-
55- return CompareExchangeResult . _create ( val , index , successful , conventions , clazz ) ;
31+ const val = response . Value . Object || null ;
32+ return CompareExchangeResult . _create ( val , response . Index , response . Successful , conventions , clazz ) ;
5633 }
5734
5835 private static _create < T > (
@@ -74,13 +51,22 @@ export class CompareExchangeResult<T> {
7451 return emptyExchangeResult ;
7552 }
7653
77- let result : T ;
54+ let result : any = null ;
7855 if ( TypeUtil . isPrimitive ( val ) ) {
7956 result = val as any as T ;
8057 } else {
58+ let rawValue = val ;
8159 // val comes here with proper key case already
8260 const entityType = conventions . getJsTypeByDocumentType ( clazz as DocumentType ) ;
83- result = conventions . deserializeEntityFromJson ( entityType , val ) as any as T ;
61+ if ( conventions . entityFieldNameConvention ) {
62+ rawValue = ObjectUtil . transformObjectKeys (
63+ rawValue , {
64+ defaultTransform : conventions . entityFieldNameConvention ,
65+ recursive : true ,
66+ arrayRecursive : true
67+ } ) ;
68+ }
69+ result = conventions . deserializeEntityFromJson ( entityType , rawValue ) as any as T ;
8470 }
8571
8672 const exchangeResult = new CompareExchangeResult < T > ( ) ;
0 commit comments