Search in sources :

Example 21 with NamedDataSchema

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

the class ResourceModelEncoder method buildDataSchemaType.

private static String buildDataSchemaType(final Class<?> type, final DataSchema dataSchema) {
    final DataSchema schemaToEncode;
    if (dataSchema instanceof TyperefDataSchema) {
        return ((TyperefDataSchema) dataSchema).getFullName();
    } else if (dataSchema instanceof PrimitiveDataSchema || dataSchema instanceof NamedDataSchema) {
        return dataSchema.getUnionMemberKey();
    } else if (dataSchema instanceof UnionDataSchema && HasTyperefInfo.class.isAssignableFrom(type)) {
        final TyperefInfo unionRef = DataTemplateUtil.getTyperefInfo(type.asSubclass(DataTemplate.class));
        schemaToEncode = unionRef.getSchema();
    } else {
        schemaToEncode = dataSchema;
    }
    JsonBuilder builder = null;
    try {
        builder = new JsonBuilder(JsonBuilder.Pretty.SPACES);
        final SchemaToJsonEncoder encoder = new SchemaToJsonEncoder(builder, AbstractSchemaEncoder.TypeReferenceFormat.MINIMIZE);
        encoder.encode(schemaToEncode);
        return builder.result();
    } catch (IOException e) {
        throw new RestLiInternalException("could not encode schema for '" + type.getName() + "'", e);
    } finally {
        if (builder != null) {
            builder.closeQuietly();
        }
    }
}
Also used : UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) JsonBuilder(com.linkedin.data.schema.JsonBuilder) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) HasTyperefInfo(com.linkedin.data.template.HasTyperefInfo) DataTemplate(com.linkedin.data.template.DataTemplate) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) SchemaToJsonEncoder(com.linkedin.data.schema.SchemaToJsonEncoder) IOException(java.io.IOException) TyperefInfo(com.linkedin.data.template.TyperefInfo) HasTyperefInfo(com.linkedin.data.template.HasTyperefInfo)

Example 22 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 23 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 24 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)

Example 25 with NamedDataSchema

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

the class AbstractDataSchemaResolver method locateDataSchema.

/**
   * Locate a {@link NamedDataSchema} with the specified name.
   *
   * @param name of schema to locate.
   * @param errorMessageBuilder to append error messages to.
   * @return the NamedDataSchema if it can be located, else return null.
   */
protected NamedDataSchema locateDataSchema(String name, StringBuilder errorMessageBuilder) {
    NamedDataSchema schema = null;
    Iterator<DataSchemaLocation> locations = possibleLocations(name);
    while (locations.hasNext()) {
        DataSchemaLocation location = locations.next();
        if (location == null || isBadLocation(location)) {
            continue;
        }
        InputStream inputStream = null;
        try {
            inputStream = locationToInputStream(location, errorMessageBuilder);
            if (inputStream == null) {
                addBadLocation(location);
            } else {
                schema = parse(inputStream, location, name, errorMessageBuilder);
                if (schema != null) {
                    break;
                }
            }
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException exc) {
                }
            }
        }
    }
    return schema;
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) FilterInputStream(java.io.FilterInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) DataSchemaLocation(com.linkedin.data.schema.DataSchemaLocation)

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