if (enclosingClass != null && classInfo.namespace.equals(enclosingClass.getFullName())) classTemplateSpec.setEnclosingClass(enclosingClass); classTemplateSpec.setClassName(classInfo.name); classTemplateSpec.setModifiers(ModifierSpec.PUBLIC, ModifierSpec.STATIC, ModifierSpec.FINAL); classTemplateSpec.setNamespace(classInfo.namespace); classTemplateSpec.setClassName(classInfo.name); classTemplateSpec.setModifiers(ModifierSpec.PUBLIC);
if (spec.getSchema() == null) { // custom type return spec.getClassName() + "Member"; Type schemaType = spec.getSchema().getType(); if (schemaType == Type.INT) { return "IntMember"; 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";
private ClassTemplateSpec createFromDataSchema(DataSchema schema) { if (schema instanceof MapDataSchema) { return new CourierMapTemplateSpec((MapDataSchema) schema); } return ClassTemplateSpec.createFromDataSchema(schema); }
/** * 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); }
if (customInfo != null) { 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) { return null; return toLiteral(fieldType.getSchema(), field.getSchemaField().getDefault());
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); } } }
private static String enclosingClassAndMemberNameToString(ClassTemplateSpec enclosingClass, String memberName) { final StringBuilder sb = new StringBuilder(); if (memberName != null) { sb.append(" in "); sb.append(memberName); } if (enclosingClass != null) { sb.append(" in "); sb.append(enclosingClass.getFullName()); } return sb.toString(); }
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); } } }
/** * 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); }
/** * Register a new class TemplateSpec. * <p/> * Registration is necessary to associate the {@link com.linkedin.pegasus.generator.spec.ClassTemplateSpec} with the source file for which it was generated. This may be used later to determine if generated class should be emitted * based on the location of the source file. * <p/> * Registration also associates the {@link com.linkedin.data.schema.DataSchema} to the generated {@link com.linkedin.pegasus.generator.spec.ClassTemplateSpec} and the generated class's full name to the the {@link com.linkedin.pegasus.generator.spec.ClassTemplateSpec}. * * @param schema provides the {@link com.linkedin.data.schema.DataSchema} of the generated class. * @param classTemplateSpec provides the generated class. */ private void registerClassTemplateSpec(DataSchema schema, ClassTemplateSpec classTemplateSpec) { classTemplateSpec.setLocation(currentLocation().toString()); _schemaToClassMap.put(schema, classTemplateSpec); _classNameToSchemaMap.put(classTemplateSpec.getFullName(), schema); _classToDataSchemaLocationMap.put(classTemplateSpec, currentLocation()); if (schema instanceof NamedDataSchema) { checkClassNameForSpecialSuffix(classTemplateSpec.getFullName()); } _classTemplateSpecs.add(classTemplateSpec); }
/** * 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())); }
customClasses.customClass = new ClassTemplateSpec(); customClasses.customClass.setFullName((String) custom); if (!allowCustomClass(schema)) { throw new IllegalArgumentException(schema + " cannot have custom class binding"); customClasses.customCoercerClass = new ClassTemplateSpec(); customClasses.customCoercerClass.setFullName((String) coercerClass);
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); } } }
/** * Instead of generate spec for the specify {@link com.linkedin.data.schema.DataSchema}, assume it is already defined in the system. */ public void registerDefinedSchema(DataSchema schema) { final ClassTemplateSpec spec = createFromDataSchema(schema); _schemaToClassMap.put(schema, spec); _classNameToSchemaMap.put(spec.getFullName(), schema); }
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.)
JavaSyntax.escapeKeyword(templateSpec.getClassName()), templateSpec.getNamespace()); code = formatter.format(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); } }
if (spec.getSchema() == null) { // custom type return spec.getClassName() + "Member"; Type schemaType = spec.getSchema().getType(); if (schemaType == Type.INT) { return "IntMember"; 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";
"Internal error in generator while processing " + templateSpec.getFullName(), e); templateSpec.getFullName(), ""); code = formatter.format(code); return new GeneratedCode(compilationUnit, code);