Scala create group of elements

前端 未结 1 638
忘掉有多难
忘掉有多难 2021-01-29 16:22

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

Updated:

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


        
相关标签:
1条回答
  • 2021-01-29 16:36

    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.

    0 讨论(0)
提交回复
热议问题