Skip to content
This repository has been archived by the owner on Jan 27, 2025. It is now read-only.

Commit

Permalink
Merge pull request #87 from metafacture/82-append
Browse files Browse the repository at this point in the history
Fix appending issues
  • Loading branch information
fsteeg authored Dec 16, 2021
2 parents 966e09f + 60ccb19 commit e2d561e
Show file tree
Hide file tree
Showing 3 changed files with 377 additions and 9 deletions.
1 change: 1 addition & 0 deletions metafix/src/main/java/org/metafacture/metafix/Metafix.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class Metafix implements StreamPipe<StreamReceiver>, Maps { // checkstyle
public static final String VAR_START = "$[";
public static final String VAR_END = "]";
public static final Map<String, String> NO_VARS = Collections.emptyMap();
public static final String ARRAY_MARKER = "[]";

private static final Logger LOG = LoggerFactory.getLogger(Metafix.class);

Expand Down
43 changes: 35 additions & 8 deletions metafix/src/main/java/org/metafacture/metafix/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,23 @@ void apply(final Hash hash, final String field, final String value) {
void apply(final Hash hash, final String field, final String value) {
hash.add(field, new Value(value));
}
},
/* For an indexed representation of arrays as hashes with 1, 2, 3 etc. keys.
* i.e. ["a", "b", "c"] as { "1":"a", "2":"b", "3": "c" }
* This is what is produced by JsonDecoder and Metafix itself for arrays.
* TODO? maybe this would be a good general internal representation, resulting
* in every value being either a hash or a string, no more separate array type.*/
INDEXED {
@Override
void apply(final Hash hash, final String field, final String value) {
final Value newValue = field.equals(APPEND_FIELD) ? new Value(value) :
newHash(h -> h.put(field, new Value(value)));
hash.add(nextIndex(hash), newValue);
}

private String nextIndex(final Hash hash) {
return "" + (hash.size() + 1) /* TODO? check if keys are actually all ints? */;
}
};

abstract void apply(Hash hash, String field, String value);
Expand Down Expand Up @@ -385,6 +402,10 @@ private void insert(final InsertMode mode, final String[] fields, final String n
// TODO: WDCD? descend into the array?
break;
case APPEND_FIELD:
if (fields.length == 1) {
add(new Value(newValue));
return;
}
add(newHash(h -> h.insert(mode, tail(fields), newValue)));
break;
case LAST_FIELD:
Expand Down Expand Up @@ -578,29 +599,30 @@ public Value insert(final InsertMode mode, final String fieldPath, final String

private Value insert(final InsertMode mode, final String[] fields, final String newValue) {
final String field = fields[0];
if (field.equals(APPEND_FIELD) || field.equals(LAST_FIELD)) {
// TODO: WDCD? $last, $append skipped for hashes here:
return insert(mode, tail(fields), newValue);
}
if (fields.length == 1) {
if (fields[0].equals(ASTERISK)) {
if (field.equals(ASTERISK)) {
//TODO: WDCD? insert into each element?
}
else {
mode.apply(this, field, newValue);
}
}
else {
if (field.equals(APPEND_FIELD) || field.equals(LAST_FIELD)) {
// TODO: WDCD? $last, $append skipped for hashes here:
return insert(mode, tail(fields), newValue);
}
if (!containsField(field)) {
put(field, newHash());
}

final Value value = get(field);
if (value != null) {
switch (value.type) {
// TODO: move impl into enum elements, here call only value.insert
case Hash:
value.asHash().insert(mode, tail(fields), newValue);
// if the field is marked as array, this hash should be smth. like { 1=a, 2=b }
final boolean isIndexedArray = field.endsWith(Metafix.ARRAY_MARKER);
value.asHash().insert(isIndexedArray ? InsertMode.INDEXED : mode, tail(fields), newValue);
break;
case Array:
value.asArray().insert(mode, tail(fields), newValue);
Expand Down Expand Up @@ -670,7 +692,12 @@ private void appendValue(final String[] newName, final Value v) {
// TODO: do something here?
break;
case Hash:
appendValue(newName, v.asHash().find(tail(newName)));
if (newName.length == 1) {
add(newName[0], v);
}
else {
appendValue(newName, v.asHash().find(tail(newName)));
}
break;
default:
break;
Expand Down
Loading

0 comments on commit e2d561e

Please sign in to comment.