Search in sources :

Example 11 with PegasusSchemaParser

use of com.linkedin.data.schema.PegasusSchemaParser in project rest.li by linkedin.

the class AbstractGenerator method parseSchema.

/**
 * Parse a source file to obtain the data schemas contained within.
 *
 * @param schemaSourceFile provides the source file.
 * @return the data schemas within the source file.
 * @throws IOException if there is a file access error.
 */
protected List<DataSchema> parseSchema(final File schemaSourceFile) throws IOException {
    PegasusSchemaParser parser = AbstractSchemaParser.parserForFile(schemaSourceFile, getSchemaResolver());
    FileInputStream schemaStream = new SchemaFileInputStream(schemaSourceFile);
    try {
        parser.setLocation(new FileDataSchemaLocation(schemaSourceFile));
        parser.parse(schemaStream);
        if (parser.hasError()) {
            return Collections.emptyList();
        }
        return parser.topLevelDataSchemas();
    } finally {
        schemaStream.close();
        if (parser.hasError()) {
            getMessage().append(schemaSourceFile.getPath() + ",");
            getMessage().append(parser.errorMessage());
        }
    }
}
Also used : FileDataSchemaLocation(com.linkedin.data.schema.resolver.FileDataSchemaLocation) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) FileInputStream(java.io.FileInputStream)

Example 12 with PegasusSchemaParser

use of com.linkedin.data.schema.PegasusSchemaParser in project rest.li by linkedin.

the class AbstractDataSchemaResolver method parse.

/**
 * Read an {@link InputStream} and parse the {@link InputStream} looking for the
 * specified name.
 *
 * @param inputStream to parse.
 * @param location of the input source.
 * @param name to locate.
 * @param errorMessageBuilder to append error messages to.
 * @return the {@link NamedDataSchema} is found in the input stream, else return null.
 */
protected NamedDataSchema parse(InputStream inputStream, final DataSchemaLocation location, String name, StringBuilder errorMessageBuilder) {
    NamedDataSchema schema = null;
    PegasusSchemaParser parser = _parserFactory.create(_dependencyResolver);
    parser.setLocation(location);
    parser.parse(new FilterInputStream(inputStream) {

        @Override
        public String toString() {
            return location.toString();
        }
    });
    if (parser.hasError()) {
        errorMessageBuilder.append("Error parsing ").append(location).append(" for \"").append(name).append("\".\n");
        errorMessageBuilder.append(parser.errorMessageBuilder());
        errorMessageBuilder.append("Done parsing ").append(location).append(".\n");
        addBadLocation(location);
    } else {
        DataSchema found = _nameToDataSchema.get(name);
        if (found != null && found instanceof NamedDataSchema) {
            schema = (NamedDataSchema) found;
        }
    }
    return schema;
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) FilterInputStream(java.io.FilterInputStream)

Example 13 with PegasusSchemaParser

use of com.linkedin.data.schema.PegasusSchemaParser in project rest.li by linkedin.

the class DataTemplateUtil method parseSchema.

/**
 * Parse data schema encoded in any format to obtain a {@link DataSchema}.
 *
 * @param schemaText the encoded data schema.
 * @param schemaFormatType the format in which the schema is encoded.
 * @param schemaResolver resolver for resolving referenced schemas.
 * @return the {@link DataSchema} parsed from the encoded data schema.
 * @throws IllegalArgumentException if the encoded data schema is invalid or there is more than one top-level schema.
 */
public static DataSchema parseSchema(String schemaText, DataSchemaResolver schemaResolver, SchemaFormatType schemaFormatType) throws IllegalArgumentException {
    final PegasusSchemaParser parser = schemaFormatType.getSchemaParserFactory().create(schemaResolver);
    parser.parse(schemaText);
    if (parser.hasError()) {
        if (debug) {
            out.println(parser.errorMessage());
        }
        throw new IllegalArgumentException(parser.errorMessage());
    }
    if (parser.topLevelDataSchemas().size() != 1) {
        throw new IllegalArgumentException("More than one top level schema");
    }
    return parser.topLevelDataSchemas().get(0);
}
Also used : PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser)

Example 14 with PegasusSchemaParser

use of com.linkedin.data.schema.PegasusSchemaParser in project rest.li by linkedin.

the class TestConversions method testConvertDataMapToDataSchema.

@Test
public void testConvertDataMapToDataSchema() throws IOException {
    for (String good : goodInputs) {
        NamedDataSchema dataSchema = (NamedDataSchema) TestUtil.dataSchemaFromString(good);
        DataMap mapFromString = TestUtil.dataMapFromString(good);
        PegasusSchemaParser parser = new SchemaParser();
        DataSchema schemaFromMap = Conversions.dataMapToDataSchema(mapFromString, parser);
        assertEquals(schemaFromMap, dataSchema);
    }
    for (String bad : badInputs) {
        DataMap mapFromString = TestUtil.dataMapFromString(bad);
        PegasusSchemaParser parser = new SchemaParser();
        DataSchema schemaFromMap = Conversions.dataMapToDataSchema(mapFromString, parser);
        assertNull(schemaFromMap);
        assertTrue(parser.hasError());
    }
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) SchemaParser(com.linkedin.data.schema.SchemaParser) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 15 with PegasusSchemaParser

use of com.linkedin.data.schema.PegasusSchemaParser in project rest.li by linkedin.

the class TestDataSchemaResolver method lookup.

public void lookup(DataSchemaResolver resolver, String[][] lookups, char separator, boolean debug, SchemaFormatType extension) {
    PegasusSchemaParser parser = extension.equals(PDSC) ? new SchemaParser(resolver) : new PdlSchemaParser(resolver);
    for (String[] entry : lookups) {
        String name = entry[0];
        String expectFound = entry[1];
        String expected = entry[2];
        DataSchema schema = parser.lookupName(name);
        if (debug) {
            out.println("----" + name + "-----");
        }
        String errorMessage = parser.errorMessage();
        if (debug && errorMessage.isEmpty() == false) {
            out.println(errorMessage);
        }
        if (expectFound == ERROR) {
            assertTrue(parser.hasError());
            assertTrue(expected == null || errorMessage.contains(expected), "Expected: " + expected + "\n Actual: " + errorMessage);
        } else if (expectFound == FOUND) {
            assertTrue(schema != null);
            String schemaText = schema.toString();
            if (debug) {
                out.println(schemaText);
            }
            assertFalse(parser.hasError());
            assertTrue(schema instanceof NamedDataSchema);
            NamedDataSchema namedSchema = (NamedDataSchema) schema;
            assertEquals(namedSchema.getFullName(), name);
            assertTrue(schemaText.contains(expected));
            assertTrue(resolver.bindings().containsKey(name));
            assertSame(resolver.bindings().get(name), namedSchema);
            String location = entry[3];
            DataSchemaLocation namedSchemalocation = resolver.nameToDataSchemaLocations().get(name);
            String locationNorm;
            if (namedSchemalocation.toString().contains(".jar")) {
                locationNorm = location.replace(separator, '/');
            } else {
                locationNorm = location.replace('/', separator);
            }
            assertNotNull(namedSchemalocation);
            assertEquals(namedSchemalocation.toString().indexOf(locationNorm), namedSchemalocation.toString().length() - locationNorm.length());
            assertTrue(resolver.locationResolved(namedSchemalocation));
        } else if (expectFound == NOT_FOUND) {
            assertTrue(schema == null);
            assertFalse(parser.hasError());
            assertTrue(expected == null || errorMessage.contains(expected));
            assertFalse(resolver.bindings().containsKey(name));
        } else {
            assertTrue(false);
        }
    }
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) SchemaParser(com.linkedin.data.schema.SchemaParser) PdlSchemaParser(com.linkedin.data.schema.grammar.PdlSchemaParser) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) PdlSchemaParser(com.linkedin.data.schema.grammar.PdlSchemaParser) DataSchemaLocation(com.linkedin.data.schema.DataSchemaLocation)

Aggregations

PegasusSchemaParser (com.linkedin.data.schema.PegasusSchemaParser)17 DataSchema (com.linkedin.data.schema.DataSchema)11 SchemaParser (com.linkedin.data.schema.SchemaParser)11 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)7 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)7 PdlSchemaParser (com.linkedin.data.schema.grammar.PdlSchemaParser)6 DataMap (com.linkedin.data.DataMap)5 Schema (org.apache.avro.Schema)4 Test (org.testng.annotations.Test)4 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)3 GenericRecord (org.apache.avro.generic.GenericRecord)3 BeforeTest (org.testng.annotations.BeforeTest)3 DataSchemaLocation (com.linkedin.data.schema.DataSchemaLocation)2 ArrayList (java.util.ArrayList)2 TestUtil.dataSchemaFromString (com.linkedin.data.TestUtil.dataSchemaFromString)1 DataSchemaResolver (com.linkedin.data.schema.DataSchemaResolver)1 DataSchemaTraverse (com.linkedin.data.schema.DataSchemaTraverse)1 SchemaParserFactory (com.linkedin.data.schema.SchemaParserFactory)1 DefaultDataSchemaResolver (com.linkedin.data.schema.resolver.DefaultDataSchemaResolver)1 FileDataSchemaLocation (com.linkedin.data.schema.resolver.FileDataSchemaLocation)1