Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return value;
}
if (Array.isArray(value)) {
host.debug('ANY is an Array');
return value.map(e => host.recurse(e, { type: spec.CANONICAL_ANY }));
}
if (isWireEnum(value)) {
host.debug('ANY is an Enum');
return deserializeEnum(value, host.findSymbol);
}
if (isWireMap(value)) {
host.debug('ANY is a Map');
const mapOfAny: spec.CollectionTypeReference = {
collection: {
kind: spec.CollectionKind.Map,
elementtype: spec.CANONICAL_ANY,
}
};
return SERIALIZERS[SerializationClass.Map].deserialize(value, { type: mapOfAny }, host);
}
if (isObjRef(value)) {
host.debug('ANY is a Ref');
return host.objects.findObject(value).instance;
}
// if the value has a struct token, it was serialized by a typed jsii
// struct, but since the deserialization target is ANY, all we can do is
// strip the data from $jsii.struct and continue to deserialize as ANY.
if (isWireStruct(value)) {
const { fqn, data } = value[TOKEN_STRUCT];
host.debug(`ANY is a struct of type ${fqn}`);
if (spec.isPrimitiveTypeReference(typeRef.type)) {
switch (typeRef.type.primitive) {
case spec.PrimitiveType.Any: return [{ serializationClass: SerializationClass.Any, typeRef }];
case spec.PrimitiveType.Date: return [{ serializationClass: SerializationClass.Date, typeRef }];
case spec.PrimitiveType.Json: return [{ serializationClass: SerializationClass.Json, typeRef }];
case spec.PrimitiveType.Boolean:
case spec.PrimitiveType.Number:
case spec.PrimitiveType.String:
return [{ serializationClass: SerializationClass.Scalar, typeRef }];
}
throw new Error('Unknown primitive type');
}
if (spec.isCollectionTypeReference(typeRef.type)) {
return [{
serializationClass: typeRef.type.collection.kind === spec.CollectionKind.Array ? SerializationClass.Array : SerializationClass.Map,
typeRef
}];
}
if (spec.isUnionTypeReference(typeRef.type)) {
const compoundTypes = flatMap(typeRef.type.union.types, t => serializationType({ type: t }, lookup));
// Propagate the top-level 'optional' field to each individual subtype
for (const t of compoundTypes) {
if (t.typeRef !== 'void') {
t.typeRef.optional = typeRef.optional;
}
}
return compoundTypes.sort((l, r) => compareSerializationClasses(l.serializationClass, r.serializationClass));
}
// The next part of the conversion is lookup-dependent
const type = lookup(typeRef.type.fqn);
async function _mapType(this: Assembler): Promise {
let elementtype: spec.TypeReference;
const objectType = type.getStringIndexType();
if (objectType) {
elementtype = await this._typeReference(objectType, declaration);
} else {
this._diagnostic(declaration,
ts.DiagnosticCategory.Error,
'Only string index maps are supported');
elementtype = spec.CANONICAL_ANY;
}
return {
collection: {
elementtype,
kind: spec.CollectionKind.Map
}
};
}
private toDotNetCollection(ref: spec.CollectionTypeReference): string {
const elementDotNetType = this.toDotNetType(ref.collection.elementtype);
switch (ref.collection.kind) {
case spec.CollectionKind.Array:
return `${elementDotNetType}[]`;
case spec.CollectionKind.Map:
return `System.Collections.Generic.IDictionary`;
default:
throw new Error(`Unsupported collection kind: ${ref.collection.kind}`);
}
}
}
private toDotNetCollection(ref: spec.CollectionTypeReference): string {
const elementDotNetType = this.toDotNetType(ref.collection.elementtype);
switch (ref.collection.kind) {
case spec.CollectionKind.Array:
return `${elementDotNetType}[]`;
case spec.CollectionKind.Map:
return `System.Collections.Generic.IDictionary`;
default:
throw new Error(`Unsupported collection kind: ${ref.collection.kind}`);
}
}
}
let elementtype: spec.TypeReference;
if (typeRef.typeArguments?.length === 1) {
elementtype = await this._typeReference(typeRef.typeArguments[0], declaration);
} else {
const count = typeRef.typeArguments ? typeRef.typeArguments.length : 'none';
this._diagnostic(declaration,
ts.DiagnosticCategory.Error,
`Array references must have exactly one type argument (found ${count})`);
elementtype = spec.CANONICAL_ANY;
}
return {
collection: {
elementtype,
kind: spec.CollectionKind.Array
}
};
}
public get arrayOfType(): TypeReference | undefined {
if (!jsii.isCollectionTypeReference(this.spec)) {
return undefined;
}
if (this.spec.collection.kind !== jsii.CollectionKind.Array) {
return undefined;
}
return new TypeReference(this.system, this.spec.collection.elementtype);
}