-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Parameter.java
172 lines (136 loc) · 5.22 KB
/
Parameter.java
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package org.graylog.plugins.enterprise.search;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.auto.value.AutoValue;
import com.google.common.collect.Maps;
import javax.annotation.Nullable;
import java.util.Map;
import java.util.Objects;
/**
* Parameters describe variable inputs to queries.
* <p>
* They consist of a declaration and a binding. Parameters without a binding are called "free" or "unbound" parameters.
* In order to execute a query all of its non-optional parameters must have a binding associated with them, i.e. be "bound".
* <p>
* The caller is expected to provide a {@link Parameter} object when binding previously declared parameters.
* In that case the declaration elements do not need to be repeated, only its {@link Parameter#name() name} property.
*/
@AutoValue
@JsonDeserialize(builder = Parameter.Builder.class)
public abstract class Parameter {
@JsonProperty
public abstract String name();
@Nullable
@JsonProperty
public abstract String title();
@Nullable
@JsonProperty
public abstract String description();
@JsonProperty("data_type")
public abstract String dataType();
@Nullable
@JsonProperty("default_value")
public abstract Object defaultValue();
@JsonProperty
public abstract boolean optional();
@Nullable
@JsonProperty
public abstract Binding binding();
public static Builder builder() {
return new AutoValue_Parameter.Builder().optional(false);
}
public static Parameter any(String name) {
return builder().name(name).dataType("any").build();
}
public abstract Builder toBuilder();
public Parameter applyExecutionState(ObjectMapper objectMapper, JsonNode state) {
final JsonNode bindingState = state.path(name());
if (bindingState.isMissingNode()) {
return this;
}
final Binding binding = objectMapper.convertValue(bindingState, Binding.class);
return toBuilder().binding(binding).build();
}
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = Binding.TYPE_FIELD,
visible = true,
defaultImpl = Binding.Fallback.class)
public interface Binding {
String TYPE_FIELD = "type";
@JsonProperty(TYPE_FIELD)
String type();
class Fallback implements Binding {
@JsonProperty
private String type;
private Map<String, Object> props = Maps.newHashMap();
@Override
public String type() {
return type;
}
@JsonAnySetter
public void setProperties(String key, Object value) {
props.put(key, value);
}
@JsonAnyGetter
public Map<String, Object> getProperties() {
return props;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Binding.Fallback fallback = (Binding.Fallback) o;
return Objects.equals(type, fallback.type) &&
Objects.equals(props, fallback.props);
}
@Override
public int hashCode() {
return Objects.hash(type, props);
}
}
}
@AutoValue.Builder
public abstract static class Builder {
@JsonProperty
public abstract Builder name(String name);
@JsonProperty
public abstract Builder title(String title);
@JsonProperty
public abstract Builder description(String description);
@JsonProperty
public abstract Builder dataType(String dataType);
@JsonProperty("default_value")
public abstract Builder defaultValue(Object defaultValue);
@JsonProperty
public abstract Builder optional(boolean optional);
@JsonProperty
public abstract Builder binding(Binding binding);
public abstract Parameter build();
// to fill the default values
@JsonCreator
public static Builder create() {
return Parameter.builder().optional(false);
}
}
public interface BindingHandler<B extends Binding> {
// this method only exists because the compiler cannot treat `Binding` and `B extends Binding` as the same types
// see SearchTypeHandler
@SuppressWarnings("unchecked")
default Object resolve(Binding binding, Object defaultValue, Map<String, QueryResult> results) {
return doResolve((B) binding, defaultValue, results);
}
Object doResolve(B binding, Object defaultValue, Map<String, QueryResult> results);
}
}