Scala 创建一组元素 [英] Scala create group of elements
问题描述
我想在消息中创建一组元素,如下图所示
I want to create a group of elements in a message as in below image
更新:
case class Element(key:String;value:String)
消息可以像下面这样表示
Message can be represented something like below
case class Msg(field1:Element,field2:Group)
Group
->代表重复组 - 我需要帮助定义组和子团体
Group
->represents the repeating group - I need help to define group and sub
groups
元素定义了在组中重复的key=value组合
The element defines the key=value combination which is repeated in groups
以下几点
是 FixMessage 的字段"属性吗?
Are the "fields" attributes of a FixMessage?
-是的,它们是 Fix Message 的属性,每个字段表示为case class Element(key:String;value:String)
-Yes they are attributes of a Fix Message and each field is represented as
case class Element(key:String;value:String)
重复组它们是 Element
重复次数不
Repeating group
they are Element
repeating no of times
键和值都是字符串吗?
-暂时将它们视为字符串
-Consider them as string for now
字段 N(字段 1、字段 2 等)代表不同的类型?
Field N (Field 1, Field 2, etc) represent different types?
-是的,它们将其表示为不同的数据类型.但现在我们可以将它们视为相同的数据类型以使其简单.
-Yes they represent it as different data types.But for now we can take them as of same data type to make it simple.
输出:
key2=value2 ;key3=value3;key4=value=4;key3=value3;key4=value=4
;key2=valuee2;key3=value3;key 4=value4;key3=value 3;key4=value4
说明
组 key2=value2
重复了 2 次子组为key3=value3;key4=value=4;key3=value3;key4=value=4
,每组增益重复2次(key2=value2
) 分别
The group key2=value2
is repeating 2 times
The sub group is key3=value3;key4=value=4;key3=value3;key4=value=4
which is gain repeating 2 times in each group (key2=value2
) respectively
推荐答案
如果我正确理解域,这样的事情应该可行:
If I understand the domain correctly, something like this should work:
case class Message(entries: List[Entry])
case class Entry(name: String, value: Value)
trait Value
object Value {
case class Single (v: String) extends Value
case class Group (entries: List[List[Entry]]) extends Value
}
Message(
Entry("Key 1", Value.Single("Value 1")),
Entry("Key 2", Value.Group(
List(
List(
Entry("Key 3", Value.Single("Value 3")),
Entry("Key 4", Value.Single("Value 4"))
),
List(
Entry("Key 3", Value.Single("Value 5")),
Entry("Key 4", Value.Single("Value 6"))
)
)
))
)
当然可以创建一些辅助函数来为它制作更好的 DSL.
Of course some helper functions could be created to make a nicer DSL for it.
这篇关于Scala 创建一组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!