You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/collector-context.md
+56-29
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
### CollectorContext
2
2
3
-
There could be use cases where we want collect the information while we are validating the data. A simple example could be fetching some value from a database or from a microservice based on the data (which could be a text or a JSON object. It should be noted that this should be a simple operation or validation might take more time to complete.) in a given JSON node and the schema keyword we are using.
3
+
There could be use cases where we want collect the information while we are validating the data. A simple example could be fetching some value from a database or from a microservice based on the data (which could be a text or a JSON object. It should be noted that this should be a simple operation or validation might take more time to complete.) in a given JSON node and the schema keyword we are using.
4
4
5
5
The fetched data can be stored somewhere so that it can be used later after the validation is done. Since the current validation logic already parses the data and schema, both validation and collecting the required information can be done in one go.
6
6
@@ -10,6 +10,12 @@ The `CollectorContext` and `Collector` classes are designed to work with this us
10
10
11
11
The `CollectorContext` is stored as a variable on the `ExecutionContext` that is used during the validation. This allows users to add objects to context at many points in the framework like Formats and Validators where the `ExecutionContext` is available as a parameter.
12
12
13
+
By default the `CollectorContext` created by the library contains maps backed by `HashMap`. If the `CollectorContext` needs to be shared by multiple threads then a `ConcurrentHashMap` needs to be used.
Collectors are added to `CollectorContext`. Collectors allow to collect the objects. A `Collector` is added to `CollectorContext` with a name and corresponding `Collector` instance.
14
20
15
21
```java
@@ -28,29 +34,44 @@ However there might be use cases where we want to add a simple Object like Strin
To use the `CollectorContext` while validating, the `validateAndCollect` method has to be invoked on the `JsonSchema` class.
35
-
This method returns a `ValidationResult` that contains the errors encountered during validation and a `ExecutionContext` instance that contains the `CollectorContext`.
36
-
Objects constructed by collectors or directly added to `CollectorContext` can be retrieved from `CollectorContext` by using the name they were added with.
37
-
38
-
To collect across multiple validation runs, the `CollectorContext` needs to be explicitly reused by passing the `ExecutionContext` as a parameter to the validation.
40
+
Implementations that need to modify values the `CollectorContext` should do so in a thread-safe manner.
To use the `CollectorContext` while validating, the `CollectorContext` should be instantiated outside and set for every validation execution.
45
50
46
-
// Do something with contextValue
47
-
...
51
+
At the end of all the runs the `CollectorContext.loadCollectors()` method can be called if needed for the `Collector` implementations to aggregate values.
48
52
49
-
// To collect more information for subsequent runs reuse the context
// This is called for Collector implementations to aggregate data
69
+
collectorContext.loadCollectors();
70
+
AtomicInteger result = (AtomicInteger) collectorContext.get("collect");
71
+
assertEquals(50, result.get());
51
72
```
52
73
53
-
There might be use cases where a collector needs to collect the data at multiple touch points. For example one use case might be collecting data in a validator and a formatter. If you are using a `Collector` rather than a `Object`, the combine method of the `Collector` allows to define how we want to combine the data into existing `Collector`. `CollectorContext``combineWithCollector` method calls the combine method on the `Collector`. User just needs to call the `CollectorContext``combineWithCollector` method every time some data needs to merged into existing `Collector`. The `collect` method on the `Collector` is called by the framework at the end of validation to return the data that was collected.
74
+
There might be use cases where a collector needs to collect the data at multiple touch points. For example one use case might be collecting data in a validator and a formatter. If you are using a `Collector` rather than a `Object`, the combine method of the `Collector` allows to define how we want to combine the data into existing `Collector`. `CollectorContext``combineWithCollector` method calls the combine method on the `Collector`. User just needs to call the `CollectorContext``combineWithCollector` method every time some data needs to merged into existing `Collector`. The `collect` method on the `Collector` is called by explicitly calling `CollectorContext.loadCollectors()`at the end of processing.
One important thing to note when using Collectors is if we call get method on `CollectorContext` before the validation is complete, we would get back a `Collector` instance that was added to `CollectorContext`.
@@ -96,12 +126,9 @@ List<String> data = collectorContext.get(SAMPLE_COLLECTOR);
96
126
If you are using simple objects and if the data needs to be collected from multiple touch points, logic is straightforward as shown.
0 commit comments