Skip to content
This repository was archived by the owner on May 16, 2025. It is now read-only.

Commit 8b65b6e

Browse files
authored
Merge pull request #373 from metafacture/maxEntityCount
Optionally specify limit for number of entities in a record.
2 parents 0b20caa + a10b3d8 commit 8b65b6e

File tree

8 files changed

+53
-5
lines changed

8 files changed

+53
-5
lines changed

metafix-runner/build.gradle

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,13 @@ application {
5959
doNotTrackState('Accessing unreadable inputs is not supported.')
6060
}
6161
}
62+
63+
tasks.withType(JavaExec) {
64+
doFirst {
65+
def prefix = project.group + '.'
66+
67+
System.properties.each { k, v ->
68+
if (k.startsWith(prefix)) systemProperties[k] = v
69+
}
70+
}
71+
}

metafix/integrationTest.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ function rm_temp() {
7474
}
7575

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

8081
function run_catmandu() {
@@ -224,10 +225,11 @@ function run_tests() {
224225

225226
metafix_command_output="$test_directory/metafix.out"
226227
metafix_command_error="$test_directory/metafix.err"
228+
metafix_command_args="$test_directory/metafix.args"
227229

228230
metafix_start_time=$(current_time)
229231

230-
run_metafix "$test_directory/$metafix_file" >"$metafix_command_output" 2>"$metafix_command_error"
232+
run_metafix "$test_directory/$metafix_file" $(cat "$metafix_command_args" 2>/dev/null || true) >"$metafix_command_output" 2>"$metafix_command_error"
231233
metafix_exit_status=$?
232234

233235
metafix_elapsed_time=$(elapsed_time "$metafix_start_time")

metafix/src/main/java/org/metafacture/metafix/Metafix.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public class Metafix implements StreamPipe<StreamReceiver>, Maps {
7676

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

79+
public static final int MAX_ENTITY_COUNT = Integer.getInteger("org.metafacture.metafix.maxEntityCount", -1);
80+
7981
private static final Logger LOG = LoggerFactory.getLogger(Metafix.class);
8082

8183
private static final String ENTITIES_NOT_BALANCED = "Entity starts and ends are not balanced";
@@ -239,7 +241,7 @@ public void startRecord(final String identifier) {
239241
flattener.startRecord(identifier);
240242
entityCountStack.clear();
241243
entityCount = 0;
242-
entityCountStack.add(Integer.valueOf(entityCount));
244+
entityCountStack.add(entityCount);
243245
recordIdentifier = identifier;
244246
entities = new ArrayList<>();
245247
}
@@ -313,22 +315,36 @@ public void startEntity(final String name) {
313315
throw new IllegalArgumentException("Entity name must not be null.");
314316
}
315317

318+
++entityCount;
319+
if (maxEntityCountExceeded()) {
320+
LOG.debug("Maximum number of entities exceeded: {}/{}", entityCount, MAX_ENTITY_COUNT);
321+
return;
322+
}
323+
316324
final Value value = isArrayName(name) ? Value.newArray() : Value.newHash();
317325
addValue(name, value);
318326
entities.add(value);
319327

320-
entityCountStack.push(Integer.valueOf(++entityCount));
328+
entityCountStack.push(entityCount);
321329
flattener.startEntity(name);
322330
}
323331

324332
@Override
325333
public void endEntity() {
326-
entityCountStack.pop().intValue();
334+
if (maxEntityCountExceeded()) {
335+
return;
336+
}
337+
338+
entityCountStack.pop();
327339
flattener.endEntity();
328340
}
329341

330342
@Override
331343
public void literal(final String name, final String value) {
344+
if (maxEntityCountExceeded()) {
345+
return;
346+
}
347+
332348
LOG.debug("Putting '{}': '{}'", name, value);
333349
flattener.literal(name, value);
334350
}
@@ -438,6 +454,10 @@ public String getEntityMemberName() {
438454
return entityMemberName;
439455
}
440456

457+
private boolean maxEntityCountExceeded() {
458+
return MAX_ENTITY_COUNT >= 0 && entityCount > MAX_ENTITY_COUNT;
459+
}
460+
441461
public enum Strictness {
442462

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

0 commit comments

Comments
 (0)