Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: incorporate JsonFieldTranslator #227

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading