2019-05-15 01:25:46 +03:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import { Coder, Reader, Writer } from "./abstract-coder";
|
|
|
|
import { pack, unpack } from "./array";
|
|
|
|
|
|
|
|
export class TupleCoder extends Coder {
|
|
|
|
readonly coders: Array<Coder>;
|
|
|
|
|
|
|
|
constructor(coders: Array<Coder>, localName: string) {
|
|
|
|
let dynamic = false;
|
2020-04-25 10:25:42 +03:00
|
|
|
const types: Array<string> = [];
|
2019-05-15 01:25:46 +03:00
|
|
|
coders.forEach((coder) => {
|
|
|
|
if (coder.dynamic) { dynamic = true; }
|
|
|
|
types.push(coder.type);
|
|
|
|
});
|
2020-04-25 10:25:42 +03:00
|
|
|
const type = ("tuple(" + types.join(",") + ")");
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
super("tuple", type, localName, dynamic);
|
|
|
|
this.coders = coders;
|
|
|
|
}
|
|
|
|
|
2020-05-13 05:41:04 +03:00
|
|
|
encode(writer: Writer, value: Array<any> | { [ name: string ]: any }): number {
|
2019-05-15 01:25:46 +03:00
|
|
|
return pack(writer, this.coders, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
decode(reader: Reader): any {
|
|
|
|
return reader.coerce(this.name, unpack(reader, this.coders));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|