简体   繁体   English

编解码器的架构是数据流的一部分

[英]Schema of codec is part of the data stream

I'm currently evaluating if scodec is the right tool for my task. 我目前正在评估scodec是否适合我的任务。 I have to parse an InputStream (file or network) which is structured the following: 我必须解析一个结构如下的InputStream(文件或网络):

| Header -  FieldDesc1 - FieldDesc2 - ...        \
- FieldDescM - Record1 - Record2 - ... - RecordN |

This means the stream starts with some metadata, which descibes what will follow. 这意味着流以一些元数据开始,这些元数据决定了随后的内容。 Each element is separated by a delimiter ( - ) which identifies what type it is. 每个元素都由分隔符( - )分隔,该分隔符用于标识元素是什么类型。 The N field descriptions contain the information which structure and size each of the N records will have. N个字段描述包含N条记录中每条记录将具有的结构和大小的信息。

I was readily able to parse header as well as the sequence of fields, because I was able to formulate a codec which is known at compile time. 我很容易解析标头以及字段顺序,因为我能够制定在编译时已知的编解码器。 But I'm kind of puzzled how to build a codec at runtime due to the information from the field descriptions. 但是由于字段描述中的信息,我有点困惑如何在运行时构建编解码器。

Is that possible? 那可能吗? If yes, perhaps you can point me to an example? 如果是,也许您可​​以举出一个例子?

Here is a starting point if it's still relevant: 如果仍然有用,这是一个起点:

  • Use a DiscriminatorCodec (and a potentially related question ), or 使用DiscriminatorCodec (以及可能相关的问题 ),或
  • Use consume() on the codec decoding the type identifier (which I guess is a simple number), and pass the type to a function returning the correct wanted Codec. 在编解码器上使用consume()来解码类型标识符(我猜这是一个简单的数字),并将类型传递给返回正确的所需编解码器的函数。

For example using consume() , you can determine what codec to use at decode time: 例如,使用consume() ,您可以确定在解码时使用哪种编解码器:

def variableTypeC = 
  int8.consume(tid => selectCodec(tid))(selectTypeId(_))

I had to work on a similar problem and went for the consume() solution (as I had the feeling it provided me with a bit more flexbility and was only discovering scodec at the time). 我不得不处理一个类似的问题,然后使用了consume()解决方案(因为我感觉它为我提供了更多的灵活性,并且当时只发现了scodec )。

I'd be happy to build an example using DiscriminatorCodec if there is any need for it :). 如果有需要,我很乐意使用DiscriminatorCodec构建示例:)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM