Skip to content

Commit 0aaa967

Browse files
authored
Update docs on CollectorContext (#913)
1 parent 0c14a7a commit 0aaa967

File tree

1 file changed

+46
-62
lines changed

1 file changed

+46
-62
lines changed

doc/collector-context.md

+46-62
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,88 @@
11
### CollectorContext
22

3-
4-
There could be usecases 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.
54

65
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.
76

8-
CollectorContext and Collector classes are designed to work with this use case.
7+
The `CollectorContext` and `Collector` classes are designed to work with this use case.
98

109
#### How to use CollectorContext
1110

12-
Objects of CollectorContext live on ThreadLocal which is unique for every thread. This allows users to add objects to context at many points in the framework like Formats, Validators (Effectively CollectorContext can be used at any touch point in the validateAndCollect method thread call).
13-
14-
CollectorContext instance can be obtained by calling the getInstance static method on CollectorContext. This method gives an instance from the ThreadLocal for the current thread.
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.
1512

16-
Collectors are added to CollectorContext. Collectors allow to collect the objects. A Collector is added to CollectorContext with a name and corresponding Collector instance.
13+
Collectors are added to `CollectorContext`. Collectors allow to collect the objects. A `Collector` is added to `CollectorContext` with a name and corresponding `Collector` instance.
1714

1815
```java
19-
CollectorContext collectorContext = CollectorContext.getInstance();
16+
CollectorContext collectorContext = executionContext.getCollectorContext();
2017
collectorContext.add(SAMPLE_COLLECTOR_NAME, new Collector<List<String>>() {
21-
@Override
22-
public List<String> collect() {
23-
List<String> references = new ArrayList<String>();
24-
references.add(getDatasourceMap().get(node.textValue()));
25-
return references;
26-
}
18+
@Override
19+
public List<String> collect() {
20+
List<String> references = new ArrayList<String>();
21+
references.add(getDatasourceMap().get(node.textValue()));
22+
return references;
23+
}
2724
});
2825
```
2926

3027
However there might be use cases where we want to add a simple Object like String, Integer, etc, into the Context. This can be done the same way a collector is added to the context.
3128

3229
```java
33-
CollectorContext collectorContext = CollectorContext.getInstance();
34-
collectorContext.add(SAMPLE_COLLECTOR,"sample-string")
35-
36-
```
37-
38-
To retrieve the `CollectorContext` after validation or walking, automatic reseting of collector contexts has to be disabled:
39-
40-
```java
41-
SchemaValidatorsConfig configuration = new SchemaValidatorsConfig();
42-
configuration.setResetCollectorContext(false);
43-
JsonSchema jsonSchema = factory.getSchema(uri, configuration);
44-
30+
CollectorContext collectorContext = executionContext.getCollectorContext();
31+
collectorContext.add(SAMPLE_COLLECTOR, "sample-string")
4532
```
4633

4734
To use the `CollectorContext` while validating, the `validateAndCollect` method has to be invoked on the `JsonSchema` class.
48-
This method returns a `ValidationResult` that contains the errors encountered during validation and a `CollectorContext` instance.
35+
This method returns a `ValidationResult` that contains the errors encountered during validation and a `ExecutionContext` instance that contains the `CollectorContext`.
4936
Objects constructed by collectors or directly added to `CollectorContext` can be retrieved from `CollectorContext` by using the name they were added with.
5037

38+
To collect across multiple validation runs, the `CollectorContext` needs to be explicitly reused by passing the `ExecutionContext` as a parameter to the validation.
5139

5240
```java
53-
List<String> contextValue;
54-
try {
55-
ValidationResult validationResult = jsonSchema.validateAndCollect(jsonNode);
56-
CollectorContext context = validationResult.getCollectorContext();
57-
contextValue = (List<String>)context.get(SAMPLE_COLLECTOR);
58-
} finally {
59-
// Remove all data from collector context.
60-
// Otherwise, data is kept between validation runs which is usually unintended
61-
// and a potential memory leak.
62-
if (CollectorContext.getInstance() != null) CollectorContext.getInstance().reset();
63-
}
64-
// do something with contextValue
41+
ValidationResult validationResult = jsonSchema.validateAndCollect(jsonNode);
42+
ExecutionContext executionContext = validationResult.getExecutionContext();
43+
CollectorContext collectorContext = executionContext.getCollectorContext();
44+
List<String> contextValue = (List<String>) collectorContext.get(SAMPLE_COLLECTOR);
45+
46+
// Do something with contextValue
47+
...
48+
49+
// To collect more information for subsequent runs reuse the context
50+
validationResult = jsonSchema.validateAndCollect(executionContext, jsonNode);
6551
```
6652

67-
There might be usecases where a collector needs to collect the data at multiple touch points. For example one usecase 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.
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.
6854

6955
```java
70-
class CustomCollector implements Collector<List<String>> {
56+
class CustomCollector implements Collector<List<String>> {
7157

72-
List<String> returnList = new ArrayList<String>();
58+
List<String> returnList = new ArrayList<>();
7359

74-
private Map<String, String> referenceMap = null;
60+
private Map<String, String> referenceMap = null;
7561

76-
public CustomCollector() {
77-
referenceMap = getDatasourceMap();
78-
}
62+
public CustomCollector() {
63+
referenceMap = getDatasourceMap();
64+
}
7965

80-
@Override
81-
public List<String> collect() {
82-
return returnList;
83-
}
66+
@Override
67+
public List<String> collect() {
68+
return returnList;
69+
}
8470

85-
@Override
86-
public void combine(Object object) {
87-
returnList.add(referenceMap.get((String) object));
88-
}
71+
@Override
72+
public void combine(Object object) {
73+
returnList.add(referenceMap.get((String) object));
74+
}
8975
}
9076

91-
CollectorContext collectorContext = CollectorContext.getInstance();
77+
CollectorContext collectorContext = executionContext.getCollectorContext();
9278
if (collectorContext.get(SAMPLE_COLLECTOR) == null) {
93-
collectorContext.add(SAMPLE_COLLECTOR, new CustomCollector());
79+
collectorContext.add(SAMPLE_COLLECTOR, new CustomCollector());
9480
}
9581
collectorContext.combineWithCollector(SAMPLE_COLLECTOR, node.textValue());
9682

9783
```
9884

99-
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.
85+
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`.
10086

10187
```java
10288
// Returns Collector before validation is done.
@@ -110,14 +96,12 @@ List<String> data = collectorContext.get(SAMPLE_COLLECTOR);
11096
If you are using simple objects and if the data needs to be collected from multiple touch points, logic is straightforward as shown.
11197

11298
```java
113-
114-
CollectorContext collectorContext = CollectorContext.getInstance();
99+
CollectorContext collectorContext = executionContext.getCollectorContext();
115100
// If collector name is not added to context add one.
116101
if (collectorContext.get(SAMPLE_COLLECTOR) == null) {
117-
collectorContext.add(SAMPLE_COLLECTOR, new ArrayList<String>());
102+
collectorContext.add(SAMPLE_COLLECTOR, new ArrayList<String>());
118103
}
119104
// In this case we are adding a list to CollectorContext.
120105
List<String> returnList = (List<String>) collectorContext.get(SAMPLE_COLLECTOR);
121106

122-
```
123-
107+
```

0 commit comments

Comments
 (0)