How do I handle a custom field that is an object instead of a simple string?
They're constantly adding custom fields to JIRA where I work. One of the latest requires a full object rather than a simple string, as the data. It represents a drop-down field with fixed selections.
Here's what the JSON looks like:
"customfield_17100": {
"disabled": false,
"id": "15700",
"self": "https://someserver.com/rest/api/2/customFieldOption/15700",
"value": "Some text value"
},
Setting a regular customfield doesn't work, since it only allows strings for the values, not objects. If the JSON is pass as a string, it doesn't count as a object.
Ah, ok. Looks like I'm out of luck for now, as this is something that is planned for v2 in #477
You can use the methods on IssueFields.Unknowns to extract values from custom fields, no matter how nested. See the documentation at https://pkg.go.dev/github.com/trivago/tgo/tcontainer#MarshalMap.Value for how to format the key.
Alternatively, you could use https://github.com/mitchellh/mapstructure to decode IssueFields.Unknowns into a struct that has the custom fields you need. This is very convenient and Go-like.
Or you can process IssueFields.Unknowns "manually" to extract what you need but that's tedious.
I need to go the other direction... I need to encode the value to send to JIRA. I'll look into modifying IssueFields.Unknowns manually.
It would still be easier than trying to create a giant curl command... :-\
Then you should still be able to use IssueFields.Unknowns.Set to set your custom fields to send to Jira https://pkg.go.dev/github.com/trivago/tgo/tcontainer#MarshalMap.Set
Or you can use https://github.com/mitchellh/mapstructure to "decode" your custom field structs into IssueFields.Unknowns, something like this: https://go.dev/play/p/otekLAkstZK
I tried this, but apparently go-jira is trying to marshall it again... or something
customFields := tcontainer.MarshalMap{}
...
data, err := json.Marshal(option)
if err != nil {
return fmt.Errorf(err.Error())
}
customFields["customfield_17100"] = data
...
if len(customFields) > 0 {
i.Fields.Unknowns = customFields
}
Why not just (untested):
customFields := tcontainer.NewMarshalMap()
customFields["customfield_17100"] = option
i.Fields.Unknowns = customFields
... you know how sometimes you're just too close, and need to take a step back, just to realize the obvious?
You are correct - that worked. Thank you. :-)
Thanks for the good collaboration here :) I will re-open this, because this seem to be a good case for a documentation entry.