-
Notifications
You must be signed in to change notification settings - Fork 3
Transformations
Transformations can be current achieved using the
HasTransformation call during the definition phase.
HasTransformation takes in a lambda and runs it on the field after population.
class SomeClass {
public int x {get; set;}
public string y {get; set;}
}
public Analyzer : AnalyticalObject<SomeClass> {
Property(s => s.x).HasTransformation(p => {
return p * 2;
});
Property(s => s.y).HasTransformation(p => {
return p + 'b';
});
}If the initial result from population looked like this:
{
["x"] = 21,
["y"] = "Bo"
}It would be transformed into:
{
["x"] = 42,
["y"] = "Bob"
}There are three overloads suitable for different purposes:
This runs the transformation if the incoming value is not null, if it is, it returns the default value provided.
public void TransformWithDefaultValueTest()
{
var analyzer = new AnalyticalObject<ValueTypeTest>();
analyzer.Property(p => p.x).HasTransformation(o => o + 1, 45);
var popRs2 = new PopulationResult(new Dictionary<string, object>()
{
["valuetypetest_x"] = null
}, analyzer.TypeMap);
Assert.AreEqual((int)popRs2["valuetypetest_x"], 45);
}This runs the transformation if the incoming value is not null, if it is, it throws an exception.
public void TransformValueTypeValidTest()
{
var analyzer = new AnalyticalObject<ValueTypeTest>();
analyzer.Property(p => p.x).HasTransformation(o => o + 1);
var popRs2 = new PopulationResult(new Dictionary<string, object>()
{
["valuetypetest_x"] = 2
}, analyzer.TypeMap);
Assert.AreEqual((int)popRs2["valuetypetest_x"], 3);
}This does nothing but simply run the function provided and allows you to return an object of a different type than the incoming value type.
public void TransformationCastTest()
{
var analyzer = new AnalyticalObject<ValueTypeTest>();
analyzer.Property(p => p.x).HasTransformation<string>(o => $"|{o}|");
var popRs = new PopulationResult(new Dictionary<string, object>()
{
["valuetypetest_x"] = 1
}, analyzer.TypeMap);
Assert.AreEqual(popRs["valuetypetest_x"], "|1|");
}If you have a common transformation, you can add it to a master list, and use it later on.
You can add all the transformation types you can add using HasTransformation.
public void TransformationFromMasterList()
{
FluentOlapConfiguration.TransformationsMasterList
.AddTransformation<int, string>("Surround", o => $"|{o}|");
var analyzer = new AnalyticalObject<ValueTypeTest>();
analyzer.Property(p => p.x).HasTransformation("Surround");
var popRs = new PopulationResult(new Dictionary<string, object>()
{
["valuetypetest_x"] = 1
}, analyzer.TypeMap);
Assert.AreEqual(popRs["valuetypetest_x"], "|1|");
}This master list will also accept an InternalType instance as a key so that there is a
default transformation for a type.
public void TransformationFromMasterList()
{
FluentOlapConfiguration.TransformationsMasterList
.AddTransformation<int, string>(InternalType.INTEGER, o => $"|{o}|");
var analyzer = new AnalyticalObject<ValueTypeTest>();
analyzer.Property(p => p.x);
var popRs = new PopulationResult(new Dictionary<string, object>()
{
["valuetypetest_x"] = 1
}, analyzer.TypeMap);
Assert.AreEqual(popRs["valuetypetest_x"], "|1|");
}FluentOlap is still growing and will continue to become more stable over time.