Scala create a group of elements

I want to create a group of elements in a message as shown below.

enter image description here

Updated:

case class Element(key:String;value:String) 

The message can be presented approximately as

  case class Msg(field1:Element,field2:Group) 

Group β†’ is a repeating group - I need help identifying a group and sub group

The element defines the combination key = value , which is repeated in groups

Below are some points

  • Are field attributes of FixMessage?

    -Yes, they are attributes of the Fix message, and each field is represented as a case class Element(key:String;value:String)

  • Repeating group they Element repeat no of times

  • Are keys and values ​​for all strings?

    -See them as a string at the moment

  • Field N (field 1, field 2, etc.) represents different types?

    Yes, they represent it as different types of data. But now we can take them as the same data type to make it simple.

Exit:

key2 = value2 ; key3=value3;key4=value=4;key3=value3;key4=value=4 ; key2 = e2 value; key3=value3;keyβ€Œβ€‹4=value4;key3=valueβ€Œβ€‹3;key4=value4

Explaination

The group key2=value2 repeated 2 times. The subgroup key3=value3;key4=value=4;key3=value3;key4=value=4 , which is a repeat of 2 times in each group ( key2=value2 ), respectively

-4
source share
1 answer

If I understood 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")) ) ) )) ) 

Of course, some helper functions can be created to make them more enjoyable DSL.

+1
source

Source: https://habr.com/ru/post/1013913/


All Articles