案例
例如,有个 GET 接口,可以批量获取用户信息👇
- > curl 'http://localhost:8080/user/1,2,3'
- [
- {
- "user_id":1,
- "other_suff":...
- },
- {
- "user_id":2,
- "other_suff":...
- },
- {
- "user_id":3,
- "other_suff":...
- }
- ]
同时,我们要将用户信息和他们的某些订单信息放在一起,组装成为👇的接口,满足其他业务需求。
- [
- {
- "user_info":{
- "user_id":1,
- "other_suff":...
- },
- "order_info":{
- "order_id":1,
- "user_id":1,
- "other_suff":...
- }
- },
- {
- "user_info":{
- "user_id":2,
- "other_suff":...
- },
- "order_info":{
- "order_id":2,
- "user_id":2,
- "other_suff":...
- }
- },
- {
- "user_info":{
- "user_id":3,
- "other_suff":...
- },
- "order_info":{
- "order_id":3,
- "user_id":3,
- "other_suff":...
- }
- }
- ]
分析
解决这个问题很简单:把user信息和order信息的json用工具解析得到结构体,然后调用他们的接口得到数据,根据id关联和拼装,最后返回。
这样的做法存在的一个问题是,代码解析了user和order的完整结构。如果user接口返回的用户信息增加了字段,我们这里的结构体要同步更新,否则我们给出的数据就是不完整的。(这可能是很痛苦的,你要求别的团队加字段,得排期...)
其实我们作为数据的“中间商”,只关心user接口json里的 user_id ,我们使用这个字段关联order数据。对于user信息里的 other_suff 或者其他数据,我们并不关心,只要保证完整传出去就好了。
根据 https://golang.org/pkg/encoding/json/#Unmarshal ,可以知道直接丢一个 map[string]interface{} 给 json.Unmarshal 也可以正常解析的,于是我们可以写出比较通用的透传代码。
- type Content []map[string]interface{}
-
- func (c Content) GetByFieldName(name string, defaultVal interface{}) infterface{} {
- for _, item := range c {
- val, ok := item[name]
- if !ok {
- continue
- }
- if val == nil {
- return defaultVal
- }
- return val
- }
- return defaultVal
- }
-
- func getUserContentByIDs(ids []int) Content {
- ...
- var c Content
- err := json.Unmarshal(jsonData, &c)
- ...
- return c
- }
-
- func getOrderContentByUserIDs(ids []int) Content {.../*同上*/}
-
- func Handler(userIDs []int) []Combine {
-
- users := getUserContentByIDs(userIDs)
- orders := getOrderContentByUserIDs(userIDs)
-
- // 这里假设用户和订单是一对一的关系
- ret := make([]Combine, 0, len(users))
- for _, u := range users {
- for _, o := range orders {
- userID := u.GetByFieldName("user_id", 0)
- orderUserID := o.GetByFieldName("user_id", 0)
- if userID != 0 && userID == orderUserID {
- ret = append(ret, Combine{
- UserInfo: u,
- OrderInfo: o,
- })
- break
- }
- }
- }
- return ret
- }
-
P.S. 在上面的例子中,每次查询Content都要遍历数组。如果数据量大或者查询频繁,可以在初始化Content的时候,根据item的唯一标标识,再给Content根据封装一个map,提高查询效率。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。