Skip to content

Commit 500d977

Browse files
committed
fix: implements serialization of enum collections
1 parent eb2414d commit 500d977

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

packages/abstractions/src/serialization/serializationWriter.ts

+7
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ export interface SerializationWriter {
9393
* @param values the value to write to the stream.
9494
*/
9595
writeEnumValue<T>(key?: string, ...values: (T | null | undefined)[]): void;
96+
97+
/**
98+
* Writes the specified collection of enum values to the stream with an optional given key.
99+
* @param key the key to write the value with.
100+
* @param values the value to write to the stream.
101+
*/
102+
writeCollectionOfEnumValue<T>(key?: string, values?: (T | null | undefined)[]): void;
96103
/**
97104
* Writes a null value for the specified key.
98105
* @param key the key to write the value with.

packages/serialization/form/src/formSerializationWriter.ts

+12
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,18 @@ export class FormSerializationWriter implements SerializationWriter {
131131
}
132132
}
133133
};
134+
public writeCollectionOfEnumValue = <T>(key?: string, values?: (T | null | undefined)[]): void => {
135+
if (values && values.length > 0) {
136+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
137+
const rawValues = values.filter((x) => x !== undefined).map((x) => `${x}`);
138+
if (rawValues.length > 0) {
139+
this.writeStringValue(
140+
key,
141+
rawValues.reduce((x, y) => `"${x}", "${y}"`),
142+
);
143+
}
144+
}
145+
};
134146
public getSerializedContent = (): ArrayBuffer => {
135147
return this.convertStringToArrayBuffer(this.writer.join(``));
136148
};

packages/serialization/json/src/jsonSerializationWriter.ts

+14
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,20 @@ export class JsonSerializationWriter implements SerializationWriter {
221221
}
222222
}
223223
};
224+
225+
public writeCollectionOfEnumValue = <T>(key?: string, values: (T | undefined | null)[]): void => {
226+
if (values.length > 0) {
227+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
228+
const rawValues = values.filter((x) => x !== undefined).map((x) => `${x}`);
229+
if (rawValues.length > 0) {
230+
this.writeStringValue(
231+
key,
232+
rawValues.reduce((x, y) => `"${x}", "${y}"`),
233+
);
234+
}
235+
}
236+
};
237+
224238
public getSerializedContent = (): ArrayBuffer => {
225239
return this.convertStringToArrayBuffer(this.writer.join(``));
226240
};

packages/serialization/multipart/src/multipartSerializationWriter.ts

+8
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ export class MultipartSerializationWriter implements SerializationWriter {
113113
): void => {
114114
throw new Error(`serialization of enum values is not supported with multipart`);
115115
};
116+
public writeCollectionOfEnumValue = <T>(
117+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
118+
key?: string,
119+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
120+
values?: (T | null | undefined)[],
121+
): void => {
122+
throw new Error(`serialization of collection of enum values is not supported with multipart`);
123+
};
116124
public getSerializedContent = (): ArrayBuffer => {
117125
return this.writer;
118126
};

packages/serialization/text/src/textSerializationWriter.ts

+12
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ export class TextSerializationWriter implements SerializationWriter {
133133
}
134134
}
135135
};
136+
public writeCollectionOfEnumValue = <T>(key?: string, values?: T[] | null): void => {
137+
if (values && values.length > 0) {
138+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
139+
const rawValues = values.filter((x) => x !== undefined).map((x) => `${x}`);
140+
if (rawValues.length > 0) {
141+
this.writeStringValue(
142+
key,
143+
rawValues.reduce((x, y) => `${x}, ${y}`),
144+
);
145+
}
146+
}
147+
};
136148
public getSerializedContent = (): ArrayBuffer => {
137149
return this.convertStringToArrayBuffer(this.writer.join(``));
138150
};

0 commit comments

Comments
 (0)