public static AndroidProperties lookupAndroidProperties(ClassTemplateSpec templateSpec) { DataSchema schema = templateSpec.getSchema(); if (schema == null) { return DEFAULT; } else { Object android = schema.getProperties().get("android"); if (android == null || !(android instanceof DataMap)) { return DEFAULT; } DataMap properties = ((DataMap) android); String primitiveStyleStr = properties.getString("primitiveStyle"); Optionality optionality = primitiveStyleStr == null ? DEFAULT.optionality : Optionality.valueOf(primitiveStyleStr); return new AndroidProperties(optionality); } } }
@Override public GeneratedCode generate(ClassTemplateSpec templateSpec) { if (predef.contains(templateSpec.getSchema())) { return null; } JavaDataTemplateGenerator.Config config = new JavaDataTemplateGenerator.Config(); JavaDataTemplateGenerator generator = new JavaDataTemplateGenerator(config); JClass result = generator.generate(templateSpec); ByteArrayOutputStream out = new ByteArrayOutputStream(); String code; try { generator.getCodeModel().build( new CapturingCodeWriter(templateSpec.getNamespace(), templateSpec.getClassName(), out)); out.flush(); out.close(); code = out.toString("UTF-8"); } catch (IOException e) { throw new RuntimeException("Error generating code for " + templateSpec.getFullName(), e); } if (code.trim().equals("")) { throw new RuntimeException("Failed to generate code for " + templateSpec.getFullName()); } return new GeneratedCode( new JavaCompilationUnit( result.name(), result._package().name()), code); }
private String toTypeString(ClassTemplateSpec spec) { if (spec.getSchema() == null) { // custom type return escapedFullname(spec); Type schemaType = spec.getSchema().getType(); if (schemaType == Type.INT) { return "Int"; // TODO: just use Int32 here? (On a 32-bit platform, Int is the same size as Int32.)
public TSProperties lookupTSProperties(ClassTemplateSpec templateSpec) { DataSchema schema = templateSpec.getSchema(); if (templateSpec instanceof UnionTemplateSpec && templateSpec.getOriginalTyperefSchema() != null) { schema = templateSpec.getOriginalTyperefSchema(); } if (schema == null) { return defaults; } else { Object typescript = schema.getProperties().get("typescript"); if (typescript == null || !(typescript instanceof DataMap)) { return defaults; } DataMap properties = ((DataMap) typescript); String optionalityString = properties.getString("optionality"); TSProperties.Optionality optionality = optionalityString == null ? defaults.optionality : TSProperties.Optionality.valueOf(optionalityString); Boolean maybeEquatable = properties.getBoolean("equatable"); boolean equatable = maybeEquatable == null ? defaults.equatable : maybeEquatable; Boolean maybeOmit = properties.getBoolean("omit"); boolean omit = maybeOmit == null ? defaults.omit : maybeOmit; return new TSProperties(optionality, equatable, omit); } } }
public SwiftProperties lookupSwiftProperties(ClassTemplateSpec templateSpec) { DataSchema schema = templateSpec.getSchema(); if (templateSpec instanceof UnionTemplateSpec && templateSpec.getOriginalTyperefSchema() != null) { schema = templateSpec.getOriginalTyperefSchema(); } if (schema == null) { return defaults; } else { Object swift = schema.getProperties().get("swift"); if (swift == null || !(swift instanceof DataMap)) { return defaults; } DataMap properties = ((DataMap) swift); String optionalityString = properties.getString("optionality"); SwiftProperties.Optionality optionality = optionalityString == null ? defaults.optionality : SwiftProperties.Optionality.valueOf(optionalityString); Boolean maybeEquatable = properties.getBoolean("equatable"); boolean equatable = maybeEquatable == null ? defaults.equatable : maybeEquatable; Boolean maybeOmit = properties.getBoolean("omit"); boolean omit = maybeOmit == null ? defaults.omit : maybeOmit; return new SwiftProperties(optionality, equatable, omit); } } }
/** * Create a TS*Syntax class around the provided ClassTemplate. * * That class will perform the heavy lifting of rendering TS-specific strings into the template. * * @param template the ClassTemplate * @return a TS*Syntax class (see {@link TSSyntax} class-level docs for more info) */ private TSTypeSyntax createTypeSyntax(ClassTemplateSpec template) { if (template instanceof RecordTemplateSpec) { return new TSRecordSyntax((RecordTemplateSpec) template); } else if (template instanceof TyperefTemplateSpec) { return TSTyperefSyntaxCreate((TyperefTemplateSpec) template); } else if (template instanceof FixedTemplateSpec) { return TSFixedSyntaxCreate((FixedTemplateSpec) template); } else if (template instanceof EnumTemplateSpec) { return new TSEnumSyntax((EnumTemplateSpec) template); } else if (template instanceof PrimitiveTemplateSpec) { return new TSPrimitiveTypeSyntax((PrimitiveTemplateSpec) template); } else if (template instanceof MapTemplateSpec) { return new TSMapSyntax((MapTemplateSpec) template); } else if (template instanceof ArrayTemplateSpec) { return new TSArraySyntax((ArrayTemplateSpec) template); } else if (template instanceof UnionTemplateSpec) { return new TSUnionSyntax((UnionTemplateSpec) template); } else { throw new RuntimeException("Unrecognized template spec: " + template + " with schema " + template.getSchema()); } }
fieldType = ClassTemplateSpec.createFromDataSchema(refSchema); String coercer = customInfo.getCoercerClass().getClassName(); String uncoerced = toLiteral(fieldType.getSchema(), field.getSchemaField().getDefault()); if (uncoerced == null) { return null; return toLiteral(fieldType.getSchema(), field.getSchemaField().getDefault());
private Expr readTypeExpr(Expr expr, ClassTemplateSpec spec) { DataSchema.Type schemaType = spec.getSchema().getType(); Expr className = expr(spec.getClassName()); switch (schemaType) { case ENUM: return className.enumRead(expr); case RECORD: case UNION: return className.readJSON(expr); case MAP: CourierMapTemplateSpec mapSpec = (CourierMapTemplateSpec)spec; ClassTemplateSpec valueSpec = selectMapValueType(mapSpec); return expr.mapValues(readTypeExpr($0, valueSpec, mapSpec.getCustomInfo(), false)); case ARRAY: ArrayTemplateSpec arraySpec = (ArrayTemplateSpec)spec; ClassTemplateSpec itemSpec = selectArrayItemsType(arraySpec); return expr.map(readTypeExpr($0, itemSpec, arraySpec.getCustomInfo(), false)); default: throw new IllegalArgumentException("unrecognized type: " + schemaType); } }
private Expr writeTypeExpr(Expr expr, ClassTemplateSpec spec) { DataSchema.Type schemaType = spec.getSchema().getType(); switch (schemaType) { case ENUM: return expr.mapValues(writeTypeExpr(coercer.coercerOutput($0), valueSpec)); } else { if (valueSpec.getSchema().isPrimitive()) { return expr; } else { return expr.map(writeTypeExpr(coercer.coercerOutput($0), itemSpec)); } else { if (itemSpec.getSchema().isPrimitive()) { return expr; } else {
DataSchema schema = spec.getSchema(); Expr directAccessor = checkedTypeAccessorExpr(expr, schema, isOptional);