为什么解组这个 API 响应 return 一个意外的 EOF?
Why does unmarshalling this API response return an unexpected EOF?
问题
我正在使用协议缓冲区和 gRPC 在 Go 中创建一个微服务。它与第三方 API (Snooth) 交互,我正在尝试使用 proto
包将 JSON 响应解组到我创建的 protobuf 结构中。
解组 returns 一个 unexpected EOF
错误。
我试过的
- 使用
json.newDecoder
代替 json.unmarshal
jsonpb.Unmarshal
而不是 proto.Unmarshal
(returns 一个 bad value in StructValue for key
错误)
- 使用
io.LimitReader
限制读取的响应
我还读过一些关于在原型类型前加上尺寸标签之类的东西?但我不确定那是什么或者它是否相关。这是 Github.
上的回购协议
问题
导致此 unexpected EOF
错误的原因是什么?我该如何修复它,以便 API 响应成功解组到原型结构中?
旁注:我是 Go 的新手,也非常感谢以下代码中的任何 feedback/improvements。谢谢!
代码
Proto
message Response {
message Meta {
int32 results = 1;
int32 returned = 2;
string errmsg = 3;
int32 status = 4;
}
Meta meta = 1;
repeated google.protobuf.Struct wines = 2;
repeated google.protobuf.Struct actions = 3;
}
main.go
func fetchFromSnooth(e string, qs string, c chan response) {
defer func() {
if r := recover(); r != nil {
log.Printf("Error fetching from Snooth: %s", r)
errmsg := fmt.Sprint(r)
c <- response{nil, snoothApiError{errmsg}}
}
}()
v := url.Values{"akey": {os.Getenv("SNOOTH_API_KEY")}}
requestUrl := fmt.Sprintf("%s%s/?%s%s", snoothRoot, e, v.Encode(), qs)
log.Printf("Fetching: %s", requestUrl)
res, err := httpClient.Get(requestUrl)
if err != nil {
panic(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}
result := &pb.Response{}
if err := proto.Unmarshal(body, result); err != nil {
panic(err)
}
c <- response{result, snoothApiError{""}}
}
func (s *snoothApiService) searchWines(params *pb.Parameters_WineSearch) response {
c := make(chan response)
defer close(c)
v := url.Values{}
go fetchFromSnooth("wines", v.Encode(), c)
return <-c
}
func main() {
snooth := snoothApiService{}
resp := snooth.searchWines(nil)
fmt.Println(resp)
}
编辑
这是我尝试解组的 API 响应类型的示例:
{
"meta": {
"results": 1489442,
"returned": 5,
"errmsg": "",
"status": 1
},
"wines": [
{
"name": "Conway Deep Sea Chardonnay la Costa Wine Co",
"code": "conway-deep-sea-chardonnay-la-costa-wine-co-2008-1",
"region": "USA > California > Central Coast",
"winery": "Conway Family Wines",
"winery_id": "conway-family-wines",
"varietal": "Chardonnay",
"price": "21.99",
"vintage": "2008",
"type": "White Wine",
"link": "http:\/\/www.snooth.com\/wine\/conway-deep-sea-chardonnay-la-costa-wine-co-2008-1\/",
"tags": "",
"image": "https:\/\/ei.isnooth.com\/multimedia\/0\/2\/8\/image_787698_square.jpeg",
"snoothrank": 3,
"available": 0,
"num_merchants": 0,
"num_reviews": 10
},
{
"name": "Olmaia Cabernet di Toscana",
"code": "olmaia-cabernet-di-toscana",
"region": "Italy > Tuscany > Toscana Igt",
"winery": "Col D Orcia",
"winery_id": "col-d-orcia",
"varietal": "Cabernet Sauvignon",
"price": "0.00",
"vintage": "",
"type": "Red Wine",
"link": "http:\/\/www.snooth.com\/wine\/olmaia-cabernet-di-toscana\/",
"tags": "",
"image": "https:\/\/ei.isnooth.com\/multimedia\/d\/e\/e\/image_790198_square.jpeg",
"snoothrank": 3.5,
"available": 0,
"num_merchants": 0,
"num_reviews": 25
},
{
"name": "Dominio Dostares Prieto Picudo Vino de la Tierra de Castilla Y León Cumal",
"code": "dominio-dostares-prieto-picudo-vino-de-la-tierra-de-castilla-y-leon-cumal-2006",
"region": "Spain > Castilla y León > Vino de la Tierra de Castilla y León",
"winery": "Bischöfliches Priesterseminar Trier",
"winery_id": "bischofliches-priesterseminar-trier",
"varietal": "Prieto Picudo",
"price": "15.89",
"vintage": "2006",
"type": "Red Wine",
"link": "http:\/\/www.snooth.com\/wine\/dominio-dostares-prieto-picudo-vino-de-la-tierra-de-castilla-y-leon-cumal-2006\/",
"tags": "",
"image": "https:\/\/ei.isnooth.com\/multimedia\/d\/0\/4\/image_336595_square.jpeg",
"snoothrank": "n\/a",
"available": 0,
"num_merchants": 0,
"num_reviews": 1
},
{
"name": "Dominio Dostares Prieto Picudo Vino de la Tierra de Castilla Y León Cumal",
"code": "dominio-dostares-prieto-picudo-vino-de-la-tierra-de-castilla-y-leon-cumal-2005",
"region": "Spain > Castilla y León > Vino de la Tierra de Castilla y León",
"winery": "Bischöfliches Priesterseminar Trier",
"winery_id": "bischofliches-priesterseminar-trier",
"varietal": "Prieto Picudo",
"price": "38.99",
"vintage": "2005",
"type": "Red Wine",
"link": "http:\/\/www.snooth.com\/wine\/dominio-dostares-prieto-picudo-vino-de-la-tierra-de-castilla-y-leon-cumal-2005\/",
"tags": "",
"image": "https:\/\/ei.isnooth.com\/multimedia\/1\/d\/a\/image_336596_square.jpeg",
"snoothrank": "n\/a",
"available": 0,
"num_merchants": 0,
"num_reviews": 1
},
{
"name": "The Little Penguin Chardonnay Premier",
"code": "the-little-penguin-chardonnay-premier-2010",
"region": "South East Australia",
"winery": "The Little Penguin",
"winery_id": "the-little-penguin",
"varietal": "Chardonnay",
"price": "11.99",
"vintage": "2010",
"type": "White Wine",
"link": "http:\/\/www.snooth.com\/wine\/the-little-penguin-chardonnay-premier-2010\/",
"tags": "",
"image": "https:\/\/ei.isnooth.com\/multimedia\/2\/c\/4\/image_826282_square.jpeg",
"snoothrank": "n\/a",
"available": 0,
"num_merchants": 0,
"num_reviews": 7
}
]
}
它确实用 encoding/json
包解析:https://play.golang.org/p/IQzMm2tDI7w
protoc
生成的代码的 Unmarshal
解析 Protocol Buffers 编码的字节流,而不是 JSON!
--更新--
我现在已经根据需要使用 jsonpb.Unmarshal
方法对响应进行了解组。为此,尽管我必须首先使用常规 json 库解组和编组响应,以便在响应中绕过一些转义值(我收到了 'bad value in Struct' 错误:
resJson, err := ioutil.ReadAll(res.Body)
j := make(map[string]interface{})
if err := json.Unmarshal(resJson, &j); err != nil {
panic(err)
}
jbytes, err := json.Marshal(j)
result := &pb.Response{}
r := strings.NewReader(string(jbytes))
if err := jsonpb.Unmarshal(r, result); err != nil {
panic(err)
}
问题
我正在使用协议缓冲区和 gRPC 在 Go 中创建一个微服务。它与第三方 API (Snooth) 交互,我正在尝试使用 proto
包将 JSON 响应解组到我创建的 protobuf 结构中。
解组 returns 一个 unexpected EOF
错误。
我试过的
- 使用
json.newDecoder
代替json.unmarshal
jsonpb.Unmarshal
而不是proto.Unmarshal
(returns 一个bad value in StructValue for key
错误)- 使用
io.LimitReader
限制读取的响应
我还读过一些关于在原型类型前加上尺寸标签之类的东西?但我不确定那是什么或者它是否相关。这是 Github.
上的回购协议问题
导致此 unexpected EOF
错误的原因是什么?我该如何修复它,以便 API 响应成功解组到原型结构中?
旁注:我是 Go 的新手,也非常感谢以下代码中的任何 feedback/improvements。谢谢!
代码
Proto
message Response {
message Meta {
int32 results = 1;
int32 returned = 2;
string errmsg = 3;
int32 status = 4;
}
Meta meta = 1;
repeated google.protobuf.Struct wines = 2;
repeated google.protobuf.Struct actions = 3;
}
main.go
func fetchFromSnooth(e string, qs string, c chan response) {
defer func() {
if r := recover(); r != nil {
log.Printf("Error fetching from Snooth: %s", r)
errmsg := fmt.Sprint(r)
c <- response{nil, snoothApiError{errmsg}}
}
}()
v := url.Values{"akey": {os.Getenv("SNOOTH_API_KEY")}}
requestUrl := fmt.Sprintf("%s%s/?%s%s", snoothRoot, e, v.Encode(), qs)
log.Printf("Fetching: %s", requestUrl)
res, err := httpClient.Get(requestUrl)
if err != nil {
panic(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}
result := &pb.Response{}
if err := proto.Unmarshal(body, result); err != nil {
panic(err)
}
c <- response{result, snoothApiError{""}}
}
func (s *snoothApiService) searchWines(params *pb.Parameters_WineSearch) response {
c := make(chan response)
defer close(c)
v := url.Values{}
go fetchFromSnooth("wines", v.Encode(), c)
return <-c
}
func main() {
snooth := snoothApiService{}
resp := snooth.searchWines(nil)
fmt.Println(resp)
}
编辑
这是我尝试解组的 API 响应类型的示例:
{
"meta": {
"results": 1489442,
"returned": 5,
"errmsg": "",
"status": 1
},
"wines": [
{
"name": "Conway Deep Sea Chardonnay la Costa Wine Co",
"code": "conway-deep-sea-chardonnay-la-costa-wine-co-2008-1",
"region": "USA > California > Central Coast",
"winery": "Conway Family Wines",
"winery_id": "conway-family-wines",
"varietal": "Chardonnay",
"price": "21.99",
"vintage": "2008",
"type": "White Wine",
"link": "http:\/\/www.snooth.com\/wine\/conway-deep-sea-chardonnay-la-costa-wine-co-2008-1\/",
"tags": "",
"image": "https:\/\/ei.isnooth.com\/multimedia\/0\/2\/8\/image_787698_square.jpeg",
"snoothrank": 3,
"available": 0,
"num_merchants": 0,
"num_reviews": 10
},
{
"name": "Olmaia Cabernet di Toscana",
"code": "olmaia-cabernet-di-toscana",
"region": "Italy > Tuscany > Toscana Igt",
"winery": "Col D Orcia",
"winery_id": "col-d-orcia",
"varietal": "Cabernet Sauvignon",
"price": "0.00",
"vintage": "",
"type": "Red Wine",
"link": "http:\/\/www.snooth.com\/wine\/olmaia-cabernet-di-toscana\/",
"tags": "",
"image": "https:\/\/ei.isnooth.com\/multimedia\/d\/e\/e\/image_790198_square.jpeg",
"snoothrank": 3.5,
"available": 0,
"num_merchants": 0,
"num_reviews": 25
},
{
"name": "Dominio Dostares Prieto Picudo Vino de la Tierra de Castilla Y León Cumal",
"code": "dominio-dostares-prieto-picudo-vino-de-la-tierra-de-castilla-y-leon-cumal-2006",
"region": "Spain > Castilla y León > Vino de la Tierra de Castilla y León",
"winery": "Bischöfliches Priesterseminar Trier",
"winery_id": "bischofliches-priesterseminar-trier",
"varietal": "Prieto Picudo",
"price": "15.89",
"vintage": "2006",
"type": "Red Wine",
"link": "http:\/\/www.snooth.com\/wine\/dominio-dostares-prieto-picudo-vino-de-la-tierra-de-castilla-y-leon-cumal-2006\/",
"tags": "",
"image": "https:\/\/ei.isnooth.com\/multimedia\/d\/0\/4\/image_336595_square.jpeg",
"snoothrank": "n\/a",
"available": 0,
"num_merchants": 0,
"num_reviews": 1
},
{
"name": "Dominio Dostares Prieto Picudo Vino de la Tierra de Castilla Y León Cumal",
"code": "dominio-dostares-prieto-picudo-vino-de-la-tierra-de-castilla-y-leon-cumal-2005",
"region": "Spain > Castilla y León > Vino de la Tierra de Castilla y León",
"winery": "Bischöfliches Priesterseminar Trier",
"winery_id": "bischofliches-priesterseminar-trier",
"varietal": "Prieto Picudo",
"price": "38.99",
"vintage": "2005",
"type": "Red Wine",
"link": "http:\/\/www.snooth.com\/wine\/dominio-dostares-prieto-picudo-vino-de-la-tierra-de-castilla-y-leon-cumal-2005\/",
"tags": "",
"image": "https:\/\/ei.isnooth.com\/multimedia\/1\/d\/a\/image_336596_square.jpeg",
"snoothrank": "n\/a",
"available": 0,
"num_merchants": 0,
"num_reviews": 1
},
{
"name": "The Little Penguin Chardonnay Premier",
"code": "the-little-penguin-chardonnay-premier-2010",
"region": "South East Australia",
"winery": "The Little Penguin",
"winery_id": "the-little-penguin",
"varietal": "Chardonnay",
"price": "11.99",
"vintage": "2010",
"type": "White Wine",
"link": "http:\/\/www.snooth.com\/wine\/the-little-penguin-chardonnay-premier-2010\/",
"tags": "",
"image": "https:\/\/ei.isnooth.com\/multimedia\/2\/c\/4\/image_826282_square.jpeg",
"snoothrank": "n\/a",
"available": 0,
"num_merchants": 0,
"num_reviews": 7
}
]
}
它确实用 encoding/json
包解析:https://play.golang.org/p/IQzMm2tDI7w
protoc
生成的代码的 Unmarshal
解析 Protocol Buffers 编码的字节流,而不是 JSON!
--更新--
我现在已经根据需要使用 jsonpb.Unmarshal
方法对响应进行了解组。为此,尽管我必须首先使用常规 json 库解组和编组响应,以便在响应中绕过一些转义值(我收到了 'bad value in Struct' 错误:
resJson, err := ioutil.ReadAll(res.Body)
j := make(map[string]interface{})
if err := json.Unmarshal(resJson, &j); err != nil {
panic(err)
}
jbytes, err := json.Marshal(j)
result := &pb.Response{}
r := strings.NewReader(string(jbytes))
if err := jsonpb.Unmarshal(r, result); err != nil {
panic(err)
}