I'm a Go newbie, so go easy on me. I'm coming from Rubyland and struggling to understand some of this new terrain.
Context: We have a service with two endpoints that hit multiple API's, massage the data and send it back as json. They're serving data on the same objects, but they do distinctly different things. The only common attribute is uid
.
They're sharing a model, which looks like this:
type Item struct {
Uid string `json:"uid"`
Val1 string `json:"param1,omitempty"`
Val2 []NestedVal `json:"param2,omitempty"`
Val5 int `json:"param5,omitempty"`
Val6 string `json:"param6,omitempty"`
Val7 string `json:"param7,omitempty"`
}
type NestedVal struct {
Val3 int `json:"val3,omitempty"`
Val4 string `json:"val4,omitempty"`
}
An example response from endpoint A
would be:
[
{
"uid": "123",
"val1": "foobar"
"val2": [
{"val3": 666, "val4": "qux"},
{"val3": 666, "val4": "qux"}
]
},
{
"uid": "456",
"val1": "foobar"
"val2": [
{"val3": 666, "val4": "qux"},
{"val3": 666, "val4": "qux"}
]
}
]
An example response from endpoint B
would be:
[
{
"uid": "123",
"val5": 999
"val6": "bar"
"val7": "baz"
},
{
"uid": "456",
"val5": 999
"val6": "bar"
"val7": "baz"
}
]
Right now these exist as stand-alone services because they serve slightly different needs, but now we need to offer them up as a combined dataset, something like:
[
{
"uid": "123",
"val1": "foobar"
"val2": [
{"val3": 666, "val4": "qux"},
{"val3": 666, "val4": "qux"}
],
"val5": 999
"val6": "bar"
"val7": "baz"
},
{
"id": "456",
"val1": "foobar"
"val2": [
{"val3": 666, "val4": "qux"},
{"val3": 666, "val4": "qux"}
],
"val5": 999
"val6": "bar"
"val7": "baz"
}
]
For what it's worth, this is how they're returned (basically the same for both endpoints):
func FetchA(uids []string) []Item {
var collection []Item
*... fetching, parsing, and appending to collection ...*
return collection
}
func HandlerA(response http.ResponseWriter, request *http.Request) {
*... pull uids out of qs ...*
collection := fetchA(uids)
responseData, _ := json.Marshal(collection)
*... write the response ...*
}
Bottom line: I have two data sets, both of type []Item
. I just want to combine them on their shared uid
keys. Obviously I'm approaching this as a Ruby dev, thinking about this problem as "I have two arrays of hashes and i want to merge them on a shared key" - I know thats the wrong frame of mind here, but I'm pretty stuck and I know the answer is right under my nose.
I've looked around and none of the answers I've seen have been particularly helpful. I checked out mergeo and go-merge but couldn't find a way around overwriting the merge-ee.
I think my real problem here is a lack of good understanding of Go data structures. This is how I'd combine the two in Ruby:
(responseA + responseB).group_by{|h| h[:uid]}.map{|k,v| v.reduce(:merge)}
EDIT: Here's a rough simulation on a Go playground https://play.golang.org/p/J0bJMjiM8DR