Search in sources :

Example 1 with JsonProperties

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;
}
Also used : Schema(org.apache.avro.Schema) JsonProperties(org.apache.avro.JsonProperties) Protocol(org.apache.avro.Protocol) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 2 with JsonProperties

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);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) AvroRuntimeException(org.apache.avro.AvroRuntimeException) IOException(java.io.IOException) OpenFileHyperlinkInfo(com.intellij.execution.filters.OpenFileHyperlinkInfo) AvroRuntimeException(org.apache.avro.AvroRuntimeException) JsonProperties(org.apache.avro.JsonProperties) OutputStreamWriter(java.io.OutputStreamWriter) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) VirtualFileWrapper(com.intellij.openapi.vfs.VirtualFileWrapper)

Aggregations

JsonProperties (org.apache.avro.JsonProperties)2 Schema (org.apache.avro.Schema)2 OpenFileHyperlinkInfo (com.intellij.execution.filters.OpenFileHyperlinkInfo)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 VirtualFileWrapper (com.intellij.openapi.vfs.VirtualFileWrapper)1 File (java.io.File)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 AvroRuntimeException (org.apache.avro.AvroRuntimeException)1 Protocol (org.apache.avro.Protocol)1 IFile (org.eclipse.core.resources.IFile)1