use of org.apache.avro.JsonProperties in project lemma by SeelabFhdo.
the class TransformToLemmaHandler method parseSchemas.
/**
* Parse Avro schemas from IFile
*/
private TransformToLemmaHandler.SchemasParsingResult parseSchemas(final IFile file) {
Function<File, JsonProperties> _xifexpression = null;
boolean _isProtocol = this.isProtocol(file);
if (_isProtocol) {
final Function<File, JsonProperties> _function = (File it) -> {
try {
return Protocol.parse(it);
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
};
_xifexpression = _function;
} else {
final Function<File, JsonProperties> _function_1 = (File it) -> {
try {
return new Schema.Parser().parse(it);
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
};
_xifexpression = _function_1;
}
final Function<File, JsonProperties> parseFunction = _xifexpression;
final JsonProperties parsedJsonProperties = parseFunction.apply(file.getLocation().toFile());
TransformToLemmaHandler.SchemasParsingResult _switchResult = null;
boolean _matched = false;
if (parsedJsonProperties instanceof Schema) {
_matched = true;
_switchResult = new TransformToLemmaHandler.SchemasParsingResult(((Schema) parsedJsonProperties));
}
if (!_matched) {
if (parsedJsonProperties instanceof Protocol) {
_matched = true;
_switchResult = new TransformToLemmaHandler.SchemasParsingResult(((Protocol) parsedJsonProperties));
}
}
if (!_matched) {
_switchResult = null;
}
return _switchResult;
}
use of org.apache.avro.JsonProperties in project avro-schema-support by opwvhk.
the class AvroSchemaToIdlAction method convertFiles.
protected void convertFiles(@NotNull Project project, @NotNull ConsoleView console, @NotNull List<VirtualFile> files) {
console.print("Converting " + files.size() + " " + AvroSchemaFileType.INSTANCE.getDisplayName() + " file(s) ", SYSTEM_OUTPUT);
console.print("to a single " + AvroIdlFileType.INSTANCE.getDisplayName() + " file\n", SYSTEM_OUTPUT);
final List<Schema> schemas = new ArrayList<>();
final Schema.Parser parser = new Schema.Parser();
for (VirtualFile file : files) {
try {
console.print("Parsing ", SYSTEM_OUTPUT);
console.printHyperlink(file.getName(), new OpenFileHyperlinkInfo(project, file, 0));
console.print("\n", SYSTEM_OUTPUT);
schemas.add(parser.parse(file.toNioPath().toFile()));
} catch (AvroRuntimeException | IOException e) {
console.print("Failed to parse Avro schema in " + file.getName() + "\n", ERROR_OUTPUT);
writeStackTrace(console, e);
return;
}
}
console.print("Asking for file to write Avro IDL to...\n", NORMAL_OUTPUT);
final VirtualFileWrapper wrapper = askForTargetFile(project, "Save as Avro IDL", "Choose the filename to save to", AvroIdlFileType.INSTANCE, files.get(0).getParent(), files.get(0).getNameWithoutExtension());
if (wrapper != null) {
final VirtualFile virtualFile = wrapper.getVirtualFile(true);
if (virtualFile != null) {
console.print("Writing Avro IDL to ", NORMAL_OUTPUT);
console.printHyperlink(virtualFile.getName(), new OpenFileHyperlinkInfo(project, virtualFile, 0));
console.print("\n", NORMAL_OUTPUT);
WriteCommandAction.runWriteCommandAction(project, actionTitle, "AvroIDL", () -> {
try (Writer writer = new OutputStreamWriter(virtualFile.getOutputStream(this))) {
final String protocolName = virtualFile.getNameWithoutExtension();
// Assume the first schema has the correct namespace.
final String namespace = schemas.get(0).getNamespace();
// TODO: switch to schemas when Avro supports the schema syntax.
// IdlUtils.writeIdlSchemas(writer, namespace, schemas);
JsonProperties emptyProperties = Schema.create(Schema.Type.NULL);
IdlUtils.writeIdlProtocol(writer, emptyProperties, namespace, protocolName, schemas, emptyList());
console.print("Wrote Avro IDL \"", NORMAL_OUTPUT);
console.print(protocolName, NORMAL_OUTPUT);
console.print("\" to ", NORMAL_OUTPUT);
console.printHyperlink(virtualFile.getName(), new OpenFileHyperlinkInfo(project, virtualFile, 0));
console.print("\n", NORMAL_OUTPUT);
FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, virtualFile), true);
} catch (RuntimeException | IOException e) {
console.print("Failed to write the Avro IDL to " + virtualFile.getName() + "\n" + e.getLocalizedMessage(), ERROR_OUTPUT);
writeStackTrace(console, e);
}
});
}
}
console.print("\nAction complete.\n", ConsoleViewContentType.SYSTEM_OUTPUT);
}
Aggregations