|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Metaform Systems, Inc. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Apache License, Version 2.0 which is available at |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: Apache-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Metaform Systems, Inc. - initial API and implementation |
| 12 | + * |
| 13 | + */ |
| 14 | + |
| 15 | +package org.eclipse.edc.connector.controlplane.transform.edc.cel.to; |
| 16 | + |
| 17 | +import jakarta.json.JsonObject; |
| 18 | +import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer; |
| 19 | +import org.eclipse.edc.policy.cel.model.CelExpressionTestRequest; |
| 20 | +import org.eclipse.edc.transform.spi.TransformerContext; |
| 21 | +import org.jetbrains.annotations.NotNull; |
| 22 | +import org.jetbrains.annotations.Nullable; |
| 23 | + |
| 24 | +import static org.eclipse.edc.policy.cel.model.CelExpressionTestRequest.CEL_EXPRESSION_TEST_REQUEST_EXPRESSION_IRI; |
| 25 | +import static org.eclipse.edc.policy.cel.model.CelExpressionTestRequest.CEL_EXPRESSION_TEST_REQUEST_LEFT_OPERAND_IRI; |
| 26 | +import static org.eclipse.edc.policy.cel.model.CelExpressionTestRequest.CEL_EXPRESSION_TEST_REQUEST_OPERATOR_IRI; |
| 27 | +import static org.eclipse.edc.policy.cel.model.CelExpressionTestRequest.CEL_EXPRESSION_TEST_REQUEST_PARAMS_IRI; |
| 28 | +import static org.eclipse.edc.policy.cel.model.CelExpressionTestRequest.CEL_EXPRESSION_TEST_REQUEST_RIGHT_OPERAND_IRI; |
| 29 | + |
| 30 | +public class JsonObjectToCelExpressionTestRequestTransformer extends AbstractJsonLdTransformer<JsonObject, CelExpressionTestRequest> { |
| 31 | + |
| 32 | + public JsonObjectToCelExpressionTestRequestTransformer() { |
| 33 | + super(JsonObject.class, CelExpressionTestRequest.class); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public @Nullable CelExpressionTestRequest transform(@NotNull JsonObject object, @NotNull TransformerContext context) { |
| 38 | + |
| 39 | + |
| 40 | + var expression = transformString(object.get(CEL_EXPRESSION_TEST_REQUEST_EXPRESSION_IRI), context); |
| 41 | + var operandLeft = transformString(object.get(CEL_EXPRESSION_TEST_REQUEST_LEFT_OPERAND_IRI), context); |
| 42 | + var operandRight = transformGenericProperty(object.get(CEL_EXPRESSION_TEST_REQUEST_RIGHT_OPERAND_IRI), context); |
| 43 | + var operator = transformString(object.get(CEL_EXPRESSION_TEST_REQUEST_OPERATOR_IRI), context); |
| 44 | + |
| 45 | + var builder = CelExpressionTestRequest.Builder.newInstance() |
| 46 | + .leftOperand(operandLeft) |
| 47 | + .rightOperand(operandRight) |
| 48 | + .operator(operator) |
| 49 | + .expression(expression); |
| 50 | + |
| 51 | + var params = object.get(CEL_EXPRESSION_TEST_REQUEST_PARAMS_IRI); |
| 52 | + if (params != null) { |
| 53 | + var jsonValue = nodeJsonValue(params); |
| 54 | + if (jsonValue instanceof JsonObject json) { |
| 55 | + visitProperties(json, (key, value) -> builder.param(key, transformGenericProperty(value, context))); |
| 56 | + } else { |
| 57 | + context.reportProblem("Expected properties to be a JsonObject"); |
| 58 | + return null; |
| 59 | + } |
| 60 | + } |
| 61 | + return builder.build(); |
| 62 | + } |
| 63 | +} |
0 commit comments