Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 无法使用Shapeless将Scrooge生成的类转换为贴图_Scala_Thrift_Shapeless_Scrooge - Fatal编程技术网

Scala 无法使用Shapeless将Scrooge生成的类转换为贴图

Scala 无法使用Shapeless将Scrooge生成的类转换为贴图,scala,thrift,shapeless,scrooge,Scala,Thrift,Shapeless,Scrooge,我正在使用生成类。它们看起来像这样,下面是一个例子: object Flags extends ThriftStructCodec3[Flags] { private val NoPassthroughFields = immutable$Map.empty[Short, TFieldBlob] val Struct = new TStruct("Flags") val IsDangerousField = new TField("isDangerous", TType.BOOL,

我正在使用生成类。它们看起来像这样,下面是一个例子:

object Flags extends ThriftStructCodec3[Flags] {
  private val NoPassthroughFields = immutable$Map.empty[Short, TFieldBlob]
  val Struct = new TStruct("Flags")
  val IsDangerousField = new TField("isDangerous", TType.BOOL, 1)
  val IsDangerousFieldManifest = implicitly[Manifest[Boolean]]
  val IsWildField = new TField("isWild", TType.BOOL, 2)
  val IsWildFieldManifest = implicitly[Manifest[Boolean]]

  def apply(
    isDangerous: Option[Boolean] = None,
    isWild: Option[Boolean] = None
  ): Flags =
    new Immutable(
      isDangerous,
      isWild
    )

  def unapply(_item: Flags): Option[scala.Product2[Option[Boolean], Option[Boolean]]] = Some(_item)

  object Immutable extends ThriftStructCodec3[Flags] {
    override def encode(_item: Flags, _oproto: TProtocol) { _item.write(_oproto) }
    override def decode(_iprot: TProtocol): Flags = Flags.decode(_iprot)
  }

  /**
   * The default read-only implementation of Flags.  You typically should not need to
   * directly reference this class; instead, use the Flags.apply method to construct
   * new instances.
   */
  class Immutable(
    val isDangerous: Option[Boolean],
    val isWild: Option[Boolean],
    override val _passthroughFields: immutable$Map[Short, TFieldBlob]
  ) extends Flags {
    def this(
      isDangerous: Option[Boolean] = None,
      isWild: Option[Boolean] = None
    ) = this(
      isDangerous,
      isWild,
      Map.empty
    )
  }
}

trait Flags
  extends ThriftStruct
  with scala.Product2[Option[Boolean], Option[Boolean]]
  with java.io.Serializable
{
  import Flags._

  def isDangerous: Option[Boolean]
  def isWild: Option[Boolean]

  def _passthroughFields: immutable$Map[Short, TFieldBlob] = immutable$Map.empty

  def _1 = isDangerous
  def _2 = isWild

  override def productArity: Int = 2

  override def productElement(n: Int): Any = n match {
    case 0 => this.isDangerous
    case 1 => this.isWild
    case _ => throw new IndexOutOfBoundsException(n.toString)
  }

  override def productPrefix: String = "Flags"
}
其目的是使用Shapeless从这些类生成嵌套映射,对其执行一些操作,然后将映射转换回节约生成的类

使用和我成功地建立了一个可以很好地处理基本案例类的框架。在堆栈溢出响应中提供的示例之上,我还为选项[A]实现了隐式

现在,因为Scrooge没有生成正确的case类,所以我尝试使用不可变类(例如,
Flags.Immutable
)。这样做意味着我必须为所有节俭类定义隐式,以便将它们转换为
NameOfClass.Immutable
。到目前为止还不错

但是我有两件事情似乎没有做对(完整的实现可以在
ClassToMap.scala
文件中找到):

(1) 从旧货店类生成地图时,隐式选项似乎被忽略

implicit def hconsToMapRecOption[K <: Symbol, V, R <: HList, T <: HList]
    (implicit
     wit: Witness.Aux[K],
     gen: LabelledGeneric.Aux[V, R],
     tmrT: Lazy[ToMapRec[T]],
     tmrH: Lazy[ToMapRec[R]]
    ): ToMapRec[FieldType[K, Option[V]] :: T] = new ToMapRec[FieldType[K, Option[V]] :: T] {
      override def apply(l: FieldType[K, Option[V]] :: T): Map[String, Any] = {
        tmrT.value(l.tail) + (wit.value.name -> l.head.map(value => tmrH.value(gen.to(value))))
      }
    }

隐式def hconstoma预选项[K我现在不能仔细看一下,但是你真的得到了Scrooge生成的类型的
LabelledGeneric
实例吗?看看
hconsToMapRecOption
,现在我意识到我需要为Scrooge生成的类型定义
LabelledGeneric
。这就是为什么它适用于案例类。我说得对吗认为这就是问题所在?但我不确定如何解决它。我认为最有可能的是问题所在。看。我对一些必要的机制有了一个新的认识,但它不是最新的。谢谢你们,两位,这真的很有帮助。我现在添加了所需的含义,效果很好。剩下的唯一一件事就是增加对工会的支持。我将尝试一下呃,虽然目前我还不确定这会怎么样。
implicit def hconsToMapRecGoatData[K <: Symbol, R <: HList, T <: HList]
    (implicit
     wit: Witness.Aux[K],
     gen: LabelledGeneric.Aux[MyGoat, R],
     tmrT: Lazy[ToMapRec[T]],
     tmrH: Lazy[ToMapRec[R]]
    ): ToMapRec[FieldType[K, MyGoatData] :: T] = new ToMapRec[FieldType[K, MyGoatData] :: T] {
      override def apply(l: FieldType[K, MyGoatData] :: T): Map[String, Any] = {
        tmrT.value(l.tail) + (wit.value.name -> tmrH.value(gen.to(l.head.goat)))
      }
    }