-4

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

enter image description here

Updated:

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

Message can be represented something like below

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

Group->represents the repeating group - I need help to define group and sub groups

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

The following are some points

  1. Are the "fields" attributes of a FixMessage?

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

  2. Repeating group they are Element repeating no of times

  3. Are the keys and values all Strings?

    -Consider them as string for now

  4. 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.

Output :

key2=value2 ;key3=value3;key4=value=4;key3=value3;key4=value=4;key2=valu‌​ e2;key3=value3;key‌​4=value4;key3=value‌​3;key4=value4

Explaination

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

Patrick Kostjens
  • 5,065
  • 6
  • 29
  • 46
coder25
  • 2,363
  • 12
  • 57
  • 104
  • @GhostCat hope it is bit clear now – coder25 Aug 18 '17 at 19:16
  • `trait Group{val ele = List[myType](); val grp = List[Group]()}` – jwvh Aug 18 '17 at 19:25
  • @jwvh thanks.so in order to achieve my goal to list...what possible approach should i follow.I extend all my classes with the trait group and add element to list – coder25 Aug 18 '17 at 19:27
  • 1
    By the way, this question is actually worse than the previous one. It is not clear what ultimate goal is. It's not clear how the java sample code solves the stated problem (or even its relationship to the problem). And the type names are so generic (Type1, Type2) that they don't provide any hint as to what the actual intent is. – Alvaro Carrasco Aug 18 '17 at 20:57
  • @AlvaroCarrasco hope it clarifies now – coder25 Aug 18 '17 at 22:19
  • @code25 Not quite. You're presenting a hierarchy with some labels, but it's still not clear what they are supposed to represent. Are the "fields" attributes of a FixMessage? are they attributes of a particular RepeatingGroup? is a RepeatingGroup a single entity or a list of Groups? Are the keys and values all Strings? does Field N (Field 1, Field 2, etc) represent different types? or do they represent names of different attributes that all share the same type? – Alvaro Carrasco Aug 18 '17 at 22:41
  • @code25 Have you examined the approach used by this library? https://github.com/mhotchen/fix-protocol (assuming that's the domain you're interested in) – Alvaro Carrasco Aug 18 '17 at 22:44
  • @Alvaro Carrasco yes i have but it doesnt encounter the group part.I am facing issue in grouping – coder25 Aug 18 '17 at 22:45
  • I am not sure what is not clear in question can anyone explain me so that i can improve it – coder25 Aug 19 '17 at 06:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/152308/discussion-between-coder25-and-alvaro-carrasco). – coder25 Aug 19 '17 at 06:09
  • @AlvaroCarrasco I have answered your queries .hope it clarifies the question now – coder25 Aug 19 '17 at 06:20
  • @Yuval Itzchakov can you please help me in this question – coder25 Aug 19 '17 at 17:14

1 Answers1

1

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"))
      )
    )
  ))
)

Of course some helper functions could be created to make a nicer DSL for it.

Alvaro Carrasco
  • 6,103
  • 16
  • 24
  • Yes u understood it clearly.but I need not pass List of list to achieve this .I may require method to add the group and sub groups to it.But not sure something like`trait Group{val ele = List[myType](); val grp = List[Group]()}` – coder25 Aug 19 '17 at 05:07
  • I have answered your queries .Please have a look – coder25 Aug 19 '17 at 06:22
  • `key2=value2` is a group which can be repeated n times ... followed by a subgroup. – coder25 Aug 19 '17 at 06:46