Skip to content

Commit

Permalink
refactor transform parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
cwensel committed Jul 20, 2023
1 parent b35820f commit 02627a2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.JsonValue;
import io.clusterless.tessellate.pipeline.Transforms;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -41,19 +42,11 @@ public Transform() {

@JsonSetter
public void addTransform(String transform) {
// this needs to be more robust
if (transform.endsWith("->")) {
transforms.add(new DiscardOp(transform));
} else if (transform.contains("->")) {
transforms.add(new RenameOp(transform));
} else if (transform.contains("+>")) {
transforms.add(new CopyOp(transform));
} else if (transform.contains("=>")) {
transforms.add(new InsertOp(transform));
} else if (transform.contains("!>")) {
transforms.add(new EvalInsertOp(transform));
} else {
transforms.add(new CoerceOp(transform));
for (Transforms value : Transforms.values()) {
if (value.matches(transform)) {
transforms.add(value.transform(transform));
return;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,37 @@

package io.clusterless.tessellate.pipeline;

import io.clusterless.tessellate.model.*;

import java.util.function.Function;

public enum Transforms {
insert,
eval,
coerce,
copy,
rename,
discard;
insert("=>", "^.+[=]>.+$", InsertOp::new),
eval("!>", "^.+[!]>.+$", EvalInsertOp::new),
copy("+>", "^.+[+]>.+$", CopyOp::new),
rename("->", "^.+[-]>.+$", RenameOp::new),
discard("->", "^.+[-]>$", DiscardOp::new),
coerce("", "^.+$", CoerceOp::new);

private final String operator;
private final String match;
private final Function<String, TransformOp> transform;

Transforms(String operator, String match, Function<String, TransformOp> transform) {
this.operator = operator;
this.match = match;
this.transform = transform;
}

public String operator() {
return operator;
}

public boolean matches(String expression) {
return expression.matches(match);
}

public TransformOp transform(String expression) {
return transform.apply(expression);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ void name(@GivenTextResource("config/pipeline.json") String pipelineJson) throws

Transform transform = pipeline.transform();

assertEquals(5, transform.transformOps().size());
assertEquals(6, transform.transformOps().size());
assertInstanceOf(CoerceOp.class, transform.transformOps().get(0));
assertInstanceOf(RenameOp.class, transform.transformOps().get(1));
assertInstanceOf(CopyOp.class, transform.transformOps().get(2));
assertInstanceOf(DiscardOp.class, transform.transformOps().get(3));
assertInstanceOf(InsertOp.class, transform.transformOps().get(4));
assertInstanceOf(EvalInsertOp.class, transform.transformOps().get(5));
}
}
3 changes: 2 additions & 1 deletion tessellate-main/src/test/resources/config/pipeline.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"two->@two",
"three+>@three|DateTime|yyyyMMdd",
"four->",
"five=>_five"
"five=>_five",
"1689820455!>six|DateTime|yyyyMMdd"
]
}

0 comments on commit 02627a2

Please sign in to comment.