Use JsonNodeUtils to manage array nodes

This commit is contained in:
Xavi Aracil 2022-11-25 08:28:02 +01:00
parent 543a2c5bdb
commit 40b34cce13
2 changed files with 15 additions and 14 deletions

View File

@ -7,6 +7,7 @@ import java.util.stream.Collectors;
import org.oneedtech.inspect.schema.Catalog; import org.oneedtech.inspect.schema.Catalog;
import org.oneedtech.inspect.schema.SchemaKey; import org.oneedtech.inspect.schema.SchemaKey;
import org.oneedtech.inspect.util.resource.Resource; import org.oneedtech.inspect.util.resource.Resource;
import org.oneedtech.inspect.vc.util.JsonNodeUtil;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects;
@ -25,7 +26,7 @@ public class Assertion extends Credential {
super(ID, resource, data, jwt, schemas); super(ID, resource, data, jwt, schemas);
JsonNode typeNode = jsonData.get("type"); JsonNode typeNode = jsonData.get("type");
this.assertionType = Assertion.Type.valueOf(typeNode); this.assertionType = Assertion.Type.valueOf(typeNode);
} }
@Override @Override
@ -69,13 +70,15 @@ public class Assertion extends Credential {
public static Assertion.Type valueOf (JsonNode typeNode) { public static Assertion.Type valueOf (JsonNode typeNode) {
if(typeNode != null) { if(typeNode != null) {
String value = typeNode.asText(); List<String> values = JsonNodeUtil.asStringList(typeNode);
if(value.equals("Assertion")) { for (String value : values) {
return Assertion; if(value.equals("Assertion")) {
return Assertion;
}
} }
} }
return Unknown; return Unknown;
} }
} }
public static final String ID = Assertion.class.getCanonicalName(); public static final String ID = Assertion.class.getCanonicalName();

View File

@ -5,7 +5,6 @@ import static org.oneedtech.inspect.vc.VerifiableCredential.Type.ClrCredential;
import static org.oneedtech.inspect.vc.VerifiableCredential.Type.EndorsementCredential; import static org.oneedtech.inspect.vc.VerifiableCredential.Type.EndorsementCredential;
import static org.oneedtech.inspect.vc.VerifiableCredential.Type.VerifiablePresentation; import static org.oneedtech.inspect.vc.VerifiableCredential.Type.VerifiablePresentation;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -14,9 +13,9 @@ import java.util.stream.Collectors;
import org.oneedtech.inspect.schema.Catalog; import org.oneedtech.inspect.schema.Catalog;
import org.oneedtech.inspect.schema.SchemaKey; import org.oneedtech.inspect.schema.SchemaKey;
import org.oneedtech.inspect.util.resource.Resource; import org.oneedtech.inspect.util.resource.Resource;
import org.oneedtech.inspect.vc.util.JsonNodeUtil;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
@ -31,7 +30,7 @@ public class VerifiableCredential extends Credential {
protected VerifiableCredential(Resource resource, JsonNode data, String jwt, Map<String, SchemaKey> schemas) { protected VerifiableCredential(Resource resource, JsonNode data, String jwt, Map<String, SchemaKey> schemas) {
super(ID, resource, data, jwt, schemas); super(ID, resource, data, jwt, schemas);
ArrayNode typeNode = (ArrayNode)jsonData.get("type"); JsonNode typeNode = jsonData.get("type");
this.credentialType = VerifiableCredential.Type.valueOf(typeNode); this.credentialType = VerifiableCredential.Type.valueOf(typeNode);
} }
@ -82,11 +81,10 @@ public class VerifiableCredential extends Credential {
VerifiableCredential, //this is an underspecifier in our context VerifiableCredential, //this is an underspecifier in our context
Unknown; Unknown;
public static VerifiableCredential.Type valueOf (ArrayNode typeArray) { public static VerifiableCredential.Type valueOf (JsonNode typeNode) {
if(typeArray != null) { if(typeNode != null) {
Iterator<JsonNode> iter = typeArray.iterator(); List<String> values = JsonNodeUtil.asStringList(typeNode);
while(iter.hasNext()) { for (String value : values) {
String value = iter.next().asText();
if(value.equals("AchievementCredential") || value.equals("OpenBadgeCredential")) { if(value.equals("AchievementCredential") || value.equals("OpenBadgeCredential")) {
return AchievementCredential; return AchievementCredential;
} else if(value.equals("ClrCredential")) { } else if(value.equals("ClrCredential")) {
@ -99,7 +97,7 @@ public class VerifiableCredential extends Credential {
} }
} }
return Unknown; return Unknown;
} }
} }
public enum ProofType { public enum ProofType {