From 66ac578304122fc8b26275d8519bbf934b077f31 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Tue, 23 Apr 2019 17:21:37 +0200 Subject: [PATCH] [New] Added FromFlatJsonVisitor.nonEmptyParameter() --- .../model/json/FromFlatJsonVisitor.java | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/li.strolch.model/src/main/java/li/strolch/model/json/FromFlatJsonVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/json/FromFlatJsonVisitor.java index 5dc64c9df..8771bb442 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/json/FromFlatJsonVisitor.java +++ b/li.strolch.model/src/main/java/li/strolch/model/json/FromFlatJsonVisitor.java @@ -1,6 +1,7 @@ package li.strolch.model.json; import java.util.Collections; +import java.util.HashSet; import java.util.Set; import com.google.gson.JsonElement; @@ -24,7 +25,7 @@ import li.strolch.utils.dbc.DBC; *

*

* To ignore {@link Parameter Parameters} or {@link ParameterBag ParameterBags} use the {@link #ignoreParameter(String, - * * String)} and {@link #ignoreBag(String)} methods + * String)} and {@link #ignoreBag(String)} methods *

*

* {@link Parameter} can be made optional by using the {@link #optionalParameter(String, String)} method @@ -34,18 +35,25 @@ import li.strolch.utils.dbc.DBC; */ public class FromFlatJsonVisitor implements StrolchRootElementVisitor { + private Set ignoredBagTypes; private MapOfSets ignoredKeys; private MapOfSets optionalKeys; + private MapOfSets nonEmptyParameters; private JsonObject srcObject; public FromFlatJsonVisitor() { + this.ignoredBagTypes = new HashSet<>(); + this.nonEmptyParameters = new MapOfSets<>(); this.ignoredKeys = new MapOfSets<>(); this.optionalKeys = new MapOfSets<>(); + this.nonEmptyParameters = new MapOfSets<>(); } public FromFlatJsonVisitor(JsonObject srcObject) { this.srcObject = srcObject; + this.ignoredBagTypes = new HashSet<>(); + this.nonEmptyParameters = new MapOfSets<>(); this.ignoredKeys = new MapOfSets<>(); this.optionalKeys = new MapOfSets<>(); } @@ -55,6 +63,16 @@ public class FromFlatJsonVisitor implements StrolchRootElementVisitor { this.optionalKeys = new MapOfSets<>(); } + public FromFlatJsonVisitor nonEmptyParameter(String bagId, String paramId) { + this.nonEmptyParameters.addElement(bagId, paramId); + return this; + } + + public FromFlatJsonVisitor ignoreBagsOfType(String bagType) { + this.ignoredBagTypes.add(bagType); + return this; + } + public FromFlatJsonVisitor ignoreBag(String bagId) { this.ignoredKeys.addSet(bagId, Collections.emptySet()); return this; @@ -131,6 +149,10 @@ public class FromFlatJsonVisitor implements StrolchRootElementVisitor { ParameterBag parameterBag = dstElement.getParameterBag(bagId); + // see if we want to ignore bags of this type + if (this.ignoredBagTypes.contains(parameterBag.getType())) + continue; + Set parameterKeySet = parameterBag.getParameterKeySet(); for (String paramId : parameterKeySet) { @@ -140,8 +162,10 @@ public class FromFlatJsonVisitor implements StrolchRootElementVisitor { JsonElement jsonElement = this.srcObject.get(paramId); if (jsonElement == null) { + if (this.optionalKeys.containsElement(bagId, paramId)) continue; + throw new StrolchModelException( "JsonObject is missing member " + paramId + " for " + parameterBag.getLocator() + "/" + paramId); @@ -154,8 +178,16 @@ public class FromFlatJsonVisitor implements StrolchRootElementVisitor { } Parameter parameter = parameterBag.getParameter(paramId); + + String asString = jsonElement.getAsString(); + if (asString.isEmpty() && this.nonEmptyParameters.containsElement(bagId, paramId)) { + throw new StrolchModelException( + "JsonElement " + paramId + " is required to be a non empty value for " + parameter + .getLocator()); + } + try { - parameter.setValueFromString(jsonElement.getAsString()); + parameter.setValueFromString(asString); } catch (Exception e) { throw new IllegalStateException("Failed to set parameter " + parameter.getLocator(), e); }