use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class FileFormatDataSchemaParser method parseSources.
public DataSchemaParser.ParseResult parseSources(String[] sources) throws IOException {
final DataSchemaParser.ParseResult result = new DataSchemaParser.ParseResult();
try {
for (String source : sources) {
final File sourceFile = new File(source);
if (sourceFile.exists()) {
if (sourceFile.isDirectory()) {
final FileUtil.FileExtensionFilter filter = new FileUtil.FileExtensionFilter(_schemaParserFactory.getLanguageExtension());
final List<File> sourceFilesInDirectory = FileUtil.listFiles(sourceFile, filter);
for (File f : sourceFilesInDirectory) {
parseFile(f, result);
result.getSourceFiles().add(f);
}
} else {
if (sourceFile.getName().endsWith(".jar")) {
final JarFile jarFile = new JarFile(sourceFile);
final Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
final JarEntry entry = entries.nextElement();
if (!entry.isDirectory() && entry.getName().endsWith(_schemaParserFactory.getLanguageExtension())) {
parseJarEntry(jarFile, entry, result);
}
}
} else {
parseFile(sourceFile, result);
}
result.getSourceFiles().add(sourceFile);
}
} else {
final StringBuilder errorMessage = new StringBuilder();
final DataSchema schema = _schemaResolver.findDataSchema(source, errorMessage);
if (schema == null) {
result._messageBuilder.append("File cannot be opened or schema name cannot be resolved: ").append(source).append("\n");
}
if (errorMessage.length() > 0) {
result._messageBuilder.append(errorMessage.toString());
}
}
}
if (result._messageBuilder.length() > 0) {
throw new IOException(result.getMessage());
}
for (Map.Entry<String, DataSchemaLocation> entry : _schemaResolver.nameToDataSchemaLocations().entrySet()) {
final DataSchema schema = _schemaResolver.bindings().get(entry.getKey());
result.getSchemaAndLocations().put(schema, entry.getValue());
}
return result;
} catch (RuntimeException e) {
if (result._messageBuilder.length() > 0) {
e = new RuntimeException("Unexpected " + e.getClass().getSimpleName() + " encountered.\n" + "This may be caused by the following parsing or processing errors:\n" + result.getMessage(), e);
}
throw e;
}
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class FileFormatDataSchemaParser method parseFile.
/**
* Parse a source that specifies a file (not a fully qualified schema name).
*
* @param schemaSourceFile provides the source file.
* @throws IOException if there is a file access error.
*/
private void parseFile(File schemaSourceFile, DataSchemaParser.ParseResult result) throws IOException {
final DataSchemaLocation location = getSchemaLocation(schemaSourceFile);
// if a the data schema has been resolved before, must skip parsing again, because one name can't be bound to two data schemas
if (_schemaResolver.locationResolved(location)) {
return;
}
final InputStream inputStream = new SchemaFileInputStream(schemaSourceFile);
final List<DataSchema> schemas = parseSchemaStream(inputStream, location, result);
for (DataSchema schema : schemas) {
if (schema instanceof NamedDataSchema) {
validateSchemaWithPath(schemaSourceFile.getAbsolutePath(), (NamedDataSchema) schema);
}
result.getSchemaAndLocations().put(schema, location);
}
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class JavaDataTemplateGenerator method generateRecordFieldAccessors.
private void generateRecordFieldAccessors(JDefinedClass templateClass, RecordTemplateSpec.Field field, JClass type, JVar schemaFieldVar) {
final RecordDataSchema.Field schemaField = field.getSchemaField();
final DataSchema fieldSchema = schemaField.getType();
final boolean isDirect = CodeUtil.isDirectType(fieldSchema);
final String wrappedOrDirect;
if (isDirect) {
wrappedOrDirect = (field.getCustomInfo() == null ? "Direct" : "CustomType");
} else {
wrappedOrDirect = "Wrapped";
}
final String capitalizedName = CodeUtil.capitalize(schemaField.getName());
final String fieldFieldName = "FIELD_" + capitalizedName;
final JFieldVar fieldField = templateClass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, RecordDataSchema.Field.class, fieldFieldName);
fieldField.init(schemaFieldVar.invoke("getField").arg(schemaField.getName()));
// Generate has method.
final JMethod has = templateClass.method(JMod.PUBLIC, getCodeModel().BOOLEAN, "has" + capitalizedName);
addAccessorDoc(templateClass, has, schemaField, "Existence checker");
setDeprecatedAnnotationAndJavadoc(has, schemaField);
final JBlock hasBody = has.body();
JExpression res = JExpr.invoke("contains").arg(fieldField);
hasBody._return(res);
if (_recordFieldRemove) {
// Generate remove method.
final String removeName = "remove" + capitalizedName;
final JMethod remove = templateClass.method(JMod.PUBLIC, getCodeModel().VOID, removeName);
addAccessorDoc(templateClass, remove, schemaField, "Remover");
setDeprecatedAnnotationAndJavadoc(remove, schemaField);
final JBlock removeBody = remove.body();
removeBody.invoke("remove").arg(fieldField);
}
final String getterName = JavaCodeUtil.getGetterName(getCodeModel(), type, capitalizedName);
if (_recordFieldAccessorWithMode) {
// Getter method with mode.
final JMethod getterWithMode = templateClass.method(JMod.PUBLIC, type, getterName);
addAccessorDoc(templateClass, getterWithMode, schemaField, "Getter");
setDeprecatedAnnotationAndJavadoc(getterWithMode, schemaField);
JVar modeParam = getterWithMode.param(_getModeClass, "mode");
final JBlock getterWithModeBody = getterWithMode.body();
res = JExpr.invoke("obtain" + wrappedOrDirect).arg(fieldField).arg(JExpr.dotclass(type)).arg(modeParam);
getterWithModeBody._return(res);
}
// Getter method without mode.
final JMethod getterWithoutMode = templateClass.method(JMod.PUBLIC, type, getterName);
addAccessorDoc(templateClass, getterWithoutMode, schemaField, "Getter");
setDeprecatedAnnotationAndJavadoc(getterWithoutMode, schemaField);
JCommentPart returnComment = getterWithoutMode.javadoc().addReturn();
if (schemaField.getOptional()) {
getterWithoutMode.annotate(Nullable.class);
returnComment.add("Optional field. Always check for null.");
} else {
getterWithoutMode.annotate(Nonnull.class);
returnComment.add("Required field. Could be null for partial record.");
}
final JBlock getterWithoutModeBody = getterWithoutMode.body();
res = JExpr.invoke("obtain" + wrappedOrDirect).arg(fieldField).arg(JExpr.dotclass(type)).arg(_strictGetMode);
getterWithoutModeBody._return(res);
// Determine dataClass
final JClass dataClass = generate(field.getDataClass());
final String setterName = "set" + capitalizedName;
if (_recordFieldAccessorWithMode) {
// Setter method with mode
final JMethod setterWithMode = templateClass.method(JMod.PUBLIC, templateClass, setterName);
addAccessorDoc(templateClass, setterWithMode, schemaField, "Setter");
setDeprecatedAnnotationAndJavadoc(setterWithMode, schemaField);
JVar param = setterWithMode.param(type, "value");
JVar modeParam = setterWithMode.param(_setModeClass, "mode");
JInvocation inv = setterWithMode.body().invoke("put" + wrappedOrDirect).arg(fieldField).arg(JExpr.dotclass(type));
dataClassArg(inv, dataClass).arg(param).arg(modeParam);
setterWithMode.body()._return(JExpr._this());
}
// Setter method without mode
final JMethod setter = templateClass.method(JMod.PUBLIC, templateClass, setterName);
addAccessorDoc(templateClass, setter, schemaField, "Setter");
setDeprecatedAnnotationAndJavadoc(setter, schemaField);
JVar param = setter.param(type, "value");
param.annotate(Nonnull.class);
JCommentPart paramDoc = setter.javadoc().addParam(param);
paramDoc.add("Must not be null. For more control, use setters with mode instead.");
JInvocation inv = setter.body().invoke("put" + wrappedOrDirect).arg(fieldField).arg(JExpr.dotclass(type));
dataClassArg(inv, dataClass).arg(param).arg(_disallowNullSetMode);
setter.body()._return(JExpr._this());
// Setter method without mode for unboxified type
if (!type.unboxify().equals(type)) {
final JMethod unboxifySetter = templateClass.method(JMod.PUBLIC, templateClass, setterName);
addAccessorDoc(templateClass, unboxifySetter, schemaField, "Setter");
setDeprecatedAnnotationAndJavadoc(unboxifySetter, schemaField);
param = unboxifySetter.param(type.unboxify(), "value");
inv = unboxifySetter.body().invoke("put" + wrappedOrDirect).arg(fieldField).arg(JExpr.dotclass(type));
dataClassArg(inv, dataClass).arg(param).arg(_disallowNullSetMode);
unboxifySetter.body()._return(JExpr._this());
}
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class JavaDataTemplateGenerator method generateUnionMemberAccessors.
private void generateUnionMemberAccessors(JDefinedClass unionClass, UnionTemplateSpec.Member member, JClass memberClass, JClass dataClass, JVar schemaField) {
final DataSchema memberType = member.getSchema();
final boolean isDirect = CodeUtil.isDirectType(memberType);
final String wrappedOrDirect;
if (isDirect) {
wrappedOrDirect = (member.getCustomInfo() == null ? "Direct" : "CustomType");
} else {
wrappedOrDirect = "Wrapped";
}
final String memberKey = memberType.getUnionMemberKey();
final String capitalizedName = CodeUtil.getUnionMemberName(memberType);
final String memberFieldName = "MEMBER_" + capitalizedName;
final JFieldVar memberField = unionClass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, DataSchema.class, memberFieldName);
memberField.init(schemaField.invoke("getType").arg(memberKey));
final String setterName = "set" + capitalizedName;
// Generate builder.
final JMethod createMethod = unionClass.method(JMod.PUBLIC | JMod.STATIC, unionClass, "create");
JVar param = createMethod.param(memberClass, "value");
final JVar newUnionVar = createMethod.body().decl(unionClass, "newUnion", JExpr._new(unionClass));
createMethod.body().invoke(newUnionVar, setterName).arg(param);
createMethod.body()._return(newUnionVar);
// Is method.
final JMethod is = unionClass.method(JMod.PUBLIC, getCodeModel().BOOLEAN, "is" + capitalizedName);
final JBlock isBody = is.body();
JExpression res = JExpr.invoke("memberIs").arg(memberKey);
isBody._return(res);
// Getter method.
final String getterName = "get" + capitalizedName;
final JMethod getter = unionClass.method(JMod.PUBLIC, memberClass, getterName);
final JBlock getterBody = getter.body();
res = JExpr.invoke("obtain" + wrappedOrDirect).arg(memberField).arg(JExpr.dotclass(memberClass)).arg(memberKey);
getterBody._return(res);
// Setter method.
final JMethod setter = unionClass.method(JMod.PUBLIC, Void.TYPE, setterName);
param = setter.param(memberClass, "value");
final JInvocation inv = setter.body().invoke("select" + wrappedOrDirect).arg(memberField).arg(JExpr.dotclass(memberClass));
dataClassArg(inv, dataClass).arg(memberKey).arg(param);
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class TemplateSpecGenerator method generateMap.
private MapTemplateSpec generateMap(MapDataSchema schema, ClassTemplateSpec enclosingClass, String memberName) {
final DataSchema valueSchema = schema.getValues();
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(valueSchema, enclosingClass, memberName);
return (MapTemplateSpec) classInfo.existingClass;
}
final MapTemplateSpec mapClass = (MapTemplateSpec) classInfo.definedClass;
registerClassTemplateSpec(schema, mapClass);
mapClass.setValueClass(processSchema(valueSchema, enclosingClass, memberName));
mapClass.setValueDataClass(determineDataClass(valueSchema, enclosingClass, memberName));
final CustomInfoSpec customInfo = getImmediateCustomInfo(valueSchema);
mapClass.setCustomInfo(customInfo);
return mapClass;
}
Aggregations