Go Pact 消费者测试中的 UUID 字段
UUID field within Go Pact consumer test
我目前正在考虑将 Pact 测试添加到我的 Go 代码中,但我对如何处理 UUID 的字段类型感到困惑。
我有以下结构,我用它来反序列化从 API 到
的响应
import (
"github.com/google/uuid"
)
type Foo struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
}
现在,当我尝试编写消费者测试时,它看起来像这样
pact.
AddInteraction().
Given("A result exists").
UponReceiving("A request to get all results").
WithRequest(dsl.Request{
Method: "get",
Path: dsl.String("/v1"),
}).
WillRespondWith(dsl.Response{
Status: 200,
Headers: dsl.MapMatcher{"Content-Type": dsl.String("application/json")},
Body: dsl.Match(&Foo{}),
})
现在的问题是模拟响应如下所示,它试图在“id”字段中放置一个字节数组,我假设在幕后就是 google/uuid 图书馆将其存储为。
{
"id": [
1
],
"name": "string",
"description": "string"
}
有人遇到过这种情况吗?什么是最好的前进方式 - 我能看到的唯一解决方案是将我的模型更改为字符串,然后在我的代码中手动转换为 UUID。
uuid.UUID 的内部表示是 type UUID [16]byte
但 json UUID 的表示是字符串
var u uuid.UUID
u, _ = uuid.Parse("f47ac10b-58cc-0372-8567-0e02b2c3d479")
foo1 := Foo{u, "n", "d"}
res, _ := json.Marshal(foo1)
fmt.Println(string(res))
{
"id": "f47ac10b-58cc-0372-8567-0e02b2c3d479",
"name": "n",
"description": "d"
}
然后加载编组的[]byte
var foo Foo
json.Unmarshal(res, &foo)
您目前不能以这种方式使用 Match
函数,因为它会递归非原始结构,尽管应该可以使用结构标签覆盖此行为。你能提出一个功能请求吗?
最简单的做法就是不使用Match
的方式,按照通常的方式手动表达合约细节。
我目前正在考虑将 Pact 测试添加到我的 Go 代码中,但我对如何处理 UUID 的字段类型感到困惑。 我有以下结构,我用它来反序列化从 API 到
的响应import (
"github.com/google/uuid"
)
type Foo struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
}
现在,当我尝试编写消费者测试时,它看起来像这样
pact.
AddInteraction().
Given("A result exists").
UponReceiving("A request to get all results").
WithRequest(dsl.Request{
Method: "get",
Path: dsl.String("/v1"),
}).
WillRespondWith(dsl.Response{
Status: 200,
Headers: dsl.MapMatcher{"Content-Type": dsl.String("application/json")},
Body: dsl.Match(&Foo{}),
})
现在的问题是模拟响应如下所示,它试图在“id”字段中放置一个字节数组,我假设在幕后就是 google/uuid 图书馆将其存储为。
{
"id": [
1
],
"name": "string",
"description": "string"
}
有人遇到过这种情况吗?什么是最好的前进方式 - 我能看到的唯一解决方案是将我的模型更改为字符串,然后在我的代码中手动转换为 UUID。
uuid.UUID 的内部表示是 type UUID [16]byte
但 json UUID 的表示是字符串
var u uuid.UUID
u, _ = uuid.Parse("f47ac10b-58cc-0372-8567-0e02b2c3d479")
foo1 := Foo{u, "n", "d"}
res, _ := json.Marshal(foo1)
fmt.Println(string(res))
{
"id": "f47ac10b-58cc-0372-8567-0e02b2c3d479",
"name": "n",
"description": "d"
}
然后加载编组的[]byte
var foo Foo
json.Unmarshal(res, &foo)
您目前不能以这种方式使用 Match
函数,因为它会递归非原始结构,尽管应该可以使用结构标签覆盖此行为。你能提出一个功能请求吗?
最简单的做法就是不使用Match
的方式,按照通常的方式手动表达合约细节。