-
Notifications
You must be signed in to change notification settings - Fork 214
/
IndexLengthRestrictionEnforcerVisitor.java
82 lines (69 loc) · 3.04 KB
/
IndexLengthRestrictionEnforcerVisitor.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
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.thingsearch.service.persistence.write.mapping;
import java.util.Optional;
import java.util.stream.Stream;
import org.eclipse.ditto.json.JsonCollectors;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonNumber;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.thingsearch.service.persistence.write.IndexLengthRestrictionEnforcer;
/**
* Enforces limits on keys and values of a JsonObject defined in {@code IndexLengthRestrictionEnforcer}.
*/
final class IndexLengthRestrictionEnforcerVisitor implements JsonObjectVisitor<JsonValue> {
private final IndexLengthRestrictionEnforcer indexLengthRestrictionEnforcer;
private final int maxArraySize;
private IndexLengthRestrictionEnforcerVisitor(final String thingId, final int maxArraySize) {
indexLengthRestrictionEnforcer = IndexLengthRestrictionEnforcer.newInstance(thingId);
this.maxArraySize = maxArraySize;
}
static JsonObject enforce(final JsonObject thingJson, final int maxArraySize) {
final String thingId = thingJson.getValueOrThrow(Thing.JsonFields.ID);
return new IndexLengthRestrictionEnforcerVisitor(thingId, maxArraySize).eval(thingJson)
.map(JsonValue::asObject)
.orElseThrow();
}
@Override
public Optional<JsonValue> nullValue(final JsonPointer key) {
return singleton(key, JsonValue.nullLiteral());
}
@Override
public Optional<JsonValue> bool(final JsonPointer key, final boolean value) {
return singleton(key, JsonValue.of(value));
}
@Override
public Optional<JsonValue> string(final JsonPointer key, final String value) {
return singleton(key, JsonValue.of(value));
}
@Override
public Optional<JsonValue> number(final JsonPointer key, final JsonNumber value) {
return singleton(key, value);
}
@Override
public Optional<JsonValue> array(final JsonPointer key, final Stream<JsonValue> array) {
return Optional.of(Stream.of(array)
.flatMap(s -> maxArraySize < 0 ? s : s.limit(maxArraySize))
.collect(JsonCollectors.valuesToArray()));
}
@Override
public Optional<JsonValue> object(final JsonPointer key, final Stream<JsonField> jsonObject) {
return Optional.of(jsonObject.collect(JsonCollectors.fieldsToObject()));
}
private Optional<JsonValue> singleton(final JsonPointer key, final JsonValue jsonValue) {
return indexLengthRestrictionEnforcer.enforce(key, jsonValue);
}
}