doujilou3903 2018-03-22 22:35
浏览 232
已采纳

使用Golang将两片相似的数据合并到JSON中

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

  • 写回答

1条回答 默认 最新

  • dongqiu7365 2018-03-22 23:11
    关注

    I think you are on the right track with "I have two arrays of hashes and i want to merge them on a shared key"

    However, in Go you are going to be responsible for doing the merging yourself as their is no notion of a group_by type function / method.

    func merge(As, Bs []Item) []Item {
        if len(As) == 0 {
            return Bs
        }
        bMap := make(map[string]Item)
        for _, b := range Bs {
            bMap[b.Uid] = b
        }
        merged := make([]Item, len(As))
        for i, a := range As {
            if b, ok := bMap[a.Uid]; ok {
                a.Val5 = b.Val5    // this and the next two lines could be done
                a.Val6 = b.Val6    // using reflection, but if you know the fields
                a.Val7 = b.Val7    // then this would be my preferred way.
            }
            merged[i] = a
        }
        return merged
    
    }
    

    Here is a playground of the results

    Note: This wouldn't account for an empty slice of As as you would get nothing in the merge. Since I don't know the specifics of your application I can't say whether that is something that can happen, but if it can you will need to account for it.

    For what it's worth, I think this is approximately the approach Verran was suggesting:

    https://play.golang.org/p/3EpWlFdB8c6

    Basically, let the results be returned on a channel and have them merged as you get both results.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境