Skip to content

Commit

Permalink
[Fix apache#2177] Partial rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Jan 21, 2025
1 parent 0cd7c8f commit 5d68dcb
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,25 @@ public static TypeDefinitionRegistry loadSchemaDefinitionFile(String fileName) {
}
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public static Map mergeMap(Map source, Map target) {
@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> mergeMap(Map<K, V> source, Map<K, V> target) {
if (source == null) {
return target;
} else if (target == null) {
return source;
} else {
Map result = new HashMap(target);
source.forEach((key, value) -> result.merge(key, value, (targetValue, srcValue) -> {
if (srcValue instanceof Map && targetValue instanceof Map) {
return mergeMap((Map) srcValue, (Map) targetValue);
} else {
return srcValue;
Map<K, V> result = new HashMap<>(target);
source.forEach((key, value) -> {
if (value != null) {
result.merge(key, value, (targetValue, srcValue) -> {
if (srcValue instanceof Map && targetValue instanceof Map) {
return (V) mergeMap((Map<K, V>) srcValue, (Map<K, V>) targetValue);
} else {
return srcValue;
}
});
}
}));
});
return result;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,24 @@
*/
package org.kie.kogito.index.service;

import java.util.AbstractMap;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.kie.kogito.event.process.NodeDefinition;
import org.kie.kogito.event.process.ProcessDefinitionDataEvent;
import org.kie.kogito.event.process.ProcessDefinitionEventBody;
import org.kie.kogito.index.CommonUtils;
import org.kie.kogito.index.json.JsonUtils;
import org.kie.kogito.index.model.Node;
import org.kie.kogito.index.model.ProcessDefinition;

import com.fasterxml.jackson.core.JsonProcessingException;

import jakarta.enterprise.context.ApplicationScoped;

import static java.util.stream.Collectors.toList;
Expand Down Expand Up @@ -57,9 +63,8 @@ public static ProcessDefinition merge(ProcessDefinition instance, ProcessDefinit
instance.setEndpoint(doMerge(data.getEndpoint(), instance.getEndpoint()));
instance.setDescription(doMerge(data.getDescription(), instance.getDescription()));
instance.setAnnotations(doMerge(data.getAnnotations(), instance.getAnnotations()));
instance.setMetadata(CommonUtils.mergeMap(data.getMetadata(), instance.getMetadata()));
instance.setMetadata(CommonUtils.mergeMap(toStringMap(data.getMetadata()), instance.getMetadata()));
instance.setNodes(doMerge(nodeDefinitions(data), instance.getNodes()));

instance.setSource(doMerge(data.getSource(), instance.getSource()));
return instance;
}
Expand All @@ -77,7 +82,7 @@ private static Node nodeDefinition(NodeDefinition definition) {
node.setName(definition.getName());
node.setUniqueId(definition.getUniqueId());
node.setType(definition.getType());
node.setMetadata(definition.getMetadata());
node.setMetadata(toStringMap(definition.getMetadata()));
return node;
}

Expand All @@ -90,4 +95,22 @@ private static <T> T doMerge(T incoming, T current) {
return current;
}

private static Map<String, String> toStringMap(Map<String, ?> input) {
if (input == null) {
return null;
}
return input.entrySet().stream()
.map(entry -> {
if (String.class.isInstance(entry.getValue())) {
return entry;
}
String value = null;
try {
value = JsonUtils.getObjectMapper().writeValueAsString(entry.getValue());
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
return new AbstractMap.SimpleEntry<>(entry.getKey(), value);
}).collect(Collectors.toMap(Map.Entry::getKey, e -> (String) e.getValue()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.kie.kogito.index;

import java.util.HashMap;
import java.util.Map;

import org.junit.jupiter.api.Test;
Expand All @@ -28,18 +29,26 @@ public class CommonUtilsTest {

@Test
void testSimpleMergeMap() {
Map src = Map.of("name", "Javierito", "different", "remain");
Map target = Map.of("name", "Fulanito", "other", "remain");
Map<String, String> src = Map.of("name", "Javierito", "different", "remain");
Map<String, String> target = Map.of("name", "Fulanito", "other", "remain");
assertThat(CommonUtils.mergeMap(src, target)).isEqualTo(Map.of("name", "Javierito", "other", "remain", "different", "remain"));
}

@Test
void testNullMergeMap() {
Map<String, String> src = new HashMap<>();
src.put("name", null);
src.put("different", "remain");
Map<String, String> target = Map.of("name", "Fulanito", "other", "remain");
assertThat(CommonUtils.mergeMap(src, target)).isEqualTo(Map.of("name", "Fulanito", "other", "remain", "different", "remain"));
}

@Test
void testComplexMergeMap() {
Map nestedSrc = Map.of("name", "Javierito", "different", "remain");
Map nestedTarget = Map.of("name", "Fulanito", "other", "remain");
Map src = Map.of("nested", nestedSrc);
Map target = Map.of("nested", nestedTarget);
Map<String, String> nestedSrc = Map.of("name", "Javierito", "different", "remain");
Map<String, String> nestedTarget = Map.of("name", "Fulanito", "other", "remain");
Map<String, Object> src = Map.of("nested", nestedSrc);
Map<String, Object> target = Map.of("nested", nestedTarget);
assertThat(CommonUtils.mergeMap(src, target)).isEqualTo(Map.of("nested", Map.of("name", "Javierito", "other", "remain", "different", "remain")));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ void testSimpleMerge() {
assertThat(JsonUtils.mergeVariable("user1", "manolo", null)).isEqualTo(expected);
}

@Test
void testNullMerge() {
ObjectNode expected = ObjectMapperFactory.get().createObjectNode().put("user1", "manolo");
assertThat(JsonUtils.mergeVariable("user1", "manolo", null)).isEqualTo(expected);
}

@Test
void testComplexMergeWithDot() {
ObjectNode expected = ObjectMapperFactory.get().createObjectNode().set("key", ObjectMapperFactory.get().createObjectNode().put("user1", "manolo").put("user2", "pepe"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class Node {
@JsonProperty("nodeType")
private String type;
private String uniqueId;
private Map<String, ?> metadata;
private Map<String, String> metadata;

public String getId() {
return id;
Expand Down Expand Up @@ -64,11 +64,11 @@ public void setUniqueId(String uniqueId) {
this.uniqueId = uniqueId;
}

public Map<String, ?> getMetadata() {
public Map<String, String> getMetadata() {
return metadata;
}

public void setMetadata(Map<String, ?> metadata) {
public void setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ProcessDefinition {
private String source;
private String description;
private Set<String> annotations;
private Map<String, ?> metadata;
private Map<String, String> metadata;
private List<Node> nodes;

public String getId() {
Expand Down Expand Up @@ -126,11 +126,11 @@ public void setAnnotations(Set<String> annotations) {
this.annotations = annotations;
}

public Map<String, ?> getMetadata() {
public Map<String, String> getMetadata() {
return metadata;
}

public void setMetadata(Map<String, ?> metadata) {
public void setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ public Entry readFrom(ProtoStreamReader reader) throws IOException {
@Override
public void writeTo(ProtoStreamWriter writer, Entry entry) throws IOException {
writer.writeString(KEY, entry.getKey());
writer.writeString(VALUE, toString(entry.getValue()));
}

private String toString(Object str) {
return str != null ? str.toString() : null;
writer.writeString(VALUE, entry.getValue());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ Function<NodeMetadata, Map<String, String>> toNodeMetadataMap() {
};
}

Function<Map<String, ?>, NodeMetadata> toNodeMetadata() {
Function<Map<String, String>, NodeMetadata> toNodeMetadata() {
return m -> {
if (m == null) {
return null;
}
NodeMetadata meta = new NodeMetadata();
meta.setAction((String) m.get(NodeMetadataMarshaller.ACTION));
meta.setState((String) m.get(NodeMetadataMarshaller.STATE));
meta.setUniqueId((String) m.get(NodeMetadataMarshaller.UNIQUE_ID));
meta.setBranch((String) m.get(NodeMetadataMarshaller.BRANCH));
meta.setAction(m.get(NodeMetadataMarshaller.ACTION));
meta.setState(m.get(NodeMetadataMarshaller.STATE));
meta.setUniqueId(m.get(NodeMetadataMarshaller.UNIQUE_ID));
meta.setBranch(m.get(NodeMetadataMarshaller.BRANCH));
return meta;
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public ProcessDefinition readFrom(ProtoStreamReader reader) throws IOException {
return pd;
}

private static Map<String, ?> buildMetadata(ProtoStreamReader reader) throws IOException {
private static Map<String, String> buildMetadata(ProtoStreamReader reader) throws IOException {
return Optional.ofNullable(reader.readCollection(METADATA, new HashSet<>(), Entry.class))
.map(entries -> entries.stream().collect(Collectors.toMap(Entry::getKey, Entry::getValue)))
.orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public class Entry {

private String key;

private Object value;
private String value;

public Entry() {
}

public Entry(String key, Object value) {
public Entry(String key, String value) {
this.key = key;
this.value = value;
}
Expand All @@ -43,7 +43,7 @@ public void setKey(String key) {
this.key = key;
}

public Object getValue() {
public String getValue() {
return value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class NodeEntity extends AbstractEntity {
foreignKey = @ForeignKey(name = "fk_definitions_nodes_metadata_definitions_nodes"))
@MapKeyColumn(name = "name")
@Column(name = "meta_value")
private Map<String, ?> metadata;
private Map<String, String> metadata;

@Id
@ManyToOne(cascade = CascadeType.ALL, optional = false)
Expand Down Expand Up @@ -96,11 +96,11 @@ public void setType(String type) {
this.type = type;
}

public Map<String, ?> getMetadata() {
public Map<String, String> getMetadata() {
return metadata;
}

public void setMetadata(Map<String, ?> metadata) {
public void setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class ProcessDefinitionEntity extends AbstractEntity {
foreignKey = @ForeignKey(name = "fk_definitions_metadata"))
@MapKeyColumn(name = "name")
@Column(name = "meta_value")
private Map<String, ?> metadata;
private Map<String, String> metadata;

@Override
public String getId() {
Expand Down Expand Up @@ -171,11 +171,11 @@ public void setAnnotations(Set<String> annotations) {
this.annotations = annotations;
}

public Map<String, ?> getMetadata() {
public Map<String, String> getMetadata() {
return metadata;
}

public void setMetadata(Map<String, ?> metadata) {
public void setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class ProcessDefinitionEntity {

private Set<String> annotations;

private Map<String, ?> metadata;
private Map<String, String> metadata;

private Set<String> roles;

Expand Down Expand Up @@ -148,11 +148,11 @@ public void setAnnotations(Set<String> annotations) {
this.annotations = annotations;
}

public Map<String, ?> getMetadata() {
public Map<String, String> getMetadata() {
return metadata;
}

public void setMetadata(Map<String, ?> metadata) {
public void setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
}

Expand Down Expand Up @@ -193,7 +193,7 @@ public static class NodeEntity {
private String uniqueId;
private String type;

private Map<String, ?> metadata;
private Map<String, String> metadata;

public String getId() {
return id;
Expand Down Expand Up @@ -227,11 +227,11 @@ public void setType(String type) {
this.type = type;
}

public Map<String, ?> getMetadata() {
public Map<String, String> getMetadata() {
return metadata;
}

public void setMetadata(Map<String, ?> metadata) {
public void setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
}

Expand Down

0 comments on commit 5d68dcb

Please sign in to comment.