How to use the @jsii/spec.PrimitiveType 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 / lib / assembler.ts View on Github external
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;

      function isUnder(file: string, dir: string): boolean {
        const relative = path.relative(dir, file);
        return !relative.startsWith(path.sep) && !relative.startsWith('..');
      }
    }
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;

      function isUnder(file: string, dir: string): boolean {
        const relative = path.relative(dir, file);
        return !relative.startsWith(path.sep) && !relative.startsWith('..');
      }
    }
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;

      function isUnder(file: string, dir: string): boolean {
        const relative = path.relative(dir, file);
        return !relative.startsWith(path.sep) && !relative.startsWith('..');
      }
    }
github aws / jsii / packages / @jsii / kernel / lib / serialization.ts View on Github external
export function serializationType(typeRef: OptionalValueOrVoid, lookup: TypeLookup): TypeSerialization[] {
  if (typeRef == null) { throw new Error("Kernel error: expected type information, got 'undefined'"); }
  if (typeRef === 'void') { return [{ serializationClass: SerializationClass.Void, typeRef }]; }
  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) {
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 };
        }
      }
github aws / jsii / packages / jsii-pacmak / lib / targets / dotnet / dotnettyperesolver.ts View on Github external
private toDotNetPrimitive(primitive: spec.PrimitiveType): string {
    switch (primitive) {
      case spec.PrimitiveType.Boolean:
        return 'bool';
      case spec.PrimitiveType.Date:
        return 'System.DateTime';
      case spec.PrimitiveType.Json:
        return 'Newtonsoft.Json.Linq.JObject';
      case spec.PrimitiveType.Number:
        return 'double';
      case spec.PrimitiveType.String:
        return 'string';
      case spec.PrimitiveType.Any:
        return 'object';
      default:
        throw new Error(`Unknown primitive type: ${primitive}`);
    }
  }
github aws / jsii / packages / jsii-pacmak / lib / targets / dotnet / dotnettyperesolver.ts View on Github external
private toDotNetPrimitive(primitive: spec.PrimitiveType): string {
    switch (primitive) {
      case spec.PrimitiveType.Boolean:
        return 'bool';
      case spec.PrimitiveType.Date:
        return 'System.DateTime';
      case spec.PrimitiveType.Json:
        return 'Newtonsoft.Json.Linq.JObject';
      case spec.PrimitiveType.Number:
        return 'double';
      case spec.PrimitiveType.String:
        return 'string';
      case spec.PrimitiveType.Any:
        return 'object';
      default:
        throw new Error(`Unknown primitive type: ${primitive}`);
    }
  }
github aws / jsii / packages / jsii-pacmak / lib / targets / dotnet / dotnettyperesolver.ts View on Github external
private toDotNetPrimitive(primitive: spec.PrimitiveType): string {
    switch (primitive) {
      case spec.PrimitiveType.Boolean:
        return 'bool';
      case spec.PrimitiveType.Date:
        return 'System.DateTime';
      case spec.PrimitiveType.Json:
        return 'Newtonsoft.Json.Linq.JObject';
      case spec.PrimitiveType.Number:
        return 'double';
      case spec.PrimitiveType.String:
        return 'string';
      case spec.PrimitiveType.Any:
        return 'object';
      default:
        throw new Error(`Unknown primitive type: ${primitive}`);
    }
  }
github aws / jsii / packages / jsii-pacmak / lib / targets / dotnet / dotnettyperesolver.ts View on Github external
private toDotNetPrimitive(primitive: spec.PrimitiveType): string {
    switch (primitive) {
      case spec.PrimitiveType.Boolean:
        return 'bool';
      case spec.PrimitiveType.Date:
        return 'System.DateTime';
      case spec.PrimitiveType.Json:
        return 'Newtonsoft.Json.Linq.JObject';
      case spec.PrimitiveType.Number:
        return 'double';
      case spec.PrimitiveType.String:
        return 'string';
      case spec.PrimitiveType.Any:
        return 'object';
      default:
        throw new Error(`Unknown primitive type: ${primitive}`);
    }
  }
github aws / jsii / packages / jsii-pacmak / lib / targets / dotnet / dotnettyperesolver.ts View on Github external
private toDotNetPrimitive(primitive: spec.PrimitiveType): string {
    switch (primitive) {
      case spec.PrimitiveType.Boolean:
        return 'bool';
      case spec.PrimitiveType.Date:
        return 'System.DateTime';
      case spec.PrimitiveType.Json:
        return 'Newtonsoft.Json.Linq.JObject';
      case spec.PrimitiveType.Number:
        return 'double';
      case spec.PrimitiveType.String:
        return 'string';
      case spec.PrimitiveType.Any:
        return 'object';
      default:
        throw new Error(`Unknown primitive type: ${primitive}`);
    }
  }