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();
}
}
}
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() + "'");
}
}
}
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;
}
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);
}
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;
}
Aggregations