aboutsummaryrefslogtreecommitdiffstats
path: root/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/SimpleTestAdapter.java
blob: f1fef2f0489f552e7a85010bf992afc6a10e3789 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.indexinglanguage;

import com.yahoo.document.DataType;
import com.yahoo.document.DocumentType;
import com.yahoo.document.Field;
import com.yahoo.document.FieldPath;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.vespa.indexinglanguage.expressions.Expression;
import com.yahoo.vespa.indexinglanguage.expressions.FieldValueAdapter;
import com.yahoo.vespa.indexinglanguage.expressions.VerificationException;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Simon Thoresen Hult
 */
public class SimpleTestAdapter implements FieldValueAdapter {

    final Map<String, DataType> types = new HashMap<>();
    final Map<String, FieldValue> values = new HashMap<>();

    public SimpleTestAdapter(Field... fields) {
        for (Field field : fields) {
            types.put(field.getName(), field.getDataType());
        }
    }

    public SimpleTestAdapter createField(Field field) {
        types.put(field.getName(), field.getDataType());
        return this;
    }

    @Override
    public DataType getInputType(Expression exp, String fieldName) {
        return types.get(fieldName);
    }

    @Override
    public FieldValue getInputValue(String fieldName) {
        return values.get(fieldName);
    }

    @Override
    public FieldValue getInputValue(FieldPath fieldPath) {
        return values.get(fieldPath.toString());
    }

    @Override
    public void tryOutputType(Expression exp, String fieldName, DataType valueType) {
        DataType fieldType = types.get(fieldName);
        if (fieldType == null) {
            throw new VerificationException(exp, "Field '" + fieldName + "' not found.");
        }
        if (!fieldType.isAssignableFrom(valueType)) {
            throw new VerificationException(exp, "Can not assign " + valueType.getName() + " to field '" +
                                                 fieldName + "' which is " + fieldType.getName() + ".");
        }
    }

    public SimpleTestAdapter setValue(String fieldName, FieldValue fieldValue) {
        values.put(fieldName, fieldValue);
        return this;
    }

    @Override
    public SimpleTestAdapter setOutputValue(Expression exp, String fieldName, FieldValue fieldValue) {
        values.put(fieldName, fieldValue);
        return this;
    }

}