Skip to content

Commit

Permalink
Merge pull request #373 from metafacture/maxEntityCount
Browse files Browse the repository at this point in the history
Optionally specify limit for number of entities in a record.
  • Loading branch information
blackwinter authored Dec 3, 2024
2 parents 0b20caa + a10b3d8 commit 8b65b6e
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 5 deletions.
10 changes: 10 additions & 0 deletions metafix-runner/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,13 @@ application {
doNotTrackState('Accessing unreadable inputs is not supported.')
}
}

tasks.withType(JavaExec) {
doFirst {
def prefix = project.group + '.'

System.properties.each { k, v ->
if (k.startsWith(prefix)) systemProperties[k] = v
}
}
}
6 changes: 4 additions & 2 deletions metafix/integrationTest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ function rm_temp() {
}

function run_metafix() {
$gradle_command --console=plain -p "$root_directory" :metafix-runner:run --args="$1" -P${noprofile}profile="${1%.*}"
local file=$1; shift
$gradle_command --console=plain -p "$root_directory" :metafix-runner:run --args="$file" -P${noprofile}profile="${file%.*}" $@
}

function run_catmandu() {
Expand Down Expand Up @@ -224,10 +225,11 @@ function run_tests() {

metafix_command_output="$test_directory/metafix.out"
metafix_command_error="$test_directory/metafix.err"
metafix_command_args="$test_directory/metafix.args"

metafix_start_time=$(current_time)

run_metafix "$test_directory/$metafix_file" >"$metafix_command_output" 2>"$metafix_command_error"
run_metafix "$test_directory/$metafix_file" $(cat "$metafix_command_args" 2>/dev/null || true) >"$metafix_command_output" 2>"$metafix_command_error"
metafix_exit_status=$?

metafix_elapsed_time=$(elapsed_time "$metafix_start_time")
Expand Down
26 changes: 23 additions & 3 deletions metafix/src/main/java/org/metafacture/metafix/Metafix.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public class Metafix implements StreamPipe<StreamReceiver>, Maps {

public static final Map<String, String> NO_VARS = Collections.emptyMap();

public static final int MAX_ENTITY_COUNT = Integer.getInteger("org.metafacture.metafix.maxEntityCount", -1);

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

private static final String ENTITIES_NOT_BALANCED = "Entity starts and ends are not balanced";
Expand Down Expand Up @@ -239,7 +241,7 @@ public void startRecord(final String identifier) {
flattener.startRecord(identifier);
entityCountStack.clear();
entityCount = 0;
entityCountStack.add(Integer.valueOf(entityCount));
entityCountStack.add(entityCount);
recordIdentifier = identifier;
entities = new ArrayList<>();
}
Expand Down Expand Up @@ -313,22 +315,36 @@ public void startEntity(final String name) {
throw new IllegalArgumentException("Entity name must not be null.");
}

++entityCount;
if (maxEntityCountExceeded()) {
LOG.debug("Maximum number of entities exceeded: {}/{}", entityCount, MAX_ENTITY_COUNT);
return;
}

final Value value = isArrayName(name) ? Value.newArray() : Value.newHash();
addValue(name, value);
entities.add(value);

entityCountStack.push(Integer.valueOf(++entityCount));
entityCountStack.push(entityCount);
flattener.startEntity(name);
}

@Override
public void endEntity() {
entityCountStack.pop().intValue();
if (maxEntityCountExceeded()) {
return;
}

entityCountStack.pop();
flattener.endEntity();
}

@Override
public void literal(final String name, final String value) {
if (maxEntityCountExceeded()) {
return;
}

LOG.debug("Putting '{}': '{}'", name, value);
flattener.literal(name, value);
}
Expand Down Expand Up @@ -438,6 +454,10 @@ public String getEntityMemberName() {
return entityMemberName;
}

private boolean maxEntityCountExceeded() {
return MAX_ENTITY_COUNT >= 0 && entityCount > MAX_ENTITY_COUNT;
}

public enum Strictness {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"key1":"value1","key2":"value2","key3":"value3","key4":"value4"}
{"key1":"value1","key2":["v1","v2"]}
{"key1":"value1","key2":["v1","v2"],"key3":"value3","key4":"value4"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"key1":"value1","key2":"value2","key3":"value3","key4":"value4"}
{"key1":"value1","key2":["v1","v2"],"key3":["v3"],"key4":"value4"}
{"key1":"value1","key2":["v1","v2"],"key3":"value3","key4":"value4"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-Dorg.metafacture.metafix.maxEntityCount=1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nothing()
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FLUX_DIR + "input.json"
|open-file
|as-lines
|decode-json
|fix(FLUX_DIR + "test.fix")
|encode-json
|write(FLUX_DIR + "output-metafix.json")
;

0 comments on commit 8b65b6e

Please sign in to comment.