SchemaUtils { @Beta public static void writeIdlProtocol(final String protocolName, final String protocolNameSpace, final Appendable appendable, final Schema... schemas) throws IOException { if (protocolNameSpace != null) { appendable.append("@namespace(\"").append(protocolNameSpace).append("\")\n"); } appendable.append("protocol ").append(protocolName).append(" {\n\n"); if (isIdlCycleSupport()) { writeIdl(appendable, new HashSet<String>(4), protocolNameSpace, schemas); } else { writeIdlLegacy(appendable, new HashSet<String>(4), protocolNameSpace, schemas); } appendable.append("}\n"); } private  SchemaUtils(); static boolean isIdlCycleSupport(); static void copyAliases(final Schema from, final Schema to); static void copyAliases(final Schema.Field from, final Schema.Field to); static void copyLogicalTypes(final Schema from, final Schema to); static void copyProperties(final JsonProperties from, final JsonProperties to); static boolean isNamedType(final Schema schema); static String getJavaClassName(final Schema schema); @Beta static void writeIdlProtocol(final String protocolName, final String protocolNameSpace,
          final Appendable appendable, final Schema... schemas); @Beta static void writeIdl(final Appendable appendable,
          final Set<String> alreadyDeclared, final String protocolNameSpace, final Schema ... pschemas); static JsonGenerator createJsonGenerator(final Appendable appendable); @Beta @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED") static void writeIdlLegacy(final Appendable appendable,
          final Set<String> alreadyDeclared, final String protocolNameSpace, final Schema ... pschemas); static void writeSchemaAttributes(final Schema schema,
          final Appendable appendable, final JsonGenerator jsonGen, final boolean crBetween); static void writeJsonProperties(final JsonProperties props,
          final Appendable appendable, final JsonGenerator jsonGen, final boolean crBetween); static final BiConsumer<Schema, Schema> SCHEMA_ESENTIALS; static final BiConsumer<Schema.Field, Schema.Field> FIELD_ESENTIALS; static final BiConsumer<Schema, Schema> SCHEMA_EVERYTHING; static final BiConsumer<Schema.Field, Schema.Field> FIELD_EVERYTHING; }
@Test public void testIdlGenerator() throws IOException, ParseException { StringWriter idlStrWriter = new StringWriter(); Schema rs = SchemaBuilder.builder().record("TestRecord2") .namespace("test") .doc("record doc") .aliases("recordAlias") .prop("propKey", "propVal") .fields() .requiredBoolean("isThere") .nullableString("strField", null) .name("customField") .orderAscending() .aliases("blaAlias") .prop("fprop", "fval") .prop("fprp2", "fval2") .type(Schema.createArray(Schema.createEnum("someEnum", "enum doc", "test", Arrays.asList("A", "B")))) .withDefault(Collections.EMPTY_LIST) .name("fixedField") .doc("fixedField doc") .type(Schema.createFixed("Myfixed", "fixed doc", "test2", 16)) .noDefault() .endRecord(); SchemaUtils.writeIdlProtocol("TestProtocol", "test", idlStrWriter, rs); LOG.debug("writer = {}", idlStrWriter); String idlStr = idlStrWriter.toString(); Assert.assertFalse(idlStr.isEmpty()); Idl idl = new Idl(new StringReader(idlStr)); idl.setSource(""); Protocol protocol = idl.CompilationUnit(); LOG.debug("Protocol = {}", protocol); Schema rs2 = protocol.getType("test.TestRecord2"); Assert.assertEquals(rs, rs2); }
@Test public void testIdlGenerator2() throws IOException, ParseException { StringWriter idlStrWriter = new StringWriter(); Schema rs = SchemaBuilder.builder().record("TestRecord2") .namespace("test") .doc("record doc") .aliases("recordAlias") .prop("propKey", "propVal") .fields() .requiredBoolean("isThere") .nullableString("strField", null) .endRecord(); SchemaUtils.writeIdlProtocol("TestProtocol", "test", idlStrWriter, rs); LOG.debug("writer = {}", idlStrWriter); String idlStr = idlStrWriter.toString(); Assert.assertFalse(idlStr.isEmpty()); Idl idl = new Idl(new StringReader(idlStr)); idl.setSource(""); Protocol protocol = idl.CompilationUnit(); LOG.debug("Protocol = {}", protocol); Schema rs2 = protocol.getType("test.TestRecord2"); Assert.assertEquals(rs, rs2); }