Skip to content

Commit 20619a4

Browse files
committed
RDBC-622 parse cmp exchange in sync way - vol 2
1 parent 2898bfe commit 20619a4

File tree

4 files changed

+25
-98
lines changed

4 files changed

+25
-98
lines changed

src/Documents/Operations/CompareExchange/CompareExchangeResult.ts

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { DocumentConventions } from "../../Conventions/DocumentConventions";
22
import { throwError } from "../../../Exceptions";
33
import { TypeUtil } from "../../../Utility/TypeUtil";
4-
import { CompareExchangeResultClass } from "../../../Types";
4+
import { CompareExchangeResultClass, ServerCasing, ServerResponse } from "../../../Types";
55
import { COMPARE_EXCHANGE } from "../../../Constants";
66
import { DocumentType } from "../../DocumentAbstractions";
7+
import { ObjectUtil } from "../../../Utility/ObjectUtil";
78

89
export 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>();

src/Documents/Operations/CompareExchange/DeleteCompareExchangeValueOperation.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { HttpRequestParameters } from "../../../Primitives/Http";
22
import { IOperation, OperationResultType } from "../OperationAbstractions";
33
import { CompareExchangeResult, CompareExchangeResultResponse } from "./CompareExchangeResult";
4-
import { CompareExchangeResultClass } from "../../../Types";
4+
import { CompareExchangeResultClass, ServerCasing, ServerResponse } from "../../../Types";
55
import { IDocumentStore } from "../../IDocumentStore";
66
import { DocumentConventions } from "../../Conventions/DocumentConventions";
77
import { HttpCache } from "../../../Http/HttpCache";
@@ -59,7 +59,7 @@ export class RemoveCompareExchangeCommand<T> extends RavenCommand<CompareExchang
5959
}
6060

6161
public createRequest(node: ServerNode): HttpRequestParameters {
62-
const uri = node.url + "/databases/" + node.database + "/cmpxchg?key=" + encodeURIComponent(this._key)
62+
const uri = node.url + "/databases/" + node.database + "/cmpxchg?key=" + encodeURIComponent(this._key)
6363
+ "&index=" + this._index;
6464
return {
6565
method: "DELETE",
@@ -69,11 +69,11 @@ export class RemoveCompareExchangeCommand<T> extends RavenCommand<CompareExchang
6969

7070
public async setResponseAsync(bodyStream: stream.Stream, fromCache: boolean): Promise<string> {
7171
let body: string = null;
72-
const resObj = await this._pipeline<CompareExchangeResultResponse>()
72+
const resObj = await this._pipeline<ServerCasing<ServerResponse<CompareExchangeResultResponse>>>()
7373
.collectBody(_ => body = _)
74-
.parseJsonAsync()
75-
.jsonKeysTransform("CompareExchangeValue", this._conventions)
74+
.parseJsonSync()
7675
.process(bodyStream);
76+
7777
this.result = CompareExchangeResult.parseFromObject(resObj, this._conventions, this._clazz);
7878
return body;
7979
}

src/Documents/Operations/CompareExchange/PutCompareExchangeValueOperation.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { ServerNode } from "../../../Http/ServerNode";
1010
import { JsonSerializer } from "../../../Mapping/Json/Serializer";
1111
import { TypeUtil } from "../../../Utility/TypeUtil";
1212
import * as stream from "readable-stream";
13-
import { ObjectTypeDescriptor, CompareExchangeResultClass } from "../../../Types";
13+
import { ObjectTypeDescriptor, CompareExchangeResultClass, ServerResponse, ServerCasing } from "../../../Types";
1414
import { IRaftCommand } from "../../../Http/IRaftCommand";
1515
import { RaftIdGenerator } from "../../../Utility/RaftIdGenerator";
1616
import { COMPARE_EXCHANGE, CONSTANTS } from "../../../Constants";
@@ -103,10 +103,9 @@ export class PutCompareExchangeValueCommand<T> extends RavenCommand<CompareExcha
103103
public async setResponseAsync(bodyStream: stream.Stream, fromCache: boolean): Promise<string> {
104104
let body: string = null;
105105

106-
const resObj = await this._pipeline<CompareExchangeResultResponse>()
106+
const resObj = await this._pipeline<ServerCasing<ServerResponse<CompareExchangeResultResponse>>>()
107107
.collectBody(_ => body = _)
108-
.parseJsonAsync()
109-
.jsonKeysTransform("CompareExchangeValue", this._conventions)
108+
.parseJsonSync()
110109
.process(bodyStream);
111110

112111
const type = !TypeUtil.isPrimitive(this._value)

src/Mapping/Json/Streams/TransformJsonKeysProfiles.ts

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { throwError } from "../../../Exceptions";
55
export type TransformJsonKeysProfile =
66
"DocumentLoad"
77
| "Patch"
8-
| "CompareExchangeValue"
98
| "SubscriptionResponsePayload"
109
| "SubscriptionRevisionsResponsePayload";
1110

@@ -33,17 +32,6 @@ export function getTransformJsonKeysProfile(
3332
};
3433
}
3534

36-
if (profile === "CompareExchangeValue") {
37-
if (!conventions) {
38-
throwError("InvalidArgumentException", "Document conventions are required for this profile.");
39-
}
40-
41-
return {
42-
getCurrentTransform:
43-
buildEntityKeysTransformForPutCompareExchangeValue(conventions.entityFieldNameConvention)
44-
};
45-
}
46-
4735
if (profile === "SubscriptionResponsePayload") {
4836
if (!conventions) {
4937
throwError("InvalidArgumentException", "Document conventions are required for this profile.");
@@ -118,52 +106,6 @@ function buildEntityKeysTransformForPatch(entityCasingConvention) {
118106
};
119107
}
120108

121-
function buildEntityKeysTransformForPutCompareExchangeValue(entityCasingConvention) {
122-
return function compareExchangeValueTransform(key, stack) {
123-
const len = stack.length;
124-
if (len === 1 || len === 2) {
125-
// Results, Includes
126-
return "camel";
127-
}
128-
129-
// len === 2 is array index
130-
131-
if (len === 3) {
132-
// top document level
133-
return key === "@metadata" ? null : entityCasingConvention;
134-
}
135-
136-
if (len === 4) {
137-
if (stack[2] === "@metadata") {
138-
// handle @metadata object keys
139-
if (key[0] === "@" || key === "Raven-Node-Type") {
140-
return null;
141-
}
142-
}
143-
}
144-
145-
if (len === 5) {
146-
// do not touch @nested-object-types keys
147-
if (stack[len - 2] === "@nested-object-types") {
148-
return null;
149-
}
150-
}
151-
152-
if (len === 6) {
153-
// @metadata.@attachments.[].name
154-
if (stack[2] === "@metadata") {
155-
if (stack[3] === "@attachments") {
156-
return "camel";
157-
}
158-
159-
return null;
160-
}
161-
}
162-
163-
return entityCasingConvention;
164-
};
165-
}
166-
167109
function buildEntityKeysTransformForSubscriptionResponsePayload(entityCasingConvention) {
168110
return function entityKeysTransform(key, stack) {
169111
const len = stack.length;

0 commit comments

Comments
 (0)