/** * Constructs a similar Protocol instance with the same {@code name}, {@code doc}, and {@code namespace} as {code p} * has. It also copies all the {@code props}. */ public Protocol(Protocol p) { this(p.getName(), p.getDoc(), p.getNamespace()); putAll(p); }
private Protocol addStringType(Protocol p) { if (stringType != StringType.String) return p; Protocol newP = new Protocol(p.getName(), p.getDoc(), p.getNamespace()); Map<Schema,Schema> types = new LinkedHashMap<>(); for (Map.Entry<String, Object> a : p.getObjectProps().entrySet()) { newP.addProp(a.getKey(), a.getValue()); } // annotate types Collection<Schema> namedTypes = new LinkedHashSet<>(); for (Schema s : p.getTypes()) namedTypes.add(addStringType(s, types)); newP.setTypes(namedTypes); // annotate messages Map<String,Message> newM = newP.getMessages(); for (Message m : p.getMessages().values()) newM.put(m.getName(), m.isOneWay() ? newP.createMessage(m, addStringType(m.getRequest(), types)) : newP.createMessage(m, addStringType(m.getRequest(), types), addStringType(m.getResponse(), types), addStringType(m.getErrors(), types))); return newP; }
@Test public void testParsing() throws IOException { Protocol protocol = getSimpleProtocol(); assertEquals(protocol.getDoc(), "Protocol used for testing."); assertEquals(6, protocol.getMessages().size()); assertEquals("Pretend you're in a cave!", protocol.getMessages().get("echo").getDoc()); }
Protocol result = new Protocol(protocol.getName(), protocol.getDoc(), protocol.getNamespace()); final Collection<Schema> types = protocol.getTypes();
private OutputFile compileInterface(Protocol protocol) { OutputFile outputFile = new OutputFile(); String mangledName = mangle(protocol.getName()); outputFile.path = makePath(mangledName, protocol.getNamespace()); StringBuilder out = new StringBuilder(); header(out, protocol.getNamespace()); doc(out, 1, protocol.getDoc()); line(out, 0, "public interface " + mangledName + " {"); line(out, 1, "public static final org.apache.avro.Protocol PROTOCOL = org.apache.avro.Protocol.parse(\"" +esc(protocol)+"\");"); for (Map.Entry<String,Message> e : protocol.getMessages().entrySet()) { String name = e.getKey(); Message message = e.getValue(); Schema request = message.getRequest(); Schema response = message.getResponse(); doc(out, 1, e.getValue().getDoc()); line(out, 1, unbox(response)+" "+ mangle(name)+"("+params(request)+")"); line(out, 2,"throws org.apache.avro.ipc.AvroRemoteException"+errors(message.getErrors())+";"); } line(out, 0, "}"); outputFile.contents = out.toString(); return outputFile; }
private OutputFile compileInterface(Protocol protocol) { OutputFile outputFile = new OutputFile(); String mangledName = mangle(protocol.getName()); outputFile.path = makePath(mangledName, protocol.getNamespace()); StringBuilder out = new StringBuilder(); header(out, protocol.getNamespace()); doc(out, 1, protocol.getDoc()); line(out, 0, "public interface " + mangledName + " {"); line(out, 1, "public static final org.apache.avro.Protocol PROTOCOL = org.apache.avro.Protocol.parse(\"" +esc(protocol)+"\");"); for (Map.Entry<String,Message> e : protocol.getMessages().entrySet()) { String name = e.getKey(); Message message = e.getValue(); Schema request = message.getRequest(); String response = message.isOneWay() ? "void" : unbox(message.getResponse()); doc(out, 1, e.getValue().getDoc()); line(out, 1, response+" "+ mangle(name)+"("+params(request)+")" + (message.isOneWay() ? "" : (" throws org.apache.avro.ipc.AvroRemoteException" +errors(message.getErrors()))) +";"); } line(out, 0, "}"); outputFile.contents = out.toString(); return outputFile; }