Skip to content

Commit

Permalink
- F better object loading
Browse files Browse the repository at this point in the history
  • Loading branch information
isidore committed Feb 12, 2024
1 parent 8c65e74 commit a9560b2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.spun.util.Tuple;
import org.approvaltests.core.Options;
import org.lambda.functions.Function1;
import org.lambda.functions.Function2;
import org.lambda.query.Query;
import org.lambda.query.Queryable;

Expand Down Expand Up @@ -32,8 +33,28 @@ public static <T> ParseInput<T> createFromParts(String expected, Function1<Query
return new Tuple<>(s, transformer.call(temp));
});
}

public static <IN1, IN2, T> ParseInput<T> createFromParts(String expected, Function2<IN1, IN2, T> transformer, Class<IN1> in1, Class<IN2> in2)
{
Function1<String, IN1> t1 = getTransformerForClass(in1);
Function1<String, IN2> t2 = getTransformerForClass(in2);

return new ParseInput<T>(expected, s -> {
var temp = Queryable.as(s.split(",")).select(String::trim);
IN1 v1 = t1.call(temp.get(0));
IN2 v2 = t2.call(temp.get(1));
T out = transformer.call(v1, v2);
return new Tuple<>(s, out);
});
}

public static <T> ParseInput<T> create(String expected, Class<T> tranformTo)
{
Function1<String, T> transformer1 = getTransformerForClass(tranformTo);
return ParseInput.create(expected, transformer1);
}

private static <T> Function1<String, T> getTransformerForClass(Class<T> tranformTo) {
var transformers = new HashMap<Class<?>, Function1<String, Object>>()
{
{
Expand All @@ -46,8 +67,10 @@ public static <T> ParseInput<T> create(String expected, Class<T> tranformTo)
put(Short.class, s -> Short.parseShort(s));
}
};
return ParseInput.create(expected, (Function1<String, T>) transformers.get(tranformTo));
Function1<String, T> transformer1 = (Function1<String, T>) transformers.get(tranformTo);
return transformer1;
}

public Queryable<Tuple<String, T>> parse(String expected)
{
return Queryable.as(expected.lines()).select(l -> l.split("->")[0].trim()).select(l -> transformer.call(l));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,14 @@ void testPeople()
Creator<Person> createPerson = (a) -> new Person(a.get(0), Integer.parseInt(a.get(1)));
ParseInput.createFromParts(expected, createPerson).verifyAll(s -> s.getAgeLabel());
}
@Test
void testPeopleEasyLoad()
{
var expected = """
Llewellyn, 25 -> adult
Oliver, 15 -> teenager
""";
ParseInput.createFromParts(expected, (n,a) -> new Person(n,a), String.class, Integer.class)
.verifyAll(s -> s.getAgeLabel());
}
}

0 comments on commit a9560b2

Please sign in to comment.