use of com.linkedin.data.schema.PegasusSchemaParser in project rest.li by linkedin.
the class AbstractGenerator method parseSchema.
/**
* Parse a source file to obtain the data schemas contained within.
*
* @param schemaSourceFile provides the source file.
* @return the data schemas within the source file.
* @throws IOException if there is a file access error.
*/
protected List<DataSchema> parseSchema(final File schemaSourceFile) throws IOException {
PegasusSchemaParser parser = AbstractSchemaParser.parserForFile(schemaSourceFile, getSchemaResolver());
FileInputStream schemaStream = new SchemaFileInputStream(schemaSourceFile);
try {
parser.setLocation(new FileDataSchemaLocation(schemaSourceFile));
parser.parse(schemaStream);
if (parser.hasError()) {
return Collections.emptyList();
}
return parser.topLevelDataSchemas();
} finally {
schemaStream.close();
if (parser.hasError()) {
getMessage().append(schemaSourceFile.getPath() + ",");
getMessage().append(parser.errorMessage());
}
}
}
use of com.linkedin.data.schema.PegasusSchemaParser 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");
addBadLocation(location);
} else {
DataSchema found = _nameToDataSchema.get(name);
if (found != null && found instanceof NamedDataSchema) {
schema = (NamedDataSchema) found;
}
}
return schema;
}
use of com.linkedin.data.schema.PegasusSchemaParser in project rest.li by linkedin.
the class DataTemplateUtil method parseSchema.
/**
* Parse data schema encoded in any format to obtain a {@link DataSchema}.
*
* @param schemaText the encoded data schema.
* @param schemaFormatType the format in which the schema is encoded.
* @param schemaResolver resolver for resolving referenced schemas.
* @return the {@link DataSchema} parsed from the encoded data schema.
* @throws IllegalArgumentException if the encoded data schema is invalid or there is more than one top-level schema.
*/
public static DataSchema parseSchema(String schemaText, DataSchemaResolver schemaResolver, SchemaFormatType schemaFormatType) throws IllegalArgumentException {
final PegasusSchemaParser parser = schemaFormatType.getSchemaParserFactory().create(schemaResolver);
parser.parse(schemaText);
if (parser.hasError()) {
if (debug) {
out.println(parser.errorMessage());
}
throw new IllegalArgumentException(parser.errorMessage());
}
if (parser.topLevelDataSchemas().size() != 1) {
throw new IllegalArgumentException("More than one top level schema");
}
return parser.topLevelDataSchemas().get(0);
}
use of com.linkedin.data.schema.PegasusSchemaParser in project rest.li by linkedin.
the class TestConversions method testConvertDataMapToDataSchema.
@Test
public void testConvertDataMapToDataSchema() throws IOException {
for (String good : goodInputs) {
NamedDataSchema dataSchema = (NamedDataSchema) TestUtil.dataSchemaFromString(good);
DataMap mapFromString = TestUtil.dataMapFromString(good);
PegasusSchemaParser parser = new SchemaParser();
DataSchema schemaFromMap = Conversions.dataMapToDataSchema(mapFromString, parser);
assertEquals(schemaFromMap, dataSchema);
}
for (String bad : badInputs) {
DataMap mapFromString = TestUtil.dataMapFromString(bad);
PegasusSchemaParser parser = new SchemaParser();
DataSchema schemaFromMap = Conversions.dataMapToDataSchema(mapFromString, parser);
assertNull(schemaFromMap);
assertTrue(parser.hasError());
}
}
use of com.linkedin.data.schema.PegasusSchemaParser in project rest.li by linkedin.
the class TestDataSchemaResolver method lookup.
public void lookup(DataSchemaResolver resolver, String[][] lookups, char separator, boolean debug, SchemaFormatType extension) {
PegasusSchemaParser parser = extension.equals(PDSC) ? new SchemaParser(resolver) : new PdlSchemaParser(resolver);
for (String[] entry : lookups) {
String name = entry[0];
String expectFound = entry[1];
String expected = entry[2];
DataSchema schema = parser.lookupName(name);
if (debug) {
out.println("----" + name + "-----");
}
String errorMessage = parser.errorMessage();
if (debug && errorMessage.isEmpty() == false) {
out.println(errorMessage);
}
if (expectFound == ERROR) {
assertTrue(parser.hasError());
assertTrue(expected == null || errorMessage.contains(expected), "Expected: " + expected + "\n Actual: " + errorMessage);
} else if (expectFound == FOUND) {
assertTrue(schema != null);
String schemaText = schema.toString();
if (debug) {
out.println(schemaText);
}
assertFalse(parser.hasError());
assertTrue(schema instanceof NamedDataSchema);
NamedDataSchema namedSchema = (NamedDataSchema) schema;
assertEquals(namedSchema.getFullName(), name);
assertTrue(schemaText.contains(expected));
assertTrue(resolver.bindings().containsKey(name));
assertSame(resolver.bindings().get(name), namedSchema);
String location = entry[3];
DataSchemaLocation namedSchemalocation = resolver.nameToDataSchemaLocations().get(name);
String locationNorm;
if (namedSchemalocation.toString().contains(".jar")) {
locationNorm = location.replace(separator, '/');
} else {
locationNorm = location.replace('/', separator);
}
assertNotNull(namedSchemalocation);
assertEquals(namedSchemalocation.toString().indexOf(locationNorm), namedSchemalocation.toString().length() - locationNorm.length());
assertTrue(resolver.locationResolved(namedSchemalocation));
} else if (expectFound == NOT_FOUND) {
assertTrue(schema == null);
assertFalse(parser.hasError());
assertTrue(expected == null || errorMessage.contains(expected));
assertFalse(resolver.bindings().containsKey(name));
} else {
assertTrue(false);
}
}
}
Aggregations