Skip to content
This repository was archived by the owner on Sep 16, 2025. It is now read-only.

Transformations

shaheen-s edited this page Dec 21, 2020 · 4 revisions

Transformations can be current achieved using the HasTransformation call during the definition phase.

HasTransformation

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"
}

Variations

There are three overloads suitable for different purposes:

HasTransformation(Func<T,T> transformation, object defaultValue)

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);
  }

HasTransformation(Func<T,T> transformation)

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);
      
  }

HasTransformation<TCast>(Func<object,TCast> transformation)

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|");
  }

Transformation Master List

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|");
  }

Internal Type default transformations

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|");
  }

Clone this wiki locally