Search in sources :

Example 26 with NamedDataSchema

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

the class TestSchemaFilter method dataSchemaFromString.

private static NamedDataSchema dataSchemaFromString(String s, boolean isAvroUnionMode) throws IOException {
    SchemaParser parser = new SchemaParser();
    parser.getValidationOptions().setAvroUnionMode(isAvroUnionMode);
    parser.parse(TestUtil.inputStreamFromString(s));
    if (parser.hasError()) {
        TestUtil.out.println("ERROR: " + parser.errorMessage());
        return null;
    }
    return (NamedDataSchema) parser.topLevelDataSchemas().get(parser.topLevelDataSchemas().size() - 1);
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) SchemaParser(com.linkedin.data.schema.SchemaParser)

Example 27 with NamedDataSchema

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

the class ResourceModelEncoder method buildDataSchemaType.

/*package*/
static String buildDataSchemaType(DataSchema schema) {
    if (schema instanceof PrimitiveDataSchema || schema instanceof NamedDataSchema) {
        return schema.getUnionMemberKey();
    }
    JsonBuilder builder = null;
    try {
        builder = new JsonBuilder(JsonBuilder.Pretty.SPACES);
        final SchemaToJsonEncoder encoder = new SchemaToJsonEncoder(builder, AbstractSchemaEncoder.TypeReferenceFormat.MINIMIZE);
        encoder.encode(schema);
        return builder.result();
    } catch (IOException e) {
        throw new RestLiInternalException("could not encode schema for '" + schema.toString() + "'", e);
    } finally {
        if (builder != null) {
            builder.closeQuietly();
        }
    }
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) JsonBuilder(com.linkedin.data.schema.JsonBuilder) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) SchemaToJsonEncoder(com.linkedin.data.schema.SchemaToJsonEncoder) IOException(java.io.IOException)

Example 28 with NamedDataSchema

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

the class AbstractGenerator method validateSchemaWithFilepath.

/**
   * Checks that the schema name and namespace match the file name and path.  These must match for
   * FileDataSchemaResolver to find a schema pdscs by fully qualified name.
   *
   */
private void validateSchemaWithFilepath(File schemaSourceFile, DataSchema schema) {
    if (schemaSourceFile != null && schemaSourceFile.isFile() && schema instanceof NamedDataSchema) {
        NamedDataSchema namedDataSchema = (NamedDataSchema) schema;
        String namespace = namedDataSchema.getNamespace();
        if (!FileUtil.removeFileExtension(schemaSourceFile.getName()).equalsIgnoreCase(namedDataSchema.getName())) {
            throw new IllegalArgumentException(namedDataSchema.getFullName() + " has name that does not match filename '" + schemaSourceFile.getAbsolutePath() + "'");
        }
        String directory = schemaSourceFile.getParentFile().getAbsolutePath();
        if (!directory.endsWith(namespace.replace('.', File.separatorChar))) {
            throw new IllegalArgumentException(namedDataSchema.getFullName() + " has namespace that does not match " + "file path '" + schemaSourceFile.getAbsolutePath() + "'");
        }
    }
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema)

Example 29 with NamedDataSchema

use of com.linkedin.data.schema.NamedDataSchema 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");
        _badLocations.add(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 30 with NamedDataSchema

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

the class AbstractDataSchemaResolver method bindNameToSchema.

@Override
public void bindNameToSchema(Name name, NamedDataSchema schema, DataSchemaLocation location) {
    String fullName = name.getFullName();
    NamedDataSchema replaced = _nameToDataSchema.put(fullName, schema);
    if (replaced != null)
        throw new IllegalStateException(fullName + " cannot be refined from " + replaced + " to " + schema);
    _nameToDataSchemaLocations.put(fullName, location);
    _resolvedLocations.add(location);
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema)

Aggregations

NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)46 DataSchema (com.linkedin.data.schema.DataSchema)25 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)16 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)12 MapDataSchema (com.linkedin.data.schema.MapDataSchema)11 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)11 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)11 DataMap (com.linkedin.data.DataMap)8 IOException (java.io.IOException)8 DataSchemaLocation (com.linkedin.data.schema.DataSchemaLocation)7 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)7 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)6 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)6 SchemaParser (com.linkedin.data.schema.SchemaParser)6 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)5 File (java.io.File)5 Test (org.testng.annotations.Test)5 CustomInfoSpec (com.linkedin.pegasus.generator.spec.CustomInfoSpec)4 RestLiInternalException (com.linkedin.restli.internal.server.RestLiInternalException)4 FileInputStream (java.io.FileInputStream)4