@Override public String typeName() { // Sadly the behavior of this function is indirectly controlled by the one calling it: TSRecordFieldSyntax. // That class has the unfortunate behavior that it can produce 2 different ClassTemplateSpecs, one of which works for // some cases, and one of which works for the others. See its own "typeName" definition for details but essentially // it will give us one of the ClassTemplateSpecs and call typeName. If we then return null // then it will give it a shot with the other ClassTemplateSpec. Unfortunately we have to do this because if // we try to just use the first one, we will return "Array<null>". This is also why we special-case unions here. // we have to access a specific ClassTemplate boolean itemIsUnion = _template.getItemClass() instanceof UnionTemplateSpec; TSTypeSyntax itemTypeSyntax = itemIsUnion? createTypeSyntax(_template.getItemClass()): _itemTypeSyntax(); String itemTypeName = typeNameQualifiedByEnclosingClass(itemTypeSyntax); return itemTypeName == null? null: "Array<" + itemTypeName + ">"; }
private ClassTemplateSpec selectArrayItemsType(ArrayTemplateSpec arraySpec) { return getDereferencedType(arraySpec.getCustomInfo(), arraySpec.getItemClass()); }
private TSTypeSyntax _itemTypeSyntax() { return createTypeSyntax(_template.getSchema().getItems()); } }
private ArrayTemplateSpec generateArray(ArrayDataSchema schema, ClassTemplateSpec enclosingClass, String memberName) { final DataSchema itemSchema = schema.getItems(); final ClassInfo classInfo = classInfoForUnnamed(enclosingClass, memberName, schema); if (classInfo.existingClass != null) { /* When type refs are used as item types inside some unnamed complex schemas like map and array, * the type refs are de-referenced and the underlying real type is used in the generated class. * In those cases the type refs are not processed by the class generation logic, an explicit * schema processing is necessary in order to processSchema the data template classes for those type * refs. */ processSchema(itemSchema, enclosingClass, memberName); return (ArrayTemplateSpec) classInfo.existingClass; } final ArrayTemplateSpec arrayClass = (ArrayTemplateSpec) classInfo.definedClass; registerClassTemplateSpec(schema, arrayClass); arrayClass.setItemClass(processSchema(itemSchema, enclosingClass, memberName)); arrayClass.setItemDataClass(determineDataClass(itemSchema, enclosingClass, memberName)); final CustomInfoSpec customInfo = getImmediateCustomInfo(itemSchema); arrayClass.setCustomInfo(customInfo); return arrayClass; }
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); } }
ArrayTemplateSpec arraySpec = (ArrayTemplateSpec)spec; ClassTemplateSpec itemSpec = selectArrayItemsType(arraySpec); CustomInfoSpec arrayCustomInfo = arraySpec.getCustomInfo(); if (arrayCustomInfo != null) { Expr coercer = expr(arrayCustomInfo.getCoercerClass().getClassName());
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; }
return "[String: " + toTypeString(((CourierMapTemplateSpec) spec).getValueClass()) + "]"; } else if (schemaType == Type.ARRAY) { return "[" + toTypeString(((ArrayTemplateSpec) spec).getItemClass()) + "]"; } else { throw new IllegalArgumentException("unrecognized type: " + schemaType);
return "Map<String, " + toType(((CourierMapTemplateSpec) spec).getValueClass(), Optionality.REQUIRED_FIELDS_MAY_BE_ABSENT) + ">"; } else if (schemaType == Type.ARRAY) { return "List<" + toType(((ArrayTemplateSpec) spec).getItemClass(), Optionality.REQUIRED_FIELDS_MAY_BE_ABSENT) + ">"; } else { throw new IllegalArgumentException("unrecognized type: " + schemaType);