Search in sources :

Example 1 with TypeAssignmentContext

use of com.linkedin.data.grammar.PdlParser.TypeAssignmentContext 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 2 with TypeAssignmentContext

use of com.linkedin.data.grammar.PdlParser.TypeAssignmentContext in project rest.li by linkedin.

the class PdlSchemaParser method parseUnion.

private UnionDataSchema parseUnion(UnionDeclarationContext union, boolean withinTypref) throws ParseException {
    UnionDataSchema schema = new UnionDataSchema();
    List<UnionMemberDeclarationContext> members = union.typeParams.members;
    List<DataSchema> types = new ArrayList<>(members.size());
    Set<DataSchema> typesDeclaredInline = new HashSet<>();
    for (UnionMemberDeclarationContext memberDecl : members) {
        TypeAssignmentContext memberType = memberDecl.member;
        DataSchema dataSchema = toDataSchema(memberType);
        if (dataSchema != null) {
            types.add(dataSchema);
            if (isDeclaredInline(memberDecl.member)) {
                typesDeclaredInline.add(dataSchema);
            }
        }
    }
    schema.setTypes(types, errorMessageBuilder());
    schema.setTypesDeclaredInline(typesDeclaredInline);
    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) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) TypeAssignmentContext(com.linkedin.data.grammar.PdlParser.TypeAssignmentContext) ArrayList(java.util.ArrayList) UnionMemberDeclarationContext(com.linkedin.data.grammar.PdlParser.UnionMemberDeclarationContext) HashSet(java.util.HashSet)

Example 3 with TypeAssignmentContext

use of com.linkedin.data.grammar.PdlParser.TypeAssignmentContext in project rest.li by linkedin.

the class PdlSchemaParser method parseMap.

private MapDataSchema parseMap(MapDeclarationContext map) throws ParseException {
    TypeAssignmentContext keyType = map.typeParams.key;
    TypeAssignmentContext valueType = map.typeParams.value;
    MapDataSchema schema = new MapDataSchema(toDataSchema(valueType));
    Map<String, Object> propsToAdd = new HashMap<String, Object>();
    if (keyType.typeReference() != null) {
        String typeName = keyType.typeReference().value;
        if (!typeName.equals("string")) {
            startErrorMessage(map).append("Unsupported map key type: ").append(typeName).append(". 'string' is the only currently supported map key type.\n");
        // TODO(jbetz):
        // Support typed map keys once https://github.com/linkedin/rest.li/pull/61 is accepted.
        //String qualifiedKeyName = computeFullName(typeName);
        //propsToAdd.put("keys", qualifiedKeyName);
        }
    } else if (keyType.typeDeclaration() != null) {
        DataSchema keySchema = parseType(keyType.typeDeclaration());
        String json = SchemaToJsonEncoder.schemaToJson(keySchema, JsonBuilder.Pretty.COMPACT);
        startErrorMessage(map).append("Unsupported map key type declaration: ").append(json).append(". 'string' is the only currently supported map key type.\n");
    // TODO(jbetz):
    // Support typed map keys once https://github.com/linkedin/rest.li/pull/61 is accepted.
    //DataMap dataMap = codec.stringToMap(json);
    //propsToAdd.put("keys", dataMap);
    }
    schema.setProperties(propsToAdd);
    schema.setValuesDeclaredInline(isDeclaredInline(valueType));
    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) HashMap(java.util.HashMap) TypeAssignmentContext(com.linkedin.data.grammar.PdlParser.TypeAssignmentContext) MapDataSchema(com.linkedin.data.schema.MapDataSchema)

Aggregations

TypeAssignmentContext (com.linkedin.data.grammar.PdlParser.TypeAssignmentContext)3 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)3 DataSchema (com.linkedin.data.schema.DataSchema)3 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)3 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)3 MapDataSchema (com.linkedin.data.schema.MapDataSchema)3 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)3 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)3 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)3 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 UnionMemberDeclarationContext (com.linkedin.data.grammar.PdlParser.UnionMemberDeclarationContext)1 Field (com.linkedin.data.schema.RecordDataSchema.Field)1 HashMap (java.util.HashMap)1