2

Hi Podio people (and maybe more specifically Andreas),

I'm trying to dig deeper into the Golang API library but bumping into my rookie Golang skills.

After doing a client.getItems(...) call I wish to loop over the fields inside of the items and only grab relevant portions. The end goal is that I can create a very much simplified json object like this

{
    1000: "John", // key = app field id, value = text
    5490: [{item_id: 4031294, app_id: 94392}],  // relations
    5163: [1,2,5] // categories
}

However I cannot seem to get a hold of the item.Fields nested Values struct {}. I tried using reflect but without any luck.

Could someone help me complete this code please?

for _, field := range item.Fields {
  switch field.PartialField.Type {
  case "text":
    simpleValue := field.Values.Value // not working as I can't access Value in struct {}
  }
}

Greetings, PJ

pjmuller
  • 79
  • 8
  • 1
    Can you provide us with a sample of the input structure you wish to grap portions out of? – TehSphinX Jul 13 '16 at 14:24
  • Hi TehSphinX. Here is a sample of the input structure: https://github.com/andreas/podio-go/blob/master/item.go#L39 and https://github.com/andreas/podio-go/blob/master/item.go#L98 – pjmuller Jul 14 '16 at 05:49

1 Answers1

2

Try a type assertion

myTexts := field.Values.([]TextValue)

You can also check for a valid assertion so your program doesn't panic

 myTexts, assertionSucceeded := field.Values.([]TextValue)
Benjamin Kadish
  • 1,472
  • 11
  • 20
  • Hi Benjamin, thanks for your quick response. With your first snippet I get `error: interface conversion: interface is []interface {}, not []podio.TextValue` and with the second it fails silently and `myTexts` does not have any values – pjmuller Jul 13 '16 at 05:22
  • Btw, `field.Values.([]interface{})` does work but I can't figure out how to then get the `values` out of the map. – pjmuller Jul 13 '16 at 05:31
  • Try using a for loop on the result of the assertion – Benjamin Kadish Jul 13 '16 at 14:38
  • I tried `for _, myText := range myTexts {` but without success. What other syntax do you suggest? – pjmuller Jul 13 '16 at 17:19
  • What map are you talking about? Or do you mean "slice"? Maybe you can also create a new question, since we do not have any context here. What is `myTexts`, for example. – TehSphinX Jul 14 '16 at 10:05