Search in sources :

Example 1 with PdlSchemaParser

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

the class TestUtil method pdlSchemaParserFromString.

public static PdlSchemaParser pdlSchemaParserFromString(String s) throws UnsupportedEncodingException {
    PdlSchemaParser parser = new PdlSchemaParser(new DefaultDataSchemaResolver());
    parser.parse(inputStreamFromString(s));
    return parser;
}
Also used : DefaultDataSchemaResolver(com.linkedin.data.schema.resolver.DefaultDataSchemaResolver) PdlSchemaParser(com.linkedin.data.schema.grammar.PdlSchemaParser)

Example 2 with PdlSchemaParser

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

the class PdlEncoderTest method parseSchema.

private DataSchema parseSchema(String text, String name) throws IOException {
    DataSchemaResolver resolver = MultiFormatDataSchemaResolver.withBuiltinFormats(pegasusSrcDir.getAbsolutePath());
    AbstractSchemaParser parser = new PdlSchemaParser(resolver);
    parser.parse(text);
    return extractSchema(parser, name);
}
Also used : MultiFormatDataSchemaResolver(com.linkedin.data.schema.resolver.MultiFormatDataSchemaResolver) DataSchemaResolver(com.linkedin.data.schema.DataSchemaResolver) AbstractSchemaParser(com.linkedin.data.schema.AbstractSchemaParser) PdlSchemaParser(com.linkedin.data.schema.grammar.PdlSchemaParser)

Example 3 with PdlSchemaParser

use of com.linkedin.data.schema.grammar.PdlSchemaParser 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 4 with PdlSchemaParser

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

the class ExtensionSchemaValidationCmdLineApp method parseAndValidateExtensionSchemas.

static void parseAndValidateExtensionSchemas(String resolverPath, File inputDir) throws IOException, InvalidExtensionSchemaException {
    // Parse each extension schema and validate it
    Iterator<File> iterator = FileUtils.iterateFiles(inputDir, new String[] { PDL }, true);
    DataSchemaResolver resolver = MultiFormatDataSchemaResolver.withBuiltinFormats(resolverPath);
    while (iterator.hasNext()) {
        File inputFile = iterator.next();
        PdlSchemaParser parser = new PdlSchemaParser(resolver);
        parser.parse(new FileInputStream(inputFile));
        if (parser.hasError()) {
            throw new InvalidExtensionSchemaException(parser.errorMessage());
        }
        List<DataSchema> topLevelDataSchemas = parser.topLevelDataSchemas();
        if (topLevelDataSchemas == null || topLevelDataSchemas.isEmpty() || topLevelDataSchemas.size() > 1) {
            throw new InvalidExtensionSchemaException("Could not parse extension schema : " + inputFile.getAbsolutePath());
        }
        DataSchema topLevelDataSchema = topLevelDataSchemas.get(0);
        if (!(topLevelDataSchema instanceof NamedDataSchema)) {
            throw new InvalidExtensionSchemaException("Invalid extension schema : " + inputFile.getAbsolutePath() + ", the schema is not a named schema.");
        }
        if (!((NamedDataSchema) topLevelDataSchema).getName().endsWith(EXTENSIONS_SUFFIX)) {
            throw new InvalidExtensionSchemaException("Invalid extension schema name: '" + ((NamedDataSchema) topLevelDataSchema).getName() + "'. The name of the extension schema must be <baseSchemaName> + 'Extensions'");
        }
        List<NamedDataSchema> includes = ((RecordDataSchema) topLevelDataSchema).getInclude();
        if (includes.size() != 1) {
            throw new InvalidExtensionSchemaException("The extension schema: '" + ((NamedDataSchema) topLevelDataSchema).getName() + "' should include and only include the base schema");
        }
        NamedDataSchema includeSchema = includes.get(0);
        if (!((NamedDataSchema) topLevelDataSchema).getName().startsWith(includeSchema.getName())) {
            throw new InvalidExtensionSchemaException("Invalid extension schema name: '" + ((NamedDataSchema) topLevelDataSchema).getName() + "'. The name of the extension schema must be baseSchemaName: '" + includeSchema.getName() + "' + 'Extensions");
        }
        List<RecordDataSchema.Field> extensionSchemaFields = ((RecordDataSchema) topLevelDataSchema).getFields().stream().filter(f -> !((RecordDataSchema) topLevelDataSchema).isFieldFromIncludes(f)).collect(Collectors.toList());
        checkExtensionSchemaFields(extensionSchemaFields);
    }
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) Options(org.apache.commons.cli.Options) LoggerFactory(org.slf4j.LoggerFactory) DataSchemaResolver(com.linkedin.data.schema.DataSchemaResolver) HelpFormatter(org.apache.commons.cli.HelpFormatter) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) GnuParser(org.apache.commons.cli.GnuParser) DataMap(com.linkedin.data.DataMap) RequiredMode(com.linkedin.data.schema.validation.RequiredMode) CoercionMode(com.linkedin.data.schema.validation.CoercionMode) ExtensionSchemaAnnotation(com.linkedin.restli.common.ExtensionSchemaAnnotation) Map(java.util.Map) OptionBuilder(org.apache.commons.cli.OptionBuilder) CommandLine(org.apache.commons.cli.CommandLine) EXTENSION_ANNOTATION_NAMESPACE(com.linkedin.data.schema.annotation.ExtensionSchemaAnnotationHandler.EXTENSION_ANNOTATION_NAMESPACE) ValidateDataAgainstSchema(com.linkedin.data.schema.validation.ValidateDataAgainstSchema) MultiFormatDataSchemaResolver(com.linkedin.data.schema.resolver.MultiFormatDataSchemaResolver) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) CommandLineParser(org.apache.commons.cli.CommandLineParser) DataList(com.linkedin.data.DataList) UnrecognizedFieldMode(com.linkedin.data.schema.validation.UnrecognizedFieldMode) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) FileInputStream(java.io.FileInputStream) PdlSchemaParser(com.linkedin.data.schema.grammar.PdlSchemaParser) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) RestLiToolsUtils(com.linkedin.restli.internal.tools.RestLiToolsUtils) ParseException(org.apache.commons.cli.ParseException) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) FileInputStream(java.io.FileInputStream) DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataSchemaResolver(com.linkedin.data.schema.DataSchemaResolver) MultiFormatDataSchemaResolver(com.linkedin.data.schema.resolver.MultiFormatDataSchemaResolver) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) File(java.io.File) PdlSchemaParser(com.linkedin.data.schema.grammar.PdlSchemaParser)

Example 5 with PdlSchemaParser

use of com.linkedin.data.schema.grammar.PdlSchemaParser 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)

Aggregations

PdlSchemaParser (com.linkedin.data.schema.grammar.PdlSchemaParser)7 DataSchema (com.linkedin.data.schema.DataSchema)3 DataSchemaResolver (com.linkedin.data.schema.DataSchemaResolver)3 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)3 DefaultDataSchemaResolver (com.linkedin.data.schema.resolver.DefaultDataSchemaResolver)3 MultiFormatDataSchemaResolver (com.linkedin.data.schema.resolver.MultiFormatDataSchemaResolver)3 FileInputStream (java.io.FileInputStream)3 AbstractSchemaParser (com.linkedin.data.schema.AbstractSchemaParser)2 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)2 DataList (com.linkedin.data.DataList)1 DataMap (com.linkedin.data.DataMap)1 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)1 DataSchemaLocation (com.linkedin.data.schema.DataSchemaLocation)1 PegasusSchemaParser (com.linkedin.data.schema.PegasusSchemaParser)1 SchemaParser (com.linkedin.data.schema.SchemaParser)1 EXTENSION_ANNOTATION_NAMESPACE (com.linkedin.data.schema.annotation.ExtensionSchemaAnnotationHandler.EXTENSION_ANNOTATION_NAMESPACE)1 CoercionMode (com.linkedin.data.schema.validation.CoercionMode)1 RequiredMode (com.linkedin.data.schema.validation.RequiredMode)1 UnrecognizedFieldMode (com.linkedin.data.schema.validation.UnrecognizedFieldMode)1 ValidateDataAgainstSchema (com.linkedin.data.schema.validation.ValidateDataAgainstSchema)1