2

I'm using the go-worker to process resque jobs. A job has a payload which has a nested JSON structure like this:

[{
  "key-a":"val-a",
  "key-b":"val-b",
  "files":[{
    "key-a": [
      {"a":"b","c": "d"},
      {"e":"f","g": "h"}
    ],
    "key-b": [
      {"a":"b","c": "d"},
      {"e":"f","g": "h"}
    ]
  }]
}]

Now go-worker gives me args ...interface{} which represents that JSON payload, not the actual JSON text. Is there an idiomatic way to convert that(args) to the correct types (could use another package to do this.) Using type assertions manually seems a bit tedious for such a thing.

Akshay Rawat
  • 4,714
  • 5
  • 40
  • 65

1 Answers1

1

If it's really giving you the actual go objects (i.e. a bunch of map[string]interface{} and not the json string itself) then there probably isn't much you can do besides a bunch of type assertions.

You could re-marshall it to json then parse it again into the correct structs, but that's a bit of a hack (and I have no idea if it would be performant or not).

Evan
  • 6,369
  • 1
  • 29
  • 30