Search in sources :

Example 6 with PropDeclarationContext

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

the class PdlSchemaParser method parseFields.

private List<Field> parseFields(RecordDataSchema recordSchema, FieldSelectionContext fieldGroup) throws ParseException {
    List<Field> results = new ArrayList<>();
    for (FieldDeclarationContext field : fieldGroup.fields) {
        if (field != null) {
            if (field.type == null) {
                throw new IllegalStateException("type is missing for field: " + field.getText());
            }
            Field result = new Field(toDataSchema(field.type));
            recordLocation(result, field);
            Map<String, Object> properties = new HashMap<>();
            result.setName(field.name, errorMessageBuilder());
            result.setOptional(field.isOptional);
            FieldDefaultContext fieldDefault = field.fieldDefault();
            if (fieldDefault != null) {
                JsonValueContext defaultValue = fieldDefault.jsonValue();
                if (defaultValue != null) {
                    result.setDefault(parseJsonValue(defaultValue));
                }
            }
            List<String> aliases = new ArrayList<>();
            RecordDataSchema.Field.Order sortOrder = null;
            for (PropDeclarationContext prop : field.props) {
                if (equalsSingleSegmentProperty(prop, DataSchemaConstants.ALIASES_KEY)) {
                    aliases = parseAliases(prop);
                } else if (equalsSingleSegmentProperty(prop, DataSchemaConstants.ORDER_KEY)) {
                    Object value = parsePropValue(prop);
                    if (!(value instanceof String)) {
                        startErrorMessage(prop).append("'order' must be string, but found ").append(prop.getText()).append(NEWLINE);
                    } else {
                        String order = (String) value;
                        try {
                            sortOrder = RecordDataSchema.Field.Order.valueOf(order.toUpperCase());
                        } catch (IllegalArgumentException exc) {
                            startErrorMessage(order).append("\"").append(order).append("\" is an invalid sort order.\n");
                        }
                    }
                } else {
                    addPropertiesAtPath(properties, prop);
                }
            }
            if (field.doc != null) {
                result.setDoc(field.doc.value);
            }
            if (aliases.size() > 0) {
                result.setAliases(aliases, errorMessageBuilder());
            }
            if (sortOrder != null) {
                result.setOrder(sortOrder);
            }
            result.setProperties(properties);
            result.setRecord(recordSchema);
            result.setDeclaredInline(isDeclaredInline(field.type));
            results.add(result);
        } else {
            startErrorMessage(field).append("Unrecognized field element parse node: ").append(field.getText()).append(NEWLINE);
        }
    }
    return results;
}
Also used : IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FieldDefaultContext(com.linkedin.data.grammar.PdlParser.FieldDefaultContext) FieldDeclarationContext(com.linkedin.data.grammar.PdlParser.FieldDeclarationContext) Field(com.linkedin.data.schema.RecordDataSchema.Field) PropDeclarationContext(com.linkedin.data.grammar.PdlParser.PropDeclarationContext) JsonValueContext(com.linkedin.data.grammar.PdlParser.JsonValueContext)

Aggregations

PropDeclarationContext (com.linkedin.data.grammar.PdlParser.PropDeclarationContext)6 HashMap (java.util.HashMap)6 IdentityHashMap (java.util.IdentityHashMap)5 ArrayList (java.util.ArrayList)3 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)2 Name (com.linkedin.data.schema.Name)2 DataMap (com.linkedin.data.DataMap)1 EnumSymbolDeclarationContext (com.linkedin.data.grammar.PdlParser.EnumSymbolDeclarationContext)1 FieldDeclarationContext (com.linkedin.data.grammar.PdlParser.FieldDeclarationContext)1 FieldDefaultContext (com.linkedin.data.grammar.PdlParser.FieldDefaultContext)1 JsonValueContext (com.linkedin.data.grammar.PdlParser.JsonValueContext)1 TypeAssignmentContext (com.linkedin.data.grammar.PdlParser.TypeAssignmentContext)1 UnionMemberAliasContext (com.linkedin.data.grammar.PdlParser.UnionMemberAliasContext)1 UnionMemberDeclarationContext (com.linkedin.data.grammar.PdlParser.UnionMemberDeclarationContext)1 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)1 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)1 DataSchema (com.linkedin.data.schema.DataSchema)1 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)1 MapDataSchema (com.linkedin.data.schema.MapDataSchema)1 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)1