Search in sources :

Example 6 with DefaultDataSchemaResolver

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

the class PegasusSchemaSnapshotCompatibilityChecker method parseSchema.

private DataSchema parseSchema(File schemaFile) throws FileNotFoundException {
    PdlSchemaParser parser = new PdlSchemaParser(new DefaultDataSchemaResolver());
    parser.parse(new FileInputStream(schemaFile));
    if (parser.hasError()) {
        throw new RuntimeException(parser.errorMessage() + " Error while parsing file: " + schemaFile.toString());
    }
    List<DataSchema> topLevelDataSchemas = parser.topLevelDataSchemas();
    if (topLevelDataSchemas.size() != 1) {
        throw new RuntimeException("Could not parse schema : " + schemaFile.getAbsolutePath() + " The size of top level schemas is not 1.");
    }
    DataSchema topLevelDataSchema = topLevelDataSchemas.get(0);
    if (!(topLevelDataSchema instanceof NamedDataSchema)) {
        throw new RuntimeException("Invalid schema : " + schemaFile.getAbsolutePath() + ", the schema is not a named schema.");
    }
    return topLevelDataSchema;
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DefaultDataSchemaResolver(com.linkedin.data.schema.resolver.DefaultDataSchemaResolver) PdlSchemaParser(com.linkedin.data.schema.grammar.PdlSchemaParser) FileInputStream(java.io.FileInputStream)

Example 7 with DefaultDataSchemaResolver

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

the class RestLiResourceModelCompatibilityChecker method check.

/**
 * Check backwards compatibility between two idl (.restspec.json) files.
 *
 * @param prevRestspecPath previously existing idl file
 * @param currRestspecPath current idl file
 * @param compatLevel compatibility level which affects the return value
 * @return true if the check result conforms the compatibility level requirement
 *         e.g. false if backwards compatible changes are found but the level is equivalent
 */
public boolean check(String prevRestspecPath, String currRestspecPath, CompatibilityLevel compatLevel) {
    _prevRestspecPath = prevRestspecPath;
    _currRestspecPath = currRestspecPath;
    Stack<Object> path = new Stack<>();
    path.push("");
    ResourceSchema prevRec = null;
    ResourceSchema currRec = null;
    try {
        prevRec = _codec.readResourceSchema(new FileInputStream(prevRestspecPath));
    } catch (FileNotFoundException e) {
        _infoMap.addRestSpecInfo(CompatibilityInfo.Type.RESOURCE_NEW, path, currRestspecPath);
    } catch (IOException e) {
        _infoMap.addRestSpecInfo(CompatibilityInfo.Type.OTHER_ERROR, path, e.getMessage());
    }
    try {
        currRec = _codec.readResourceSchema(new FileInputStream(currRestspecPath));
    } catch (FileNotFoundException e) {
        _infoMap.addRestSpecInfo(CompatibilityInfo.Type.RESOURCE_MISSING, path, prevRestspecPath);
    } catch (Exception e) {
        _infoMap.addRestSpecInfo(CompatibilityInfo.Type.OTHER_ERROR, path, e.getMessage());
    }
    if (prevRec == null || currRec == null) {
        return _infoMap.isCompatible(compatLevel);
    }
    final DataSchemaResolver resolver;
    if (_resolverPath == null) {
        resolver = new DefaultDataSchemaResolver();
    } else {
        resolver = MultiFormatDataSchemaResolver.withBuiltinFormats(_resolverPath);
    }
    ResourceCompatibilityChecker checker = new ResourceCompatibilityChecker(prevRec, resolver, currRec, resolver);
    boolean check = checker.check(compatLevel);
    _infoMap.addAll(checker.getInfoMap());
    return check;
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) DataSchemaResolver(com.linkedin.data.schema.DataSchemaResolver) MultiFormatDataSchemaResolver(com.linkedin.data.schema.resolver.MultiFormatDataSchemaResolver) DefaultDataSchemaResolver(com.linkedin.data.schema.resolver.DefaultDataSchemaResolver) FileNotFoundException(java.io.FileNotFoundException) DefaultDataSchemaResolver(com.linkedin.data.schema.resolver.DefaultDataSchemaResolver) ResourceCompatibilityChecker(com.linkedin.restli.tools.compatibility.ResourceCompatibilityChecker) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.apache.commons.cli.ParseException) Stack(java.util.Stack)

Example 8 with DefaultDataSchemaResolver

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

the class TestUtil method pdlSchemaParserFromInputStream.

public static PdlSchemaParser pdlSchemaParserFromInputStream(InputStream is) throws UnsupportedEncodingException {
    PdlSchemaParser parser = new PdlSchemaParser(new DefaultDataSchemaResolver());
    parser.parse(is);
    return parser;
}
Also used : DefaultDataSchemaResolver(com.linkedin.data.schema.resolver.DefaultDataSchemaResolver) PdlSchemaParser(com.linkedin.data.schema.grammar.PdlSchemaParser)

Example 9 with DefaultDataSchemaResolver

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

the class TestPdlSchemaParser method testFixedParserLocations.

@Test
public void testFixedParserLocations() {
    PdlSchemaParser parser = new PdlSchemaParser(new DefaultDataSchemaResolver(), true);
    parser.parse(getClass().getResourceAsStream("TestFixedForParserContextLocations.pdl"));
    List<DataSchema> topLevelSchemas = parser.topLevelDataSchemas();
    Assert.assertEquals(topLevelSchemas.size(), 1, "Expected 1 top-level schema to be parsed.");
    FixedDataSchema topSchema = (FixedDataSchema) topLevelSchemas.get(0);
    Map<Object, PdlSchemaParser.ParseLocation> locations = parser.getParseLocations();
    checkParseLocationForFixed(locations, topSchema);
}
Also used : EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DefaultDataSchemaResolver(com.linkedin.data.schema.resolver.DefaultDataSchemaResolver) Test(org.testng.annotations.Test)

Example 10 with DefaultDataSchemaResolver

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

the class SchemaTranslator method getResolver.

/**
 * Allows caller to specify a file path for schema resolution.
 */
private static DataSchemaResolver getResolver(SchemaParserFactory parserFactory, AvroToDataSchemaTranslationOptions options) {
    String resolverPath = options.getFileResolutionPaths();
    if (resolverPath != null) {
        FileDataSchemaResolver resolver = new FileDataSchemaResolver(parserFactory, resolverPath);
        resolver.setExtension(AVRO_FILE_EXTENSION);
        return resolver;
    } else {
        return new DefaultDataSchemaResolver(parserFactory);
    }
}
Also used : FileDataSchemaResolver(com.linkedin.data.schema.resolver.FileDataSchemaResolver) DefaultDataSchemaResolver(com.linkedin.data.schema.resolver.DefaultDataSchemaResolver)

Aggregations

DefaultDataSchemaResolver (com.linkedin.data.schema.resolver.DefaultDataSchemaResolver)10 DataSchema (com.linkedin.data.schema.DataSchema)6 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)6 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)5 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)5 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)5 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)5 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)5 Test (org.testng.annotations.Test)4 PdlSchemaParser (com.linkedin.data.schema.grammar.PdlSchemaParser)3 FileInputStream (java.io.FileInputStream)2 DataSchemaResolver (com.linkedin.data.schema.DataSchemaResolver)1 FileDataSchemaResolver (com.linkedin.data.schema.resolver.FileDataSchemaResolver)1 MultiFormatDataSchemaResolver (com.linkedin.data.schema.resolver.MultiFormatDataSchemaResolver)1 ResourceSchema (com.linkedin.restli.restspec.ResourceSchema)1 ResourceCompatibilityChecker (com.linkedin.restli.tools.compatibility.ResourceCompatibilityChecker)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Stack (java.util.Stack)1 ParseException (org.apache.commons.cli.ParseException)1