简体   繁体   English

使用它限制大小字节后如何忽略编解码器

[英]How to ignore Codec after using it to limit size bytes

I would create a model for the KMIP protocols that works with TTLV encoding ( Tag, Type, Length, Value ) 我将为使用TTLV编码( 标记,类型,长度,值 )的KMIP协议创建一个模型。

The ttlv function is "high level" and accepts the tag, the type and the codec of the value. ttlv函数是“高级”的,它接受值的标签,类型和编解码器。

 def ttlv[A<:HList](tag:ByteVector, itype:ByteVector, value: Codec[A]) =
   constant(tag) :: constant(itype) :: 
   (uint32 >>:~ {
     case length => limitedSizeBytes(length, value)
   })

The result is a Codec[Unit :: Unit :: Long :: A] . 结果是Codec[Unit :: Unit :: Long :: A] However, I would have a Codec[Unit :: Unit :: Unit :: A] (or Codec[A] ) to cast the codec to a case class with only the value A . 但是,我会有一个Codec[Unit :: Unit :: Unit :: A] (或Codec[A] )将编解码器转换为仅包含值Acase class How to ignore the uint32 after used by limitedSizeBytes ? limitedSizeBytes使用后如何忽略uint32 Otherwise I am interested for comments for better approaches. 否则,我有兴趣寻求更好的方法的意见。


Here are case class examples: 以下是case class示例:

case class RequestHeader(protocol:Int)
case class RequestPayload(payload:CompromiseDate, name:CertificateName)
case class RequestMessage(header:RequestHeader, payload: RequestPayload)
case class CompromiseDate(value:Int)
case class CertificateName(value:String)

More high level functions like ttlvStructure 更多高级功能,如ttlvStructure

def ttlvStructure[A<:HList](tag:ByteVector, struct:Codec[A]) =
  ttlv(tag, hex"01", struct)
def ttlvTextString(tag:ByteVector) =
  ttlv(tag, hex"07", utf8.hlist)
def ttlvInt(tag:ByteVector) =
  ttlv(tag, hex"02", int32.hlist)

And the final codec: 最后的编解码器:

implicit val certificateNameCodec =
  ttlvTextString(hex"420020").as[CertificateName]

implicit val compromiseDateCodec =
  ttlvInt(hex"420021").as[CompromiseDate]

implicit val requestPayloadCodec =
  ttlvStructure(hex"420003", Codec[CompromiseDate] :: Codec[CertificateName]).as[RequestPayload]

implicit val requestHeaderCodec =
  ttlvInt(hex"420002").as[RequestHeader]

implicit val requestMessageCodec =
  ttlvStructure(hex"420001", Codec[RequestHeader] :: Codec[RequestPayload]).as[RequestMessage]

Example of object to encode: 编码对象的示例:

val m = RequestMessage(
      RequestHeader(14),
      RequestPayload(
        CompromiseDate(8),
        CertificateName("certificate name")
      )
    )

Solution : 解决方案

variableSizeBytesLong is here to do what I want: variableSizeBytesLong在这里是我想要做的:

def ttlv[A<:HList](tag:ByteVector, itype:ByteVector, value: Codec[A]): Codec[A] =
    (constant(tag) :~>: constant(itype) :~>: variableSizeBytesLong(uint32, value))

Try 尝试

val defaultLength: Long = ???

def ttlv[A<:HList](tag:ByteVector, itype:ByteVector, value: Codec[A]): Codec[A] =
  constant(tag) :~>: constant(itype) :~>:
    (uint32 >>:~ (length => limitedSizeBytes(length, value))).xmap[A](_.tail, defaultLength :: _)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Spark 中以字节为单位计算 dataframe 的大小? - How to calculate the size of dataframe in bytes in Spark? 如何在Play配置(HOCON)中读取字节格式的大小? - How to read size in bytes syntax in Play configuration (HOCON)? scala-如何获取固定大小为8个字节的Long值的byteArray - scala - how to get byteArray of Long value in fixed size of 8 bytes scodec忽略hlist和case类之间的编解码器转换中的最后一个值 - scodec ignore last value in codec conversion between hlist and case class 使用 Spark Structured Streaming 时限制 kafka 批量大小 - Limit kafka batch size when using Spark Structured Streaming scala邮箱大小限制 - scala mailbox size limit Scala AWS 大小限制 - Scala AWS size limit 如何动态提供N个编解码器来处理不包含size字节的二进制字段记录的字段作为VectorCodec - How to dynamically provide N codecs to process fields as a VectorCodec for a record of binary fields that do not contain size bytes 如何在Scala中使用actor时限制并发? - How to limit concurrency when using actors in Scala? 如何为Spark中的SaveAsSequenceFile方法提供编解码器? - How to provide a codec to the SaveAsSequenceFile method in Spark?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM