/** * Returns the escaped fully qualified name of a {@link ClassTemplateSpec}. * * @param spec to build a escaped fully qualified name for. * * @return the escaped fullname. */ public static String escapedFullname(ClassTemplateSpec spec) { // TODO: Remove below null and introduce module namespacing return toFullname(null, escapeKeyword(spec.getClassName())); }
return spec.getClassName() + "Member"; return "FixedMember"; } else if (schemaType == Type.ENUM) { return spec.getClassName() + "Member"; } else if (schemaType == Type.RECORD) { return spec.getClassName() + "Member"; } else if (schemaType == Type.MAP) { return "MapMember";
return spec.getClassName() + "Member"; return "FixedMember"; } else if (schemaType == Type.ENUM) { return spec.getClassName() + "Member"; } else if (schemaType == Type.RECORD) { return spec.getClassName() + "Member"; } else if (schemaType == Type.MAP) { return "MapMember";
/** * Returns the escaped fully qualified name of a {@link ClassTemplateSpec}. * * @param spec to build a escaped fully qualified name for. * * @return the escaped fullname. */ public static String escapedFullname(ClassTemplateSpec spec) { return toFullname(spec.getNamespace(), escapeKeyword(spec.getClassName())); }
@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); }
/** * Given an expression that evaluates to a Swift data binding type, and it's pegasus type, * return an expression that evaluates to the corresponding SwiftyJson JSON type. * * @param expr provides the expression that evaluates to a SwiftyJson JSON type. * @param spec provides the pegasus type that the returned expression should toSwiftCode to. */ private Expr writeTypeExpr(Expr expr, ClassTemplateSpec spec, CustomInfoSpec customInfo) { if (customInfo != null) { Expr coercer = expr(customInfo.getCoercerClass().getClassName()); return coercer.coercerOutput(writeTypeExpr(expr, spec)); } else { return writeTypeExpr(expr, spec); } }
/** * See {@link org.coursera.courier.swift.SwiftProperties} for customization options. */ @Override public GeneratedCode generate(ClassTemplateSpec templateSpec) { String code; SwiftProperties swiftProperties = globalConfig.lookupSwiftProperties(templateSpec); if (swiftProperties.omit) return null; SwiftSyntax syntax = new SwiftSyntax(templateSpec, swiftProperties, globalConfig); SwiftyJSON swifty = new SwiftyJSON(syntax); try { if (templateSpec instanceof RecordTemplateSpec) { code = engine.render("rythm-swift/record.txt", templateSpec, syntax, swifty); } else if (templateSpec instanceof EnumTemplateSpec) { code = engine.render("rythm-swift/enum.txt", templateSpec); } else if (templateSpec instanceof UnionTemplateSpec) { code = engine.render("rythm-swift/union.txt", templateSpec, syntax, swifty); } else { return null; // Indicates that we are declining to generate code for the type (e.g. map or array) } } catch (RythmException e) { throw new RuntimeException( "Internal error in generator while processing " + templateSpec.getFullName(), e); } SwiftCompilationUnit compilationUnit = new SwiftCompilationUnit( templateSpec.getClassName(), ""); code = formatter.format(code); return new GeneratedCode(compilationUnit, code); }
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); } }
DataSchema refSchema = customInfo.getCustomSchema().getDereferencedDataSchema(); fieldType = ClassTemplateSpec.createFromDataSchema(refSchema); String coercer = customInfo.getCoercerClass().getClassName(); String uncoerced = toLiteral(fieldType.getSchema(), field.getSchemaField().getDefault()); if (uncoerced == null) {
Expr coercer = expr(custom.getCoercerClass().getClassName()); if (isOptional) { return directAccessor.map(coercer.coercerInput($0)); Expr coercer = expr(custom.getCoercerClass().getClassName()); if (isOptional) { return directAccessor.map(coercer.coercerInput(readTypeExpr($0, spec)));
CustomInfoSpec customInfo = mapSpec.getCustomInfo(); if (customInfo != null) { Expr coercer = expr(customInfo.getCoercerClass().getClassName()); return expr.mapValues(writeTypeExpr(coercer.coercerOutput($0), valueSpec)); } else { CustomInfoSpec arrayCustomInfo = arraySpec.getCustomInfo(); if (arrayCustomInfo != null) { Expr coercer = expr(arrayCustomInfo.getCoercerClass().getClassName()); return expr.map(writeTypeExpr(coercer.coercerOutput($0), itemSpec)); } else {
JavaSyntax.escapeKeyword(templateSpec.getClassName()), templateSpec.getNamespace()); code = formatter.format(code);