forked from eclipse-edc/Connector
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dsp): add transformer for JsonObjectToDataAddress (eclipse-edc#2811
- Loading branch information
1 parent
61a7cd2
commit 1ec7eb5
Showing
13 changed files
with
273 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...c/main/java/org/eclipse/edc/jsonld/transformer/to/JsonObjectToDataAddressTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.jsonld.transformer.to; | ||
|
||
import jakarta.json.JsonArray; | ||
import jakarta.json.JsonObject; | ||
import jakarta.json.JsonValue; | ||
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer; | ||
import org.eclipse.edc.spi.types.domain.DataAddress; | ||
import org.eclipse.edc.transform.spi.TransformerContext; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import static org.eclipse.edc.spi.CoreConstants.EDC_NAMESPACE; | ||
|
||
|
||
/** | ||
* Converts from an {@link DataAddress} as a {@link JsonObject} in JSON-LD expanded form to an {@link DataAddress}. | ||
*/ | ||
public class JsonObjectToDataAddressTransformer extends AbstractJsonLdTransformer<JsonObject, DataAddress> { | ||
|
||
//TODO: move into a module-level constants file | ||
public static final String PROPERTIES_KEY = EDC_NAMESPACE + "properties"; | ||
|
||
public JsonObjectToDataAddressTransformer() { | ||
super(JsonObject.class, DataAddress.class); | ||
} | ||
|
||
@Override | ||
public @Nullable DataAddress transform(@NotNull JsonObject jsonObject, @NotNull TransformerContext context) { | ||
var builder = DataAddress.Builder.newInstance(); | ||
visitProperties(jsonObject, (s, v) -> transformProperties(s, v, builder, context)); | ||
return builder.build(); | ||
} | ||
|
||
private void transformProperties(String key, JsonValue jsonValue, DataAddress.Builder builder, TransformerContext context) { | ||
if ((PROPERTIES_KEY).equals(key) && jsonValue instanceof JsonArray) { | ||
var props = jsonValue.asJsonArray().getJsonObject(0); | ||
visitProperties(props, (k, val) -> transformProperties(k, val, builder, context)); | ||
} else { | ||
var object = transformGenericProperty(jsonValue, context); | ||
if (object instanceof String) { | ||
builder.property(key, object.toString()); | ||
} else { | ||
context.reportProblem("DataAddress#properties can only contain Strings, but an attempt was made to put in a " + object.getClass()); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.