Search in sources :

Example 1 with ProgramRegistry

use of net.morimekta.providence.reflect.util.ProgramRegistry in project providence by morimekta.

the class CMessageBuilderTest method setUp.

@Before
public void setUp() throws IOException {
    File file = tmp.newFile("test.thrift").getCanonicalFile().getAbsoluteFile();
    ProgramRegistry tmp = new ProgramRegistry();
    ThriftProgramParser parser = new ThriftProgramParser();
    ProgramConverter converter = new ProgramConverter(tmp);
    ProgramType program = parser.parse(getClass().getResourceAsStream("/parser/tests/test.thrift"), file, ImmutableList.of());
    tmp.putProgram(file.getPath(), converter.convert(file.getPath(), program));
    registry = tmp.registryForPath(file.getCanonicalFile().getAbsolutePath());
}
Also used : ProgramRegistry(net.morimekta.providence.reflect.util.ProgramRegistry) ProgramConverter(net.morimekta.providence.reflect.util.ProgramConverter) ProgramType(net.morimekta.providence.model.ProgramType) File(java.io.File) ThriftProgramParser(net.morimekta.providence.reflect.parser.ThriftProgramParser) Before(org.junit.Before)

Example 2 with ProgramRegistry

use of net.morimekta.providence.reflect.util.ProgramRegistry in project providence by morimekta.

the class CServiceMethodTest method setUp.

@Before
public void setUp() throws IOException {
    File numeric = ResourceUtils.copyResourceTo("/parser/calculator/number.thrift", tmp.getRoot());
    File calculator = ResourceUtils.copyResourceTo("/parser/calculator/calculator.thrift", tmp.getRoot());
    ProgramRegistry registry = new ProgramRegistry();
    ThriftProgramParser parser = new ThriftProgramParser();
    ProgramConverter converter = new ProgramConverter(registry);
    ProgramType program = parser.parse(new FileInputStream(numeric), numeric, ImmutableList.of(tmp.getRoot()));
    registry.putProgram(numeric.getCanonicalPath(), converter.convert(numeric.getCanonicalPath(), program));
    program = parser.parse(new FileInputStream(calculator), calculator, ImmutableList.of(tmp.getRoot()));
    registry.putProgram(calculator.getCanonicalPath(), converter.convert(calculator.getCanonicalPath(), program));
    this.registry = registry.registryForPath(calculator.getCanonicalPath());
}
Also used : ProgramRegistry(net.morimekta.providence.reflect.util.ProgramRegistry) ProgramConverter(net.morimekta.providence.reflect.util.ProgramConverter) ProgramType(net.morimekta.providence.model.ProgramType) File(java.io.File) ThriftProgramParser(net.morimekta.providence.reflect.parser.ThriftProgramParser) FileInputStream(java.io.FileInputStream) Before(org.junit.Before)

Example 3 with ProgramRegistry

use of net.morimekta.providence.reflect.util.ProgramRegistry in project providence by morimekta.

the class CServiceTest method setUp.

@Before
public void setUp() throws IOException {
    File numeric = ResourceUtils.copyResourceTo("/parser/calculator/number.thrift", tmp.getRoot());
    File calculator = ResourceUtils.copyResourceTo("/parser/calculator/calculator.thrift", tmp.getRoot());
    ProgramRegistry registry = new ProgramRegistry();
    ThriftProgramParser parser = new ThriftProgramParser();
    ProgramConverter converter = new ProgramConverter(registry);
    ProgramType program = parser.parse(new FileInputStream(numeric), numeric, ImmutableList.of(tmp.getRoot()));
    registry.putProgram(numeric.getCanonicalPath(), converter.convert(numeric.getCanonicalPath(), program));
    program = parser.parse(new FileInputStream(calculator), calculator, ImmutableList.of(tmp.getRoot()));
    registry.putProgram(calculator.getCanonicalPath(), converter.convert(calculator.getCanonicalPath(), program));
    this.registry = registry.registryForPath(calculator.getCanonicalPath());
}
Also used : ProgramRegistry(net.morimekta.providence.reflect.util.ProgramRegistry) ProgramConverter(net.morimekta.providence.reflect.util.ProgramConverter) ProgramType(net.morimekta.providence.model.ProgramType) File(java.io.File) ThriftProgramParser(net.morimekta.providence.reflect.parser.ThriftProgramParser) FileInputStream(java.io.FileInputStream) Before(org.junit.Before)

Example 4 with ProgramRegistry

use of net.morimekta.providence.reflect.util.ProgramRegistry in project providence by morimekta.

the class ConvertOptions method getServiceDefinition.

public PService getServiceDefinition() throws ParseException, IOException {
    ProgramRegistry registry = getProgramRegistry();
    PService srv = registry.getService(type, null);
    if (srv == null) {
        String programName = type.substring(0, type.lastIndexOf("."));
        programName = programName.replaceAll("[-.]", "_");
        Map<String, File> includeMap = FormatUtils.getIncludeMap(getRc(), includes);
        String filePath = includeMap.get(programName).toString();
        CProgram document = registry.registryForPath(filePath).getProgram();
        Set<String> services = new TreeSet<>(document.getServices().stream().map(s -> s.getQualifiedName()).collect(Collectors.toSet()));
        throw new ArgumentException("Unknown service %s in %s.\n" + "Found %s", type, ReflectionUtils.programNameFromPath(type), services.size() == 0 ? "none" : Strings.join(", ", services));
    }
    return srv;
}
Also used : ProgramRegistry(net.morimekta.providence.reflect.util.ProgramRegistry) TreeSet(java.util.TreeSet) CProgram(net.morimekta.providence.reflect.contained.CProgram) ArgumentException(net.morimekta.console.args.ArgumentException) File(java.io.File) PService(net.morimekta.providence.descriptor.PService)

Example 5 with ProgramRegistry

use of net.morimekta.providence.reflect.util.ProgramRegistry in project providence by morimekta.

the class Convert method run.

@SuppressWarnings("unchecked")
void run(String... args) {
    try {
        ArgumentParser cli = options.getArgumentParser("pvd", "Providence Converter");
        try {
            cli.parse(args);
            if (options.showHelp()) {
                System.out.println(cli.getProgramDescription());
                System.out.println("Usage: " + cli.getSingleLineUsage());
                System.out.println();
                System.out.println("Example code to run:");
                System.out.println("$ cat call.json | pvd -I thrift/ -S cal.Calculator");
                System.out.println("$ pvd -i binary,file:my.data -o json_protocol -I thrift/ cal.Operation");
                System.out.println();
                System.out.println("Note that when handling service calls, only 1 call can be converted.");
                System.out.println();
                cli.printUsage(System.out);
                System.out.println();
                System.out.println("Available formats are:");
                for (Format format : Format.values()) {
                    System.out.println(String.format(" - %-20s : %s", format.name(), format.desc));
                }
                return;
            }
            if (options.showVersion()) {
                System.out.println(cli.getProgramDescription());
                return;
            }
            if (options.listTypes) {
                ProgramRegistry registry = options.getProgramRegistry();
                for (ProgramTypeRegistry pr : registry.getLoadedRegistries()) {
                    CProgram program = pr.getProgram();
                    System.out.println(program.getProgramFilePath() + ":");
                    for (PDeclaredDescriptor dd : program.getDeclaredTypes()) {
                        if (dd instanceof CStructDescriptor) {
                            System.out.println("  struct    " + dd.getQualifiedName());
                        } else if (dd instanceof CUnionDescriptor) {
                            System.out.println("  union     " + dd.getQualifiedName());
                        } else if (dd instanceof CExceptionDescriptor) {
                            System.out.println("  exception " + dd.getQualifiedName());
                        }
                    }
                    for (PService s : program.getServices()) {
                        System.out.println("  service   " + s.getQualifiedName());
                    }
                }
                return;
            }
            cli.validate();
            if (options.getDefinition() == null) {
                MessageReader in = options.getServiceInput();
                MessageWriter out = options.getServiceOutput();
                PService service = options.getServiceDefinition();
                PServiceCall call = in.read(service);
                in.close();
                out.write(call);
                out.separator();
                out.close();
                // TODO: Validate we don't have garbage data after call.
                if (options.out.base64mime && options.out.file == null) {
                    System.out.println();
                }
            } else {
                AtomicInteger num = new AtomicInteger(0);
                int size = options.getInput().peek(m -> num.incrementAndGet()).collect(options.getOutput());
                if (num.get() == 0 || size == 0) {
                    throw new IOException("No data");
                }
                if (options.out.base64mime && options.out.file == null) {
                    System.out.println();
                }
            }
            return;
        } catch (ArgumentException e) {
            System.err.println(e.getMessage());
            System.out.println("Usage: " + cli.getSingleLineUsage());
            System.err.println();
            System.err.println("Run $ pvd --help # for available options.");
            if (options.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        } catch (SerializerException e) {
            System.out.flush();
            System.err.println();
            System.err.println(e.asString());
            if (options.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        } catch (UncheckedIOException | IOException e) {
            System.out.flush();
            System.err.println();
            System.err.println("I/O error: " + e.getMessage());
            if (options.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        }
    } catch (Exception e) {
        System.out.flush();
        System.err.println();
        System.err.println("Unchecked exception: " + e.getMessage());
        if (options.verbose()) {
            System.err.println();
            e.printStackTrace();
        }
    }
    exit(1);
}
Also used : ArgumentParser(net.morimekta.console.args.ArgumentParser) ProgramTypeRegistry(net.morimekta.providence.reflect.util.ProgramTypeRegistry) CProgram(net.morimekta.providence.reflect.contained.CProgram) IOException(java.io.IOException) PDeclaredDescriptor(net.morimekta.providence.descriptor.PDeclaredDescriptor) MessageWriter(net.morimekta.providence.mio.MessageWriter) ProgramRegistry(net.morimekta.providence.reflect.util.ProgramRegistry) ArgumentException(net.morimekta.console.args.ArgumentException) CUnionDescriptor(net.morimekta.providence.reflect.contained.CUnionDescriptor) UncheckedIOException(java.io.UncheckedIOException) PService(net.morimekta.providence.descriptor.PService) STTY(net.morimekta.console.util.STTY) PServiceCall(net.morimekta.providence.PServiceCall) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CExceptionDescriptor(net.morimekta.providence.reflect.contained.CExceptionDescriptor) CStructDescriptor(net.morimekta.providence.reflect.contained.CStructDescriptor) MessageReader(net.morimekta.providence.mio.MessageReader) Format(net.morimekta.providence.tools.common.formats.Format) SerializerException(net.morimekta.providence.serializer.SerializerException) ProgramRegistry(net.morimekta.providence.reflect.util.ProgramRegistry) CUnionDescriptor(net.morimekta.providence.reflect.contained.CUnionDescriptor) MessageReader(net.morimekta.providence.mio.MessageReader) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ArgumentParser(net.morimekta.console.args.ArgumentParser) SerializerException(net.morimekta.providence.serializer.SerializerException) IOException(java.io.IOException) ArgumentException(net.morimekta.console.args.ArgumentException) UncheckedIOException(java.io.UncheckedIOException) SerializerException(net.morimekta.providence.serializer.SerializerException) Format(net.morimekta.providence.tools.common.formats.Format) ProgramTypeRegistry(net.morimekta.providence.reflect.util.ProgramTypeRegistry) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CProgram(net.morimekta.providence.reflect.contained.CProgram) PServiceCall(net.morimekta.providence.PServiceCall) PDeclaredDescriptor(net.morimekta.providence.descriptor.PDeclaredDescriptor) MessageWriter(net.morimekta.providence.mio.MessageWriter) CStructDescriptor(net.morimekta.providence.reflect.contained.CStructDescriptor) ArgumentException(net.morimekta.console.args.ArgumentException) CExceptionDescriptor(net.morimekta.providence.reflect.contained.CExceptionDescriptor) PService(net.morimekta.providence.descriptor.PService)

Aggregations

ProgramRegistry (net.morimekta.providence.reflect.util.ProgramRegistry)6 File (java.io.File)4 ProgramType (net.morimekta.providence.model.ProgramType)3 ThriftProgramParser (net.morimekta.providence.reflect.parser.ThriftProgramParser)3 ProgramConverter (net.morimekta.providence.reflect.util.ProgramConverter)3 Before (org.junit.Before)3 FileInputStream (java.io.FileInputStream)2 ArgumentException (net.morimekta.console.args.ArgumentException)2 PService (net.morimekta.providence.descriptor.PService)2 CProgram (net.morimekta.providence.reflect.contained.CProgram)2 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 TreeSet (java.util.TreeSet)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ArgumentParser (net.morimekta.console.args.ArgumentParser)1 STTY (net.morimekta.console.util.STTY)1 PServiceCall (net.morimekta.providence.PServiceCall)1 PDeclaredDescriptor (net.morimekta.providence.descriptor.PDeclaredDescriptor)1 MessageReader (net.morimekta.providence.mio.MessageReader)1 MessageWriter (net.morimekta.providence.mio.MessageWriter)1