Skip to content

Commit

Permalink
Merge pull request #114 from statisticsnorway/regex-indices
Browse files Browse the repository at this point in the history
Regex replace name indices when validating glob
  • Loading branch information
mallport authored Jun 25, 2024
2 parents 88b4db6 + 38a5b7f commit b82894c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/main/java/no/ssb/dlp/pseudo/service/pseudo/PseudoField.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,15 @@ public PseudoField(String name, String pattern, String pseudoFunc, EncryptedKeys
if (keyset != null) {
pseudoConfig.getKeysets().add(keyset);
}
final boolean validPattern = new FieldDescriptor(name).globMatches(pattern);
if (!validPattern) {
throw new IllegalArgumentException(String.format("The pattern '%s' will not match the field name '%s'. " +
"Are you sure you didn't mean to use '/%s'?", pattern, name, pattern));

if (name != null) {
// Regex replace such that "path[9]/thing" -> "path/thing"
String nameNoIndices = name.replaceAll("\\[.*?]", "");
final boolean validPattern = new FieldDescriptor(nameNoIndices).globMatches(pattern);
if (!validPattern) {
throw new IllegalArgumentException(String.format("The pattern '%s' will not match the field name '%s'. " +
"Are you sure you didn't mean to use '/%s'?", pattern, name, pattern));
}
}
pseudoConfig.getRules().add(new PseudoFuncRule(name, pattern, pseudoFunc));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,21 @@ void preprocessorWithNullValues() {
verify(recordMapProcessor, times(2)).init(any());
}

@Test
void nameWithIndices() {
setUpProcessorMocks();

when(recordMapProcessor.hasPreprocessors()).thenReturn(true);
when(recordMapProcessor.init(any())).thenReturn(Collections.singletonMap("testField", "initializedValue"));

PseudoField pseudoField = new PseudoField("path[9]/thing", "**/path/thing", null, null);
List<String> values = Arrays.asList("v1", null, "v2");

Completable result = pseudoField.getPreprocessor(values, recordMapProcessor);

TestObserver<Void> testObserver = result.test();
testObserver.assertComplete();
testObserver.assertNoErrors();
}

}

0 comments on commit b82894c

Please sign in to comment.