com.snowplowanalytics.snowplow.enrich.common.utils

MapTransformer

object MapTransformer

The problem we're trying to solve: converting maps to classes in Scala is not very easy to do in a functional way, and it gets even harder if you have a class with >22 fields (so can't use case classes).

For a discussion about this on Stack Overflow, see: http://stackoverflow.com/questions/4290955/instantiating-a-case-class-from-a-list-of-parameters

The idea is to use Java Reflection with a big ol' TransformMap:

("key in map" -> Tuple2(transformFunc, "field in class"), "another key" -> Tuple2(transformFunc, "field in class"), "a third key" -> Tuple2(transformFunc, "field in class"))

And then there would be an iteration through the source map which looks up the key in the TransformMap and then applies the transformFunc using Java Reflection, storing the answer in the class's specified field.

If I can get this working, then the next step is to generate a "results" map which contains the results of each transformFunc. Each transformFunc should either return a ValidationNel[String, Unit], or should be promoted (via an implicit) to the same.

Having this should allow me to do something like: resultsMap.foldLeft(Unit.success, |@|) to roll up any validation errors into one final ValidatioNel.

If I can get all that working, then the final step is to support transformFuncs which set multiple fields. To avoid the complexity spiralling, it would probably be simplest if any transformFunc that wanted to return multiple values returned a TupleN, and then we use the same TupleN for the target fields. Maybe there will be an implicit to convert "raw" target fields into Tuple1s.

Okay, let's start...

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. MapTransformer
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Type Members

  1. type Field = String

  2. type Key = String

  3. type SettersMap = Map[Key, Method]

  4. type SourceMap = Map[Key, Value]

  5. type TransformFunc = (Key, Value) ⇒ Validation[String, _]

  6. type TransformMap = Map[Key, Tuple2[(Key, Value) ⇒ Validation[String, _], _]]

  7. class TransformableClass[T] extends AnyRef

    A pimped object, now transformable by using the transform method.

  8. type Value = String

Value Members

  1. final def !=(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

    Definition Classes
    AnyRef → Any
  4. final def ==(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  5. final def ==(arg0: Any): Boolean

    Definition Classes
    Any
  6. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  7. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  8. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  9. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  10. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  11. def generate[T <: AnyRef](sourceMap: SourceMap, transformMap: TransformMap)(implicit m: Manifest[T]): Validated[T]

    A factory to generate a new object using a TransformMap.

    A factory to generate a new object using a TransformMap.

    sourceMap

    Contains the source data to apply to the obj

    transformMap

    Determines how the source data should be transformed before storing in the obj

    returns

    a ValidationNel containing either a Nel of error Strings, or the new object

  12. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  13. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  14. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  15. implicit def makeTransformable[T <: AnyRef](obj: T)(implicit m: Manifest[T]): TransformableClass[T]

    An implicit conversion to take any Object and make it Transformable.

    An implicit conversion to take any Object and make it Transformable.

    obj

    Any Object

    returns

    the new Transformable class, with manifest attached

  16. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  17. final def notify(): Unit

    Definition Classes
    AnyRef
  18. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  19. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  20. def toString(): String

    Definition Classes
    AnyRef → Any
  21. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  22. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  23. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped