How to use the @jsii/spec.CollectionKind function in @jsii/spec

To help you get started, we’ve selected a few @jsii/spec examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github aws / jsii / packages / @jsii / kernel / lib / serialization.ts View on Github external
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}`);
github aws / jsii / packages / @jsii / kernel / lib / serialization.ts View on Github external
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);
github aws / jsii / packages / jsii / lib / assembler.ts View on Github external
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
        }
      };
    }
github aws / jsii / packages / jsii-pacmak / lib / targets / dotnet / dotnettyperesolver.ts View on Github external
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}`);
    }
  }
}
github aws / jsii / packages / jsii-pacmak / lib / targets / dotnet / dotnettyperesolver.ts View on Github external
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}`);
    }
  }
}
github aws / jsii / packages / jsii / lib / assembler.ts View on Github external
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
        }
      };
    }
github aws / jsii / packages / jsii-reflect / lib / type-ref.ts View on Github external
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);
  }