Skip to content

Commit

Permalink
Rewrite Mapper interface som that the Mapper can populate values in P…
Browse files Browse the repository at this point in the history
…seudoFuncOutput. Simplified MapFunc to delegate most of the work to the Mapper.
  • Loading branch information
Skaar, Bjørn-Andre committed Dec 11, 2023
1 parent 938a059 commit 769ee1c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 45 deletions.
21 changes: 6 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>no.ssb.dapla.dlp.pseudo.func</groupId>
<artifactId>dapla-dlp-pseudo-func</artifactId>
<version>1.2.9-SNAPSHOT</version>
<version>1.3.0-SNAPSHOT</version>
<name>dapla-dlp-pseudo-func</name>

<properties>
Expand Down Expand Up @@ -186,29 +186,20 @@

<repositories>
<repository>
<id>artifact-registry</id>
<url>${artifact-registry.url}</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
<repository>
<id>artifact-registry-snapshot</id>
<id>artifact-registry</id>
<url>${artifact-registry.url}</url>
<releases>
<enabled>false</enabled>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
</repositories>

<profiles>
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/no/ssb/dapla/dlp/pseudo/func/PseudoFuncOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Value
@NoArgsConstructor
public class PseudoFuncOutput {
private List<Object> values = new ArrayList<>();
private List<String> warnings = new ArrayList<>();
List<Object> values = new ArrayList<>();
List<String> warnings = new ArrayList<>();
Map<String, String> metadata = new HashMap<>();


public PseudoFuncOutput(Object v) {
if (v instanceof Collection) {
Expand All @@ -39,14 +43,18 @@ public void addWarning(String warning) {
warnings.add(warning);
}

public void addMetadata(String key, String value) {
metadata.put(key, value);
}

public List<String> getStringValues() {
return values.stream()
.map(String::valueOf)
.collect(Collectors.toList());
}

public Object getFirstValue() {
return getValues().get(0);
return getValues().getFirst();
}

@Override
Expand All @@ -57,4 +65,5 @@ public String toString() {
public boolean hasWarnings() {
return ! warnings.isEmpty();
}

}
27 changes: 3 additions & 24 deletions src/main/java/no/ssb/dapla/dlp/pseudo/func/map/MapFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@

@Slf4j
public class MapFunc extends AbstractPseudoFunc {
private final MapFuncConfig config;
private final MapFuncConfigService mapFuncConfigService = new MapFuncConfigService();
private final Mapper mapper;

public MapFunc(PseudoFuncConfig genericConfig) {
super(genericConfig.getFuncDecl());
this.config = mapFuncConfigService.resolve(genericConfig);
this.mapper = loadMapper();
this.mapper.setConfig(genericConfig.asMap());
}
Expand All @@ -31,35 +28,17 @@ public static Mapper loadMapper() {

@Override
public void init(PseudoFuncInput input) {
for (Object inputValue : input.getValues()) {
mapper.init(String.valueOf(inputValue));
}
mapper.init(input);
}

@Override
public PseudoFuncOutput apply(PseudoFuncInput input) {
PseudoFuncOutput output = new PseudoFuncOutput();

for (Object inputValue : input.getValues()) {
String plain = String.valueOf(inputValue);
final Object pseudonymized = mapper.map(plain);
output.add(pseudonymized);
}

return output;
return mapper.map(input);
}

@Override
public PseudoFuncOutput restore(PseudoFuncInput input) {
PseudoFuncOutput output = new PseudoFuncOutput();

for (Object inputValue : input.getValues()) {
String mapped = String.valueOf(inputValue);
final Object clear = mapper.restore(mapped);
output.add(clear);
}

return output;
return mapper.restore(input);
}

}
9 changes: 6 additions & 3 deletions src/main/java/no/ssb/dapla/dlp/pseudo/func/map/Mapper.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package no.ssb.dapla.dlp.pseudo.func.map;

import no.ssb.dapla.dlp.pseudo.func.PseudoFuncInput;
import no.ssb.dapla.dlp.pseudo.func.PseudoFuncOutput;

import java.util.Map;

public interface Mapper {

void init(Object data);
void init(PseudoFuncInput data);
void setConfig(Map<String, Object> config);
Object map(Object data);
PseudoFuncOutput map(PseudoFuncInput data);

Object restore(Object mapped);
PseudoFuncOutput restore(PseudoFuncInput mapped);

}

0 comments on commit 769ee1c

Please sign in to comment.