Object

com.snowplowanalytics.snowplow.enrich.common.utils

MapTransformer

Related Doc: package utils

Permalink

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
Visibility
  1. Public
  2. All

Type Members

  1. type Field = String

    Permalink
  2. type Key = String

    Permalink
  3. type SettersMap = Map[Key, Method]

    Permalink
  4. type SourceMap = Map[Key, Value]

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

    Permalink
  6. type TransformMap = Map[Key, Tuple2[TransformFunc, _]]

    Permalink
  7. class TransformableClass[T] extends AnyRef

    Permalink

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

  8. type Value = String

    Permalink

Value Members

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

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

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

    Permalink
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  5. def clone(): AnyRef

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

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

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

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

    Permalink

    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

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

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

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

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

    Permalink

    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

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

    Permalink
    Definition Classes
    AnyRef
  15. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  16. final def notifyAll(): Unit

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

    Permalink
    Definition Classes
    AnyRef
  18. def toString(): String

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

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

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

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped