/** * Parse a single {@link HollowSchema} from the provided String. * * @param schema the schema as a string * @return the schema * @throws IOException if the schema cannot be parsed */ public static HollowSchema parseSchema(String schema) throws IOException { StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(schema)); configureTokenizer(tokenizer); return parseSchema(tokenizer); }
/** * Parse a collection of {@link HollowSchema}s from the provided Reader. * * @param reader the reader * @return the list of schema * @throws IOException if the schema cannot be parsed */ public static List<HollowSchema> parseCollectionOfSchemas(Reader reader) throws IOException { StreamTokenizer tokenizer = new StreamTokenizer(reader); configureTokenizer(tokenizer); List<HollowSchema> schemaList = new ArrayList<HollowSchema>(); HollowSchema schema = parseSchema(tokenizer); while (schema != null) { schemaList.add(schema); schema = parseSchema(tokenizer); } return schemaList; }
@Test public void parsesListSchema() throws IOException { String listSchema = "ListOfTypeA List<TypeA>;\n"; HollowListSchema schema = (HollowListSchema) HollowSchemaParser.parseSchema(listSchema); Assert.assertEquals("ListOfTypeA", schema.getName()); Assert.assertEquals("TypeA", schema.getElementType()); Assert.assertEquals(schema, HollowSchemaParser.parseSchema(schema.toString())); }
@Test public void parsesSetSchema() throws IOException { String listSchema = "SetOfTypeA Set<TypeA>;\n"; HollowSetSchema schema = (HollowSetSchema) HollowSchemaParser.parseSchema(listSchema); Assert.assertEquals("SetOfTypeA", schema.getName()); Assert.assertEquals("TypeA", schema.getElementType()); Assert.assertEquals(schema, HollowSchemaParser.parseSchema(schema.toString())); }
@Test public void parsesSetSchemaWithMultiFieldKey() throws IOException { String listSchema = "SetOfTypeA Set<TypeA> @HashKey(id.value, region.country.id, key);\n"; HollowSetSchema schema = (HollowSetSchema) HollowSchemaParser.parseSchema(listSchema); Assert.assertEquals("SetOfTypeA", schema.getName()); Assert.assertEquals("TypeA", schema.getElementType()); Assert.assertEquals(new PrimaryKey("TypeA", "id.value", "region.country.id", "key"), schema.getHashKey()); Assert.assertEquals(schema, HollowSchemaParser.parseSchema(schema.toString())); }
@Test public void parsesMapSchema() throws IOException { String listSchema = "MapOfStringToTypeA Map<String, TypeA>;\n"; HollowMapSchema schema = (HollowMapSchema) HollowSchemaParser.parseSchema(listSchema); Assert.assertEquals("MapOfStringToTypeA", schema.getName()); Assert.assertEquals("String", schema.getKeyType()); Assert.assertEquals("TypeA", schema.getValueType()); Assert.assertEquals(schema, HollowSchemaParser.parseSchema(schema.toString())); }
@Test public void parsesSetSchemaWithKey() throws IOException { String listSchema = "SetOfTypeA Set<TypeA> @HashKey(id.value);\n"; HollowSetSchema schema = (HollowSetSchema) HollowSchemaParser.parseSchema(listSchema); Assert.assertEquals("SetOfTypeA", schema.getName()); Assert.assertEquals("TypeA", schema.getElementType()); Assert.assertEquals(new PrimaryKey("TypeA", "id.value"), schema.getHashKey()); Assert.assertEquals(schema, HollowSchemaParser.parseSchema(schema.toString())); }
@Test public void parsesMapSchemaWithMultiFieldPrimaryKey() throws IOException { String listSchema = "MapOfStringToTypeA Map<String, TypeA> @HashKey(id.value, region.country.id, key);\n"; HollowMapSchema schema = (HollowMapSchema) HollowSchemaParser.parseSchema(listSchema); Assert.assertEquals("MapOfStringToTypeA", schema.getName()); Assert.assertEquals("String", schema.getKeyType()); Assert.assertEquals("TypeA", schema.getValueType()); Assert.assertEquals(new PrimaryKey("String", "id.value", "region.country.id", "key"), schema.getHashKey()); Assert.assertEquals(schema, HollowSchemaParser.parseSchema(schema.toString())); }
@Test public void parsesMapSchemaWithPrimaryKey() throws IOException { String listSchema = "MapOfStringToTypeA Map<String, TypeA> @HashKey(value);\n"; HollowMapSchema schema = (HollowMapSchema) HollowSchemaParser.parseSchema(listSchema); Assert.assertEquals("MapOfStringToTypeA", schema.getName()); Assert.assertEquals("String", schema.getKeyType()); Assert.assertEquals("TypeA", schema.getValueType()); Assert.assertEquals(new PrimaryKey("String", "value"), schema.getHashKey()); Assert.assertEquals(schema, HollowSchemaParser.parseSchema(schema.toString())); }
@Test public void parsesObjectSchema() throws IOException { String objectSchema = "/* This is a comment\n" + " consisting of multiple lines */\n" + " TypeA {\n" + " int a1;\n" + " \tstring a2; //This is a comment\n" + " String a3;\n" + "}\n"; HollowObjectSchema schema = (HollowObjectSchema) HollowSchemaParser.parseSchema(objectSchema); Assert.assertEquals("TypeA", schema.getName()); Assert.assertEquals(3, schema.numFields()); Assert.assertEquals(FieldType.INT, schema.getFieldType(0)); Assert.assertEquals("a1", schema.getFieldName(0)); Assert.assertEquals(FieldType.STRING, schema.getFieldType(1)); Assert.assertEquals("a2", schema.getFieldName(1)); Assert.assertEquals(FieldType.REFERENCE, schema.getFieldType(2)); Assert.assertEquals("String", schema.getReferencedType(2)); Assert.assertEquals("a3", schema.getFieldName(2)); // HollowObjectSchame.toString is parsed properly Assert.assertEquals(schema, HollowSchemaParser.parseSchema(schema.toString())); }
@Test public void parsesObjectSchemaMultipleWithKey() throws IOException { String objectSchema = " TypeA @PrimaryKey(a1, a3.value) {\n" + " int a1;\n" + " string a2;\n" + " String a3;\n" + "}\n"; HollowObjectSchema schema = (HollowObjectSchema) HollowSchemaParser.parseSchema(objectSchema); Assert.assertEquals("TypeA", schema.getName()); Assert.assertEquals(3, schema.numFields()); Assert.assertEquals(FieldType.INT, schema.getFieldType(0)); Assert.assertEquals("a1", schema.getFieldName(0)); Assert.assertEquals(FieldType.STRING, schema.getFieldType(1)); Assert.assertEquals("a2", schema.getFieldName(1)); Assert.assertEquals(FieldType.REFERENCE, schema.getFieldType(2)); Assert.assertEquals("String", schema.getReferencedType(2)); Assert.assertEquals("a3", schema.getFieldName(2)); // Make sure primary key and HollowObjectSchame.toString is parsed properly Assert.assertEquals(new PrimaryKey("TypeA", "a1", "a3.value"), schema.getPrimaryKey()); Assert.assertEquals(schema, HollowSchemaParser.parseSchema(schema.toString())); }
@Test public void parsesObjectSchemaWithKey() throws IOException { String objectSchema = " TypeA @PrimaryKey(a1) {\n" + " int a1;\n" + " string a2;\n" + " String a3;\n" + "}\n"; HollowObjectSchema schema = (HollowObjectSchema) HollowSchemaParser.parseSchema(objectSchema); Assert.assertEquals("TypeA", schema.getName()); Assert.assertEquals(3, schema.numFields()); Assert.assertEquals(FieldType.INT, schema.getFieldType(0)); Assert.assertEquals("a1", schema.getFieldName(0)); Assert.assertEquals(FieldType.STRING, schema.getFieldType(1)); Assert.assertEquals("a2", schema.getFieldName(1)); Assert.assertEquals(FieldType.REFERENCE, schema.getFieldType(2)); Assert.assertEquals("String", schema.getReferencedType(2)); Assert.assertEquals("a3", schema.getFieldName(2)); // Make sure primary key and HollowObjectSchame.toString is parsed properly Assert.assertEquals(new PrimaryKey("TypeA", "a1"), schema.getPrimaryKey()); Assert.assertEquals(schema, HollowSchemaParser.parseSchema(schema.toString())); }