Search in sources :

Example 6 with DataSchema

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);
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) Field(com.linkedin.data.schema.RecordDataSchema.Field) TypeAssignmentContext(com.linkedin.data.grammar.PdlParser.TypeAssignmentContext) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 7 with DataSchema

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;
}
Also used : UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema)

Example 8 with DataSchema

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;
}
Also used : UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) PdlParser(com.linkedin.data.grammar.PdlParser) TypeDeclarationContext(com.linkedin.data.grammar.PdlParser.TypeDeclarationContext) NamedTypeDeclarationContext(com.linkedin.data.grammar.PdlParser.NamedTypeDeclarationContext) AnonymousTypeDeclarationContext(com.linkedin.data.grammar.PdlParser.AnonymousTypeDeclarationContext)

Example 9 with DataSchema

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;
}
Also used : UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) Name(com.linkedin.data.schema.Name)

Example 10 with DataSchema

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;
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataMap(com.linkedin.data.DataMap)

Aggregations

DataSchema (com.linkedin.data.schema.DataSchema)131 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)82 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)53 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)48 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)44 DataMap (com.linkedin.data.DataMap)43 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)41 MapDataSchema (com.linkedin.data.schema.MapDataSchema)40 Test (org.testng.annotations.Test)37 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)36 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)24 ByteString (com.linkedin.data.ByteString)15 TestUtil.dataMapFromString (com.linkedin.data.TestUtil.dataMapFromString)15 TestUtil.dataSchemaFromString (com.linkedin.data.TestUtil.dataSchemaFromString)14 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)14 ArrayList (java.util.ArrayList)12 DataList (com.linkedin.data.DataList)11 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)9 SchemaParser (com.linkedin.data.schema.SchemaParser)9 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)9