How to use the @jsii/spec.CANONICAL_ANY 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 / kernel.ts View on Github external
// are going to skip the override.
    if (!methodInfo && obj[override.method]) {
      this._debug(`Skipping override of private method ${override.method}`);
      return;
    }

    if (!methodInfo) {
      // We've overriding a method on an object we have NO type information on (probably
      // because it's an anonymous object).
      // Pretend it's an (...args: any[]) => any
      methodInfo = {
        name: override.method,
        returns: { type: spec.CANONICAL_ANY },
        parameters: [{
          name: 'args',
          type: spec.CANONICAL_ANY,
          variadic: true
        }],
        variadic: true
      };
    }

    this._defineOverridenMethod(obj, objref, override, methodInfo);
  }
github aws / jsii / packages / jsii / lib / assembler.ts View on Github external
function _tryMakePrimitiveType(this: Assembler): spec.PrimitiveTypeReference | undefined {
      if (!type.symbol) {
        if (type.flags & ts.TypeFlags.Object) {
          return { primitive: spec.PrimitiveType.Json };
        }
        if (type.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) {
          return spec.CANONICAL_ANY;
        }
      } else if (type.symbol.valueDeclaration && isUnder(type.symbol.valueDeclaration.getSourceFile().fileName, this.stdlib)) {
        switch (type.symbol.name) {
          case 'Boolean':
            return { primitive: spec.PrimitiveType.Boolean };
          case 'Date':
            return { primitive: spec.PrimitiveType.Date };
          case 'Number':
            return { primitive: spec.PrimitiveType.Number };
          case 'String':
            return { primitive: spec.PrimitiveType.String };
        }
      }
      // Not a primitive type!
      return undefined;
github aws / jsii / packages / @jsii / kernel / lib / serialization.ts View on Github external
if (typeof value === 'function') {
        throw new Error('JSII Kernel is unable to serialize `function`. An instance with methods might have been returned by an `any` method?');
      }

      if (typeof value !== 'object' || value == null) {
        throw new Error(`JSII kernel assumption violated, ${JSON.stringify(value)} is not an object`);
      }

      if (SYMBOL_WIRE_TYPE in value && (value as any)[SYMBOL_WIRE_TYPE] === TOKEN_MAP) {
        return SERIALIZERS[SerializationClass.Map].serialize(
          value,
          {
            type: {
              collection: {
                kind: spec.CollectionKind.Map,
                elementtype: spec.CANONICAL_ANY,
              }
            }
          },
          host
        );
      }

      // To make sure people aren't going to try and return Map<> or Set<> out, test for
      // those and throw a descriptive error message. We can't detect these cases any other
      // way, and the by-value serialized object will be quite useless.
      if (value instanceof Set || value instanceof Map) { throw new Error("Can't return objects of type Set or Map"); }

      // Use a previous reference to maintain object identity. NOTE: this may cause us to return
      // a different type than requested! This is just how it is right now.
      // https://github.com/aws/jsii/issues/399
      const prevRef = objectReference(value);
github aws / jsii / packages / @jsii / kernel / lib / serialization.ts View on Github external
}
      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}`);
        return SERIALIZERS[SerializationClass.Struct].deserialize(data, { type: { fqn } }, host);
github aws / jsii / packages / @jsii / kernel / lib / kernel.ts View on Github external
let methodInfo = this._tryTypeInfoForMethod(override.method, typeFqn, interfaces);

    // If this is a private method (doesn't have methodInfo, key resolves on the object), we
    // are going to skip the override.
    if (!methodInfo && obj[override.method]) {
      this._debug(`Skipping override of private method ${override.method}`);
      return;
    }

    if (!methodInfo) {
      // We've overriding a method on an object we have NO type information on (probably
      // because it's an anonymous object).
      // Pretend it's an (...args: any[]) => any
      methodInfo = {
        name: override.method,
        returns: { type: spec.CANONICAL_ANY },
        parameters: [{
          name: 'args',
          type: spec.CANONICAL_ANY,
          variadic: true
        }],
        variadic: true
      };
    }

    this._defineOverridenMethod(obj, objref, override, methodInfo);
  }
github aws / jsii / packages / @jsii / kernel / lib / serialization.ts View on Github external
      return mapValues(value, (v) => host.recurse(v, { type: spec.CANONICAL_ANY }));
    },
github aws / jsii / packages / jsii / lib / assembler.ts View on Github external
if (type.symbol.name === 'Array') {
      return { type: await _arrayType.call(this) };
    }

    if (type.symbol.name === '__type' && type.symbol.members) {
      return { type: await _mapType.call(this) };
    }

    if (type.symbol.escapedName === 'Promise') {
      const typeRef = type as ts.TypeReference;
      if (!typeRef.typeArguments || typeRef.typeArguments.length !== 1) {
        this._diagnostic(declaration,
          ts.DiagnosticCategory.Error,
          'Un-specified promise type (need to specify as Promise)');
        return { type: spec.CANONICAL_ANY };
      }
      return { type: await this._typeReference(typeRef.typeArguments[0], declaration) };

    }

    return { type: { fqn: await this._getFQN(type) } };

    async function _arrayType(this: Assembler): Promise {
      const typeRef = type as ts.TypeReference;
      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,
github aws / jsii / packages / @jsii / kernel / lib / serialization.ts View on Github external
      return mapValues(value, (v) => host.recurse(v, { type: spec.CANONICAL_ANY }));
    },
github aws / jsii / packages / jsii / lib / assembler.ts View on Github external
async function _arrayType(this: Assembler): Promise {
      const typeRef = type as ts.TypeReference;
      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
        }
      };
    }