use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class PdlSchemaParser method parseIncludes.
private FieldsAndIncludes parseIncludes(PdlParser.FieldIncludesContext includeSet) throws ParseException {
List<NamedDataSchema> includes = new ArrayList<>();
Set<NamedDataSchema> includesDeclaredInline = new HashSet<>();
List<Field> fields = new ArrayList<>();
if (includeSet != null) {
List<TypeAssignmentContext> includeTypes = includeSet.typeAssignment();
for (TypeAssignmentContext includeRef : includeTypes) {
DataSchema includedSchema = toDataSchema(includeRef);
if (includedSchema != null) {
DataSchema dereferencedIncludedSchema = includedSchema.getDereferencedDataSchema();
if (includedSchema instanceof NamedDataSchema && dereferencedIncludedSchema instanceof RecordDataSchema) {
NamedDataSchema includedNamedSchema = (NamedDataSchema) includedSchema;
RecordDataSchema dereferencedIncludedRecordSchema = (RecordDataSchema) dereferencedIncludedSchema;
fields.addAll(dereferencedIncludedRecordSchema.getFields());
includes.add(includedNamedSchema);
if (isDeclaredInline(includeRef)) {
includesDeclaredInline.add(includedNamedSchema);
}
} else {
startErrorMessage(includeRef).append("Include is not a record type or a typeref to a record type: ").append(includeRef).append(NEWLINE);
}
} else {
startErrorMessage(includeRef).append("Unable to resolve included schema: ").append(includeRef).append(NEWLINE);
}
}
}
return new FieldsAndIncludes(fields, includes, includesDeclaredInline);
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class PdlSchemaParser method computeFullName.
// Extended fullname computation to handle imports
public String computeFullName(String name) {
String fullname;
DataSchema schema = DataSchemaUtil.typeStringToPrimitiveDataSchema(name);
if (schema != null) {
fullname = name;
} else if (Name.isFullName(name) || getCurrentNamespace().isEmpty()) {
// already a fullname
fullname = name;
} else if (currentImports.containsKey(name)) {
// imported names are higher precedence than names in current namespace
fullname = currentImports.get(name).getFullName();
} else {
// assumed to be in current namespace
fullname = getCurrentNamespace() + "." + name;
}
return fullname;
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class PdlSchemaParser method parse.
/**
* Parse list of Data objects.
*
* The {{DataSchema}'s parsed are in {{#topLevelDataSchemas}.
* Parse errors are in {{#errorMessageBuilder} and indicated
* by {{#hasError()}.
*
* @param document provides the source code in AST form
*/
private DataSchema parse(DocumentContext document) throws ParseException {
PdlParser.NamespaceDeclarationContext namespaceDecl = document.namespaceDeclaration();
if (namespaceDecl != null) {
setCurrentNamespace(namespaceDecl.qualifiedIdentifier().value);
} else {
setCurrentNamespace("");
}
if (document.packageDeclaration() != null) {
setCurrentPackage(document.packageDeclaration().qualifiedIdentifier().value);
} else {
setCurrentPackage(null);
}
setCurrentImports(document.importDeclarations());
TypeDeclarationContext typeDeclaration = document.typeDeclaration();
DataSchema schema;
if (typeDeclaration.namedTypeDeclaration() != null) {
NamedDataSchema namedSchema = parseNamedType(typeDeclaration.namedTypeDeclaration());
if (!namedSchema.getNamespace().equals(getCurrentNamespace())) {
throw new ParseException(typeDeclaration, "Top level type declaration may not be qualified with a namespace different than the file namespace: " + typeDeclaration.getText());
}
schema = namedSchema;
} else if (typeDeclaration.anonymousTypeDeclaration() != null) {
schema = parseAnonymousType(typeDeclaration.anonymousTypeDeclaration());
} else {
throw new ParseException(typeDeclaration, "Unrecognized type declaration: " + typeDeclaration.getText());
}
addTopLevelSchema(schema);
return schema;
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class PdlSchemaParser method parseTyperef.
private TyperefDataSchema parseTyperef(NamedTypeDeclarationContext context, TyperefDeclarationContext typeref) throws ParseException {
Name name = toName(typeref.name);
TyperefDataSchema schema = new TyperefDataSchema(name);
bindNameToSchema(name, schema);
DataSchema refSchema = toDataSchema(typeref.ref);
schema.setReferencedType(refSchema);
schema.setRefDeclaredInline(isDeclaredInline(typeref.ref));
setProperties(context, schema);
return schema;
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class DataElementUtil method element.
/**
* Get the {@link DataElement} by following the specified path starting from
* the provided {@link DataElement}.
*
* @param element provides the {@link DataElement} that is the starting point.
* @param path provides the path components through an {@link Iterable}.
* @return the {@link DataElement} if the path can be followed, else return null.
* @throws IllegalArgumentException if the provided path's syntax is incorrect, Data object does not match provided
* {@link DataSchema}.
*/
public static DataElement element(DataElement element, Iterable<Object> path) throws IllegalArgumentException {
DataElement currentElement = element;
for (Object component : path) {
Object name;
if (currentElement.getValue().getClass() == DataMap.class && component.getClass() != String.class) {
name = component.toString();
} else if (currentElement.getValue().getClass() == DataList.class && component.getClass() != Integer.class) {
try {
name = Integer.parseInt(component.toString());
} catch (NumberFormatException e) {
return null;
}
} else {
name = component;
}
Object childValue = currentElement.getChild(name);
if (childValue == null) {
return null;
}
DataSchema childSchema = null;
DataSchema schema = currentElement.getSchema();
if (schema != null) {
schema = schema.getDereferencedDataSchema();
switch(schema.getType()) {
case ARRAY:
childSchema = ((ArrayDataSchema) schema).getItems();
break;
case MAP:
childSchema = ((MapDataSchema) schema).getValues();
break;
case UNION:
childSchema = ((UnionDataSchema) schema).getType((String) name);
break;
case RECORD:
RecordDataSchema.Field field = ((RecordDataSchema) schema).getField((String) name);
if (field != null) {
childSchema = field.getType();
}
break;
default:
throw new IllegalArgumentException(currentElement.pathAsString() + " has schema of type " + schema.getType() + " which cannot have child, but has child with name \"" + name + "\"");
}
}
currentElement = new SimpleDataElement(childValue, name, childSchema, currentElement);
}
return currentElement;
}
Aggregations