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

Handle and access repeated fields as arrays #78

Merged
merged 7 commits into from
Nov 30, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private Map<String, String> fileMap(final String location, final String separato
private static final Pattern NAMED_GROUP_PATTERN = Pattern.compile("\\(\\?<(.+?)>");

private static final String EMPTY = "";
private static final String DOT_APPEND = "." + Value.Hash.APPEND_FIELD;
private static final String DOT_APPEND = "." + Value.APPEND_FIELD;

abstract void apply(Record record, List<String> params, Map<String, String> options);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public Predicate<Value> of(final String string) {
equal {
@Override
public Predicate<Value> of(final String string) {
return v -> v.toString().equals(string);
return v -> v.toString().equals(string);
}
},
match {
Expand Down
22 changes: 5 additions & 17 deletions metafix/src/main/java/org/metafacture/metafix/Metafix.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ private void emit(final String field, final Value value) {
Value.asList(value, array -> {
final boolean isMulti = array.size() > 1 || value.isArray();
if (isMulti) {
outputStreamReceiver.startEntity(field + "[]");
outputStreamReceiver.startEntity(field);
}

for (int i = 0; i < array.size(); ++i) {
final Value arrayValue = array.get(i);

if (arrayValue.isHash()) {
outputStreamReceiver.startEntity(isMulti ? "" : field);
outputStreamReceiver.startEntity(isMulti ? (i + 1) + "" : field);
arrayValue.asHash().forEach(this::emit);
outputStreamReceiver.endEntity();
}
Expand All @@ -182,21 +182,9 @@ public void startEntity(final String name) {
entities.size() <= currentEntityIndex ? null : entities.get(currentEntityIndex);
entityCountStack.push(Integer.valueOf(entityCount));
flattener.startEntity(name);
entities.add(currentEntity(name, previousEntity != null ? previousEntity : currentRecord));
}

private Value.Hash currentEntity(final String name, final Value.Hash previousEntity) {
final Value existingValue = previousEntity != null ? previousEntity.get(name) : null;
final Value.Hash currentEntity;
if (existingValue != null && existingValue.isHash()) {
currentEntity = previousEntity.get(name).asHash();
}
else {
final Value value = Value.newHash();
currentEntity = value.asHash();
(previousEntity != null ? previousEntity : currentRecord).add(name, value);
}
return currentEntity;
final Value value = Value.newHash();
(previousEntity != null ? previousEntity : currentRecord).add(name, value);
entities.add(value.asHash());
}

@Override
Expand Down
Loading