This repository has been archived by the owner on Apr 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parser: new VaultPreprocessing component for performing compatibility…
… adjustments and other last-minute changes over uncontrolled inputs, right before compilation happens
- Loading branch information
Showing
9 changed files
with
178 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/java/com/lumiomedical/vault/parser/preprocessor/VaultPreprocessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.lumiomedical.vault.parser.preprocessor; | ||
|
||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.lumiomedical.vault.exception.VaultParserException; | ||
|
||
/** | ||
* The preprocessors are expected to be used to perform compatibility adjustments or last-minute changes over uncontrolled inputs. | ||
* Preprocessing should happen before structure validation and before compilation. | ||
* | ||
* @author Pierre Lecerf (plecerf@lumiomedical.com) | ||
* Created on 2021/01/25 | ||
*/ | ||
public interface VaultPreprocessor | ||
{ | ||
/** | ||
* Can alter a given configuration node, or leave it as is, before it goes through compilation. | ||
* | ||
* @param node A configuration node | ||
* @return an altered configuration node | ||
* @throws VaultParserException | ||
*/ | ||
ObjectNode preprocess(ObjectNode node) throws VaultParserException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/test/java/com/lumiomedical/vault/parser/PreprocessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.lumiomedical.vault.parser; | ||
|
||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.lumiomedical.vault.container.definition.Definitions; | ||
import com.lumiomedical.vault.exception.VaultParserException; | ||
import com.noleme.json.Json; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static com.noleme.commons.function.RethrowConsumer.rethrower; | ||
|
||
/** | ||
* @author Pierre Lecerf (plecerf@lumiomedical.com) | ||
* Created on 2020/11/24 | ||
*/ | ||
public class PreprocessorTest | ||
{ | ||
@Test | ||
void test() throws VaultParserException | ||
{ | ||
var parser = new VaultCompositeParser(); | ||
|
||
Assertions.assertThrows(VaultParserException.class, () -> { | ||
parser.extract("com/lumiomedical/vault/parser/simple.old_style.json"); | ||
}); | ||
|
||
parser.registerPreprocessor(PreprocessorTest::preprocessor); | ||
|
||
Assertions.assertDoesNotThrow(() -> parser.extract("com/lumiomedical/vault/parser/simple.old_style.json")); | ||
|
||
Definitions definitions = parser.extract("com/lumiomedical/vault/parser/simple.old_style.json"); | ||
|
||
ParserTest.makeAssertions(definitions); | ||
} | ||
|
||
/** | ||
* | ||
* @param node | ||
* @return | ||
* @throws VaultParserException | ||
*/ | ||
private static ObjectNode preprocessor(ObjectNode node) throws VaultParserException | ||
{ | ||
if (!node.has("services") || !node.get("services").isArray()) | ||
return node; | ||
|
||
var objServices = Json.newObject(); | ||
|
||
node.get("services").forEach(rethrower(serviceNode -> { | ||
|
||
if (!serviceNode.has("identifier")) | ||
throw new VaultParserException("A service node declared in an old-style service array has no identifier field."); | ||
|
||
objServices.set( | ||
serviceNode.get("identifier").asText(), | ||
serviceNode | ||
); | ||
})); | ||
|
||
node.set("services", objServices); | ||
|
||
return node; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/test/resources/com/lumiomedical/vault/parser/provider.integer.old_style.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"variables": { | ||
"provider.integer.value": 1234 | ||
}, | ||
"services": [ | ||
{ | ||
"identifier": "provider.integer", | ||
"class": "com.lumiomedical.vault.service.IntegerProvider", | ||
"constructor": [ | ||
"##provider.integer.value##" | ||
] | ||
} | ||
] | ||
} |
49 changes: 49 additions & 0 deletions
49
src/test/resources/com/lumiomedical/vault/parser/simple.old_style.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{ | ||
"imports": [ | ||
"com/lumiomedical/vault/parser/provider.string.json", | ||
"com/lumiomedical/vault/parser/provider.boolean.json", | ||
"com/lumiomedical/vault/parser/provider.integer.old_style.json" | ||
], | ||
"variables": { | ||
"provider.integer.value": 2345, | ||
"provider.double.value": 12.34, | ||
"provider.string.base_value": "SomeString", | ||
"provider.string.value": "##provider.string.base_value##" | ||
}, | ||
"services": [ | ||
{ | ||
"identifier": "alias.string", | ||
"alias": "provider.string" | ||
}, | ||
{ | ||
"identifier": "provider.boolean.base", | ||
"class": "com.lumiomedical.vault.service.BooleanProvider", | ||
"constructor": [ false ] | ||
}, | ||
{ | ||
"identifier": "provider.double", | ||
"class": "com.lumiomedical.vault.service.DoubleProvider", | ||
"constructor": [ | ||
"##provider.double.value##" | ||
], | ||
"invocations": [ | ||
["provide"] | ||
] | ||
}, | ||
{ | ||
"identifier": "provider.string", | ||
"class": "com.lumiomedical.vault.service.StringProvider", | ||
"method": "build", | ||
"arguments": [ | ||
"##provider.string.value##" | ||
] | ||
}, | ||
{ | ||
"identifier": "provider.boolean", | ||
"class": "com.lumiomedical.vault.service.ServiceProvider", | ||
"arguments": [ | ||
"@provider.boolean.base" | ||
] | ||
} | ||
] | ||
} |