Search in sources :

Example 1 with JsonValueContext

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

FieldDeclarationContext (com.linkedin.data.grammar.PdlParser.FieldDeclarationContext)1 FieldDefaultContext (com.linkedin.data.grammar.PdlParser.FieldDefaultContext)1 JsonValueContext (com.linkedin.data.grammar.PdlParser.JsonValueContext)1 PropDeclarationContext (com.linkedin.data.grammar.PdlParser.PropDeclarationContext)1 Field (com.linkedin.data.schema.RecordDataSchema.Field)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1