#Decoding a tuple pair list

1 messages · Page 1 of 1 (latest)

dawn locust
#

This is the best I got and it works wonders! The problem is that a decoder that decodes dynamic and I don't know how to use it with another decoder

fn decode_pair_properties() {
  use pair_list <- decode.then(decode.list(decode_pair()))
  pair_list |> dynamic.properties |> decode.success
}

fn decode_pair() {
  use a <- decode.field(0, decode.dynamic)
  use b <- decode.field(1, decode.dynamic)
  decode.success(#(a, b))
}
remote scroll
#

Sounds you want decode.string

fn decode_pair() {
  use a <- decode.field(0, decode.string)
  use b <- decode.field(1, decode.string)
  decode.success(#(a, b))
}
dawn locust
remote scroll
#

You will need a recursive custom type and decode.one_of, something like this

type Value {
  ValueString(String)
  ValuePairs(List(#(String, Value)))
}

fn decode_pair() {
  use a <- decode.field(0, decode.string)
  
  use b <- decode.field(1, decode.one_of(
    decode.string
    |> decode.map(ValueString)
    , [ 
      decode.list(decode_pair())
      |> decode.map(ValuePairs)
    ]))
    
  decode.success(#(a, b))
}
dawn locust
remote scroll
#

That above is the whole decoder ^, you can break that into smaller functions

#

Sorry, plus decode.list

let pairs= decode.run(dynamic_data, decode.list(decode_pair()))
dawn locust
#

Sorry, I should have made it clearer that I wanted to decode this into a struct like

type Mascot {
  Mascot(name: String, favorite_food: String, friends: List(Mascot))
}
remote scroll
#

What is the input data like? Probably objects in JSON?

dawn locust
#

unfortunately it isn't json, its in glua tables

remote scroll
dawn locust
#

me trying not to xy problem challenge (impossible)

dawn locust
#

Oh this post got bumped to the top oh joy