|
| 1 | +package org.hisp.dhis.jsontree; |
| 2 | + |
| 3 | +import org.hisp.dhis.jsontree.JsonBuilder.JsonArrayBuilder; |
| 4 | + |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.HashSet; |
| 7 | +import java.util.LinkedHashMap; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.Set; |
| 11 | +import java.util.function.Consumer; |
| 12 | +import java.util.function.Function; |
| 13 | + |
| 14 | +import static java.util.stream.Collectors.toMap; |
| 15 | +import static org.hisp.dhis.jsontree.JsonBuilder.createArray; |
| 16 | +import static org.hisp.dhis.jsontree.JsonNodeType.OBJECT; |
| 17 | +import static org.hisp.dhis.jsontree.JsonPatchException.clash; |
| 18 | + |
| 19 | +/** |
| 20 | + * {@linkplain JsonNodeOperation}s are used to make bulk modifications using {@link JsonNode#patch(List)}. |
| 21 | + * <p> |
| 22 | + * {@linkplain JsonNodeOperation} is a path based operation that is not yet "bound" to target. |
| 23 | + * <p> |
| 24 | + * The order of operations made into a set does not matter. Any order has the same outcome when applied to the same |
| 25 | + * target. |
| 26 | + * |
| 27 | + * @author Jan Bernitt |
| 28 | + * @since 1.1 |
| 29 | + */ |
| 30 | +sealed public interface JsonNodeOperation { |
| 31 | + |
| 32 | + static String parentPath( String path ) { |
| 33 | + //TODO move callers to JsonPath |
| 34 | + return JsonPath.of( path ).dropLastSegment().toString(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @return the target of the operation |
| 39 | + */ |
| 40 | + String path(); |
| 41 | + |
| 42 | + /** |
| 43 | + * @return true when this operation targets an array index |
| 44 | + */ |
| 45 | + default boolean isArrayOp() { |
| 46 | + return path().endsWith( "]" ); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @return true when this is an {@link Insert} operation |
| 51 | + */ |
| 52 | + default boolean isRemove() { |
| 53 | + return this instanceof Remove; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @param path relative path to remove |
| 58 | + */ |
| 59 | + record Remove(String path) implements JsonNodeOperation {} |
| 60 | + |
| 61 | + /** |
| 62 | + * <h4>Insert into Arrays</h4> |
| 63 | + * In an array the value is inserted before the existing value at the path index. That means the current value at |
| 64 | + * the path index will be after the inserted value in the updated tree. |
| 65 | + * <p> |
| 66 | + * <h4>Merge</h4> |
| 67 | + * <ul> |
| 68 | + * <li>object + object = add all properties of inserted object to target object</li> |
| 69 | + * <li>array + array = insert all elements of inserted array at target index into the target array</li> |
| 70 | + * <li>array + primitive = append inserted element to target array</li> |
| 71 | + * <li>primitive + primitive = create array with current value and inserted value</li> |
| 72 | + * <li>* + object = trying to merge an object value into a non object target is an error</li> |
| 73 | + * </ul> |
| 74 | + * |
| 75 | + * @param path relative path to the target property, this either is the root, an object member or an array index or |
| 76 | + * range |
| 77 | + * @param value the new value |
| 78 | + * @param merge when true, insert the value's items not the value itself |
| 79 | + */ |
| 80 | + record Insert(String path, JsonNode value, boolean merge) implements JsonNodeOperation { |
| 81 | + public Insert(String path, JsonNode value) { this(path, value, false); } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * As each target path may only occur once a set of operations may need folding inserts for arrays. This means each |
| 86 | + * operation that wants to insert at the same index in the same target array is merged into a single operation |
| 87 | + * inserting all the values in the order they occur in the #ops parameter. |
| 88 | + * |
| 89 | + * @param ops a set of ops that may contain multiple inserts targeting the same array index |
| 90 | + * @return a list of operations where the clashing array inserts have been merged by concatenating the inserted |
| 91 | + * elements |
| 92 | + * @throws JsonPathException if the ops is found to contain other operations clashing on same path (that are not |
| 93 | + * array inserts) |
| 94 | + */ |
| 95 | + static List<JsonNodeOperation> mergeArrayInserts(List<JsonNodeOperation> ops) { |
| 96 | + if (ops.stream().filter( JsonNodeOperation::isArrayOp ).count() < 2) return ops; |
| 97 | + return List.copyOf( ops.stream() |
| 98 | + .collect( toMap(JsonNodeOperation::path, Function.identity(), (op1, op2) -> { |
| 99 | + if (!op1.isArrayOp() || op1.isRemove() || op2.isRemove() ) |
| 100 | + throw JsonPatchException.clash( ops, op1, op2 ); |
| 101 | + JsonNode merged = createArray( arr -> { |
| 102 | + Consumer<JsonNodeOperation> add = op -> { |
| 103 | + Insert insert = (Insert) op; |
| 104 | + if ( insert.merge() ) { |
| 105 | + arr.addElements( insert.value().elements(), JsonArrayBuilder::addElement ); |
| 106 | + } else { |
| 107 | + arr.addElement( insert.value() ); |
| 108 | + } |
| 109 | + }; |
| 110 | + add.accept( op1 ); |
| 111 | + add.accept( op2 ); |
| 112 | + } ); |
| 113 | + return new Insert( op1.path(), merged, true ); |
| 114 | + }, LinkedHashMap::new ) ).values()); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @param ops set of patch operations |
| 119 | + * @implNote array merge inserts don't need special handling as it is irrelevant how many elements are inserted at |
| 120 | + * the target index as each operation is independent and uniquely targets an insert position in the target array in |
| 121 | + * its state before any change |
| 122 | + */ |
| 123 | + static void checkPatch( List<JsonNodeOperation> ops ) { |
| 124 | + if (ops.size() < 2) return; |
| 125 | + Map<String, JsonNodeOperation> opsByPath = new HashMap<>(); |
| 126 | + Set<String> parents = new HashSet<>(); |
| 127 | + for ( JsonNodeOperation op : ops ) { |
| 128 | + String path = op.path(); |
| 129 | + if (op instanceof Insert insert && insert.merge && insert.value.getType() == OBJECT) { |
| 130 | + insert.value.keys().forEach( p -> checkPatchPath( ops, op, path+"."+p, opsByPath, parents ) ); |
| 131 | + checkPatchParents( ops, op, path, opsByPath, parents ); |
| 132 | + } else { |
| 133 | + checkPatchPath( ops, op, path, opsByPath, parents ); |
| 134 | + checkPatchParents( ops, op, parentPath( path ), opsByPath, parents ); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private static void checkPatchPath( List<JsonNodeOperation> ops, JsonNodeOperation op, String path, |
| 140 | + Map<String, JsonNodeOperation> opsByPath, Set<String> parents ) { |
| 141 | + if ( opsByPath.containsKey( path ) ) throw clash( ops, opsByPath.get( path ), op ); |
| 142 | + if ( parents.contains( path ) ) throw clash( ops, op, null ); |
| 143 | + opsByPath.put( path, op ); |
| 144 | + } |
| 145 | + |
| 146 | + private static void checkPatchParents( List<JsonNodeOperation> ops, JsonNodeOperation op, String path, |
| 147 | + Map<String, JsonNodeOperation> opsByPath, Set<String> parents ) { |
| 148 | + while ( !path.isEmpty() ) { |
| 149 | + if ( opsByPath.containsKey( path ) ) throw clash( ops, opsByPath.get( path ), op ); |
| 150 | + parents.add( path ); |
| 151 | + path = parentPath( path ); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments