public static Set<ClassTemplateSpec> directReferencedTypes(ClassTemplateSpec spec) { Set<ClassTemplateSpec> results = new HashSet<ClassTemplateSpec>(); if (spec instanceof RecordTemplateSpec) { RecordTemplateSpec recordSpec = (RecordTemplateSpec)spec; for (RecordTemplateSpec.Field field: recordSpec.getFields()) { results.add(field.getType()); } } else if (spec instanceof UnionTemplateSpec) { UnionTemplateSpec unionSpec = (UnionTemplateSpec)spec; for (UnionTemplateSpec.Member member: unionSpec.getMembers()) { results.add(member.getClassTemplateSpec()); } } else if (spec instanceof CourierMapTemplateSpec) { CourierMapTemplateSpec mapSpec = (CourierMapTemplateSpec)spec; if (mapSpec.getKeyClass() != null) { results.add(mapSpec.getKeyClass()); } results.add(mapSpec.getValueClass()); } else if (spec instanceof ArrayTemplateSpec) { ArrayTemplateSpec arraySpec = (ArrayTemplateSpec)spec; results.add(arraySpec.getItemClass()); } return results; }
/** * Returns Java source code that computes the hashCodes of each of the fields, as a list of * parameters. * * This is the same as {@link #fieldList} except when the fields are Java arrays, in which * case they are wrapped with a utilty method to hash them correctly. E.g. * * <code> * intField, stringField, mapField, Arrays.deepHashCode(javaArrayField), recordField * </code> * * @param fields provides the fields to include in the hashcode expression. * @return a java expression that calculates a hash code. */ public String hashCodeList(List<RecordTemplateSpec.Field> fields) { StringBuilder sb = new StringBuilder(); Iterator<RecordTemplateSpec.Field> iter = fields.iterator(); while(iter.hasNext()) { RecordTemplateSpec.Field field = iter.next(); Type schemaType = field.getSchemaField().getType().getType(); sb.append(escapeKeyword(field.getSchemaField().getName())); if (iter.hasNext()) sb.append(", "); } return sb.toString(); } }
boolean explicit = Boolean.TRUE.equals(field.getSchemaField().getProperties().get("explicit")); if (!explicit && field.getSchemaField().getOptional()) { return "nil"; } else { CustomInfoSpec customInfo = field.getCustomInfo(); ClassTemplateSpec fieldType; if (customInfo != null) { fieldType = ClassTemplateSpec.createFromDataSchema(refSchema); String coercer = customInfo.getCoercerClass().getClassName(); String uncoerced = toLiteral(fieldType.getSchema(), field.getSchemaField().getDefault()); if (uncoerced == null) { return null; fieldType = field.getType(); return toLiteral(fieldType.getSchema(), field.getSchemaField().getDefault());
final RecordTemplateSpec.Field newField = new RecordTemplateSpec.Field(); newField.setSchemaField(field); newField.setType(fieldClass); newField.setDataClass(determineDataClass(field.getType(), recordClass, field.getName())); newField.setCustomInfo(customInfo);
/** * Returns the fields as a list of parameter declarations for inclusion in Java source. E.g.: * * <code> * org.example.Record field1, List<Integer> field2, Map<String, Integer> field3, Integer field4 * </code> * * @param fields provides the fields to construct a parameter declaration list for. * @return the fields as a list of parameter declarations. */ public String fieldAndTypeList(List<RecordTemplateSpec.Field> fields) { StringBuilder sb = new StringBuilder(); Iterator<RecordTemplateSpec.Field> iter = fields.iterator(); while(iter.hasNext()) { RecordTemplateSpec.Field field = iter.next(); sb.append(toOptionalType(field.getType(), field.getSchemaField().getOptional())); sb.append(" "); sb.append(escapeKeyword(field.getSchemaField().getName())); if (iter.hasNext()) sb.append(", "); } return sb.toString(); }
/** * Returns the fields as a list of parameters for inclusion in Java source. E.g.: * * <code> * field1, field2, field3, field4 * </code> * * @param fields provides the fields to construct a parameter list for. * @return the fields as a list of parameters for inclusion in Java source. */ public String fieldList(List<RecordTemplateSpec.Field> fields) { StringBuilder sb = new StringBuilder(); Iterator<RecordTemplateSpec.Field> iter = fields.iterator(); while(iter.hasNext()) { RecordTemplateSpec.Field field = iter.next(); sb.append(escapeKeyword(field.getSchemaField().getName())); if (iter.hasNext()) sb.append(", "); } return sb.toString(); }
public String typeName() { // To resolve type name we have to determine whether to use the DataSchema in _field.getType() or // the one in _field.getSchemaField().getType(). We reach first for the schemaField as it does not swallow // Typerefs. (e.g. if a type was defined as CustomInt, it will give us the string CustomInt, whereas // field.getType() would dereference all the way to the bottom). // // The only problem with schemaField is that it _does_ swallow the type names for enclosed unions. ARGH // can we catch a break?? Thankfully in the case of the enclosed union it ends up returning null, so // we back off to _field.getType() if schemaField returned null. TSTypeSyntax candidateSyntax = createTypeSyntax(_schemaField().getType()); if (candidateSyntax.typeName() == null || "".equals(candidateSyntax)) { candidateSyntax = createTypeSyntax(_field.getType()); } return typeNameQualifiedByEnclosingClass(candidateSyntax); }
public String toSetAccessor(String anchor, RecordTemplateSpec.Field field) { return writeTypeExpr(expr(anchor), selectFieldType(field), field.getCustomInfo()).toSwiftCode(); }
private ClassTemplateSpec selectFieldType(RecordTemplateSpec.Field field) { return getDereferencedType(field.getCustomInfo(), field.getType()); }
private RecordDataSchema.Field _schemaField() { return _field.getSchemaField(); } private TSTypeSyntax _fieldTypeSyntax() {
public boolean isOptional(RecordTemplateSpec.Field field) { boolean isFieldOptional = field.getSchemaField().getOptional(); return isFieldOptional || swiftProperties.optionality == Optionality.REQUIRED_FIELDS_MAY_BE_ABSENT; }
public String toGetAccessor(String anchor, RecordTemplateSpec.Field field) { CustomInfoSpec customInfo = field.getCustomInfo(); return readTypeExpr( expr(anchor), selectFieldType(field), customInfo, syntax.isOptional(field)).toSwiftCode(); }