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

Commit

Permalink
Require explicit array initialization. (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackwinter committed Jan 19, 2022
1 parent a42943d commit 7d9b2a4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 27 deletions.
29 changes: 2 additions & 27 deletions metafix/src/main/java/org/metafacture/metafix/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,21 +305,6 @@ void apply(final Hash hash, final String field, final Value value) {
void apply(final Hash hash, final String field, final Value value) {
hash.add(field, 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 Value value) {
hash.add(nextIndex(hash), field.equals(ReservedField.$append.name()) ? value : newHash(h -> h.put(field, value)));
}

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, Value value);
Expand Down Expand Up @@ -697,7 +682,7 @@ private Value insert(final InsertMode mode, final String[] fields, final Value n
// TODO: move impl into enum elements, here call only value.insert
value.matchType()
.ifArray(a -> a.insert(mode, tail, newValue))
.ifHash(h -> h.insert(insertMode(mode, field, tail), tail, newValue))
.ifHash(h -> h.insert(mode, tail, newValue))
.orElseThrow();
}
}
Expand All @@ -708,7 +693,7 @@ private Value insert(final InsertMode mode, final String[] fields, final Value n
private Value processRef(final InsertMode mode, final Value newValue, final String field, final String[] tail) {
final Value referencedValue = getReferencedValue(field);
if (referencedValue != null) {
return referencedValue.asHash().insert(insertMode(mode, field, tail), tail, newValue);
return referencedValue.asHash().insert(mode, tail, newValue);
}
else {
throw new IllegalArgumentException("Using ref, but can't find: " + field + " in: " + this);
Expand Down Expand Up @@ -738,16 +723,6 @@ private Value getReferencedValue(final String field) {
return referencedValue;
}

private InsertMode insertMode(final InsertMode mode, final String field, final String[] tail) {
// 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);
final boolean nextIsRef = tail.length > 0 && (
tail[0].startsWith(ReservedField.$first.name()) ||
tail[0].startsWith(ReservedField.$last.name()) ||
isNumber(tail[0]));
return isIndexedArray && !nextIsRef ? InsertMode.INDEXED : mode;
}

/**
* Removes the given field/value pair from this hash.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ public void complexAppendWithArrayOfObjects() {
@Test
public void appendWithWildcard() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('stringimals[]')",
"copy_field('?nimal', 'stringimals[].$append')"
),
i -> {
Expand Down Expand Up @@ -542,6 +543,7 @@ public void simpleCopyWithWildcard() {
@Test
public void appendWithMultipleWildcards() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('stringimals[]')",
"copy_field('?ni??l', 'stringimals[].$append')"
),
i -> {
Expand Down Expand Up @@ -572,6 +574,7 @@ public void appendWithMultipleWildcards() {
@Test
public void appendWithAsteriksWildcard() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('stringimals[]')",
"copy_field('*al', 'stringimals[].$append')"
),
i -> {
Expand Down Expand Up @@ -601,6 +604,7 @@ public void appendWithAsteriksWildcard() {
@Test
public void appendWithBracketWildcard() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('stringimals[]')",
"copy_field('[ac]nimal', 'stringimals[].$append')"
),
i -> {
Expand Down Expand Up @@ -630,6 +634,7 @@ public void appendWithBracketWildcard() {
// See https://github.com/metafacture/metafacture-fix/issues/89
public void appendWithAsteriksWildcardAtTheEnd() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('stringimals[]')",
"copy_field('ani*', 'stringimals[].$append')"
),
i -> {
Expand Down

0 comments on commit 7d9b2a4

Please sign in to comment.