Skip to content

Commit

Permalink
fix: incorporate JsonFieldTranslator
Browse files Browse the repository at this point in the history
  • Loading branch information
paullatzelsperger committed Jan 17, 2024
1 parent 2a42b39 commit 0ad5479
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,50 @@
package org.eclipse.edc.identityhub.store.sql.credentials.schema.postgres;

import org.eclipse.edc.spi.types.PathItem;
import org.eclipse.edc.sql.translation.JsonFieldMapping;
import org.eclipse.edc.sql.translation.JsonFieldTranslator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Maps the canonical format of a {@link org.eclipse.edc.identitytrust.model.VerifiableCredential} onto its JSON representation
* and generates query statements for Postgres.
*/
public class CredentialJsonMapping extends JsonFieldMapping {
public class CredentialJsonMapping extends JsonFieldTranslator {

private final Map<String, String> replacements = new HashMap<>();

public CredentialJsonMapping(String columnName) {
super(columnName);
add("credentialSubject", PostgresDialectStatements.CREDENTIAL_SUBJECT_ALIAS);
replacements.put("credentialSubject", PostgresDialectStatements.CREDENTIAL_SUBJECT_ALIAS);
}

@Override
public String getStatement(String canonicalPropertyName, Class<?> type) {
return super.getStatement(canonicalPropertyName, type);
}
public String getLeftOperand(List<PathItem> path, Class<?> type) {
var stmt = super.getLeftOperand(path, type);

@Override
public String getStatement(List<PathItem> path, Class<?> type) {
var replacement = fieldMap.get(path.get(0).toString());

if (path.stream().allMatch(p -> p.toString().equals("types"))) {
return "(%s)::jsonb".formatted(stmt).replace("->>", "->");
}

if (path.size() == 1) {
return stmt;
}
// the WHERE clause can't handle set-returning functions such as "json_array_elements". thus, we must use an alias
// in the FROM clause, and re-use the same alias in the WHERE clause, for example:
// SELECT * FROM credential_resource r, json_array_elements(r.verifiable_credential -> 'credentialSubject') subj WHERE subj ->> 'test-key' = 'test-val2';
// SELECT * FROM credential_resource r, json_array_elements(r.verifiable_credential -> 'credentialSubject') subj WHERE subj ->> 'test-key' = 'test-val2';
var firstPathItem = path.get(0).toString();
var replacement = replacements.get(firstPathItem);
if (replacement != null) {
List<PathItem> newPath = new ArrayList<>();
var newPath = new ArrayList<PathItem>();
path.stream().skip(1).forEach(newPath::add);
var newMapping = new JsonFieldMapping(replacement.toString());
return newMapping.getStatement(newPath, type);
var newMapping = new JsonFieldTranslator(replacement);
return newMapping.getLeftOperand(newPath, type);
}
var stmt = super.getStatement(path, type);

if (path.stream().allMatch(p -> p.toString().equals("types"))) {
stmt = "(%s)::jsonb".formatted(stmt).replace("->>", "->");
}
return stmt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package org.eclipse.edc.identityhub.did.store.sql.schema.postgres;

import org.eclipse.edc.identityhub.did.store.sql.DidResourceStatements;
import org.eclipse.edc.sql.translation.JsonFieldMapping;
import org.eclipse.edc.sql.translation.JsonFieldTranslator;
import org.eclipse.edc.sql.translation.TranslationMapping;

/**
Expand All @@ -30,8 +30,8 @@ public class DidDocumentMapping extends TranslationMapping {

public DidDocumentMapping(DidResourceStatements statements) {
add(FIELD_ID, statements.getIdColumn());
add(FIELD_SERVICE, new JsonFieldMapping(FIELD_SERVICE));
add(FIELD_VERIFICATION_METHOD, new JsonFieldMapping(FIELD_VERIFICATION_METHOD));
add(FIELD_SERVICE, new JsonFieldTranslator(FIELD_SERVICE));
add(FIELD_VERIFICATION_METHOD, new JsonFieldTranslator(FIELD_VERIFICATION_METHOD));
add(FIELD_AUTHENTICATION, FIELD_AUTHENTICATION);
}
}

0 comments on commit 0ad5479

Please sign in to comment.