Home > Blockchain >  List null fields recursively with Shapeless
List null fields recursively with Shapeless

Time:03-24

I'm trying to list null fields recursively with Shapeless. But it doesn't seem to show all the fields:

https://scastie.scala-lang.org/PtLdSRC2Qfipu054Hzerrw

import shapeless._
import shapeless.labelled.FieldType

trait NullFields[T] {
  def apply(value: T, parent: String): Seq[String]
}

object NullFields extends NullFieldsLowPriority {
  implicit lazy val hNil = new NullFields[HNil] {
    override def apply(value: HNil, parent: String) = Seq.empty
  }

  implicit def hConsOption[K <: Symbol, V, TailFields <: HList](implicit
      witness: Witness.Aux[K],
      tailNullFields: NullFields[TailFields]
  ) = new NullFields[FieldType[K, Option[V]] :: TailFields] {
    override def apply(
        params: FieldType[K, Option[V]] :: TailFields,
        parent: String
    ) =
      (if (params.head.isEmpty) Seq(parent   witness.value.name)
       else Seq.empty)   
        tailNullFields(params.tail, parent)
  }

  implicit def hConsCaseClass[
      K <: Symbol,
      CC <: Product,
      Fields <: HList,
      TailFields <: HList
  ](implicit
      generic: LabelledGeneric.Aux[CC, Fields],
      nullFields: NullFields[Fields],
      witness: Witness.Aux[K],
      tailNullFields: NullFields[TailFields]
  ) = new NullFields[FieldType[K, CC] :: TailFields] {
    override def apply(params: FieldType[K, CC] :: TailFields, parent: String) =
      nullFields(generic.to(params.head), s"$parent${witness.value.name}.")   
        tailNullFields(params.tail, parent)
  }

  implicit def caseClass[CC, Fields <: HList](implicit
      generic: LabelledGeneric.Aux[CC, Fields],
      nullFields: NullFields[Fields]
  ) = new NullFields[CC] {
    override def apply(value: CC, parent: String) =
      nullFields(generic.to(value), parent)
  }
}

trait NullFieldsLowPriority {
  implicit def hConsDefault[K <: Symbol, V, TailFields <: HList](implicit
      tailNullFields: NullFields[TailFields]
  ) = new NullFields[FieldType[K, V] :: TailFields] {
    override def apply(params: FieldType[K, V] :: TailFields, parent: String) =
      tailNullFields(params.tail, parent)
  }
}

case class B(
    balba: Int = 0,
    profile_experience_desc: Option[String] = None,
    vmaf: Va
)
case class Va(
    tw_segment: Option[Double],
    i20_segment: Option[Double],
    ttq_segment: Option[Int]
)

println(implicitly[NullFields[B]].apply(B(vmaf = Va(None, None, None)), ""))

CodePudding user response:

I am not sure this is exactly what you want, but I hope you can tweak it to make it work for your need. I have considered the following as nulls:

  • All Nones
  • Any other types which are null
  • Recursive nulls or Nones found in case classes and/or Options

You can add further implicits to handle other cases:

import shapeless._
import shapeless.labelled.FieldType

trait NullFields[T] {
  def apply(value: T, parent: String): Seq[String]
}

object NullFields extends LowPrioriyImplicits {
  def apply[A](implicit nullFields: NullFields[A]): NullFields[A] = nullFields

  def instance[A](fn: (A, String) => Seq[String]): NullFields[A] = new NullFields[A] {
    override def apply(field: A, parent: String): Seq[String] = fn(field, parent)
  }

  implicit def optionNullFields[V](implicit nullFields: NullFields[V]): NullFields[Option[V]] = instance {
    (option: Option[V], parent: String) => 
      if (option.isEmpty) Seq(parent)
      else nullFields(option.head, parent)
  }

  implicit def genericNullFields[CC, Fields <: HList](implicit
      generic: LabelledGeneric.Aux[CC, Fields],
      nullFields: NullFields[Fields]
  ): NullFields[CC] = instance { (value: CC, parent: String) => nullFields(generic.to(value), parent) }

  implicit def hConsNullFields[K <: Symbol, Field, TailFields <: HList](implicit
      witness: Witness.Aux[K],
      nullFields: Lazy[NullFields[Field]],
      tailNullFields: Lazy[NullFields[TailFields]]
  ): NullFields[FieldType[K, Field] :: TailFields] = instance {
    (params: FieldType[K, Field] :: TailFields, parent: String) =>
      nullFields.value(params.head, (if (parent.isEmpty) "" else s"$parent.")   witness.value.name)   
        tailNullFields.value(params.tail, parent)
  }

  implicit val hNilNullFields: NullFields[HNil] = instance { (value: HNil, parent: String) => Seq.empty }
}

trait LowPrioriyImplicits {
  implicit def other[A]: NullFields[A] = NullFields.instance {
    (value: A, parent: String) => if (value == null) Seq(parent) else Seq.empty
  }
}

Scastie link for the same

  • Related