如何通过 grpcurl 发送时间戳?

Saq*_*Ali 5 go protocol-buffers grpcurl

我正在使用 GRPC/proto-buffers 在 GoLang 中编写我的第一个 API 端点。我对 Go 还很陌生。以下是我为测试用例编写的文件

package my_package

import (
    "context"
    "testing"

    "github.com/stretchr/testify/require"

    "google.golang.org/protobuf/types/known/structpb"
    "github.com/MyTeam/myproject/cmd/eventstream/setup"
    v1handler "github.com/MyTeam/myproject/internal/handlers/myproject/v1"
    v1interface "github.com/MyTeam/myproject/proto/.gen/go/myteam/myproject/v1"
)

func TestEndpoint(t *testing.T) {
    conf := &setup.Config{}

    // Initialize our API handlers
    myhandler := v1handler.New(&v1handler.Config{})

    t.Run("Success", func(t *testing.T) {
        res, err := myhandler.Endpoint(context.Background(), &v1interface.EndpointRequest{
            A: "S",
            B: &structpb.Struct{
                Fields: map[string]*structpb.Value{
                    "T": &structpb.Value{
                        Kind: &structpb.Value_StringValue{
                            StringValue: "U",
                        },
                    },
                    "V": &structpb.Value{
                        Kind: &structpb.Value_StringValue{
                            StringValue: "W",
                        },
                    },
                },
            },
            C: &timestamppb.Timestamp{Seconds: 1590179525, Nanos: 0},
        })
        require.Nil(t, err)

        // Assert we got what we want.
        require.Equal(t, "Ok", res.Text)
    })


}
Run Code Online (Sandbox Code Playgroud)

这是在上面包含的文件EndpointRequest中定义对象的方式v1.go

// An v1 interface Endpoint Request object.
message EndpointRequest {

  // a is something.
  string a = 1 [(validate.rules).string.min_len = 1];

  // b can be a complex object.
  google.protobuf.Struct b = 2;

 // c is a timestamp.
 google.protobuf.Timestamp c = 3;

}
Run Code Online (Sandbox Code Playgroud)

上面的测试用例似乎工作正常。

但我想用 grpcurl 模拟同样的测试用例。

这有效:

grpcurl -d '{"a": "S", "b": {"T": "U", "V": "W"}}' -plaintext localhost:11000 myteam.myproject.v1.MyProject/Endpoint
Run Code Online (Sandbox Code Playgroud)

但是当我尝试以下操作时,它失败了:

grpcurl -d '{"a": "S", "b": {"T": "U", "V": "W"}, "c": "1590179525"}' -plaintext localhost:11000 myteam.myproject.v1.MyProject/Endpoint
Error invoking method "myteam.myproject.v1.MyProject/Endpoint": error getting request data: bad Timestamp: parsing time "1590179525" as "2006-01-02T15:04:05.999999999Z07:00": cannot parse "179525" as "-"
Run Code Online (Sandbox Code Playgroud)

如何通过 grpcurl 发送时间戳?

Mar*_*arc 7

google.protobuf.Timestamp的基础类型是timestamppb.Timestamp

文档详细说明了 json 表示的格式:

JSON 映射

在 JSON 格式中,Timestamp 类型被编码为RFC 3339格式的字符串。即,格式为“{year}-{month}-{day}T{hour}:{min}:{sec}[.{frac_sec}]Z”,其中 {year} 始终使用四位数字表示,而 { Month}、{day}、{hour}、{min} 和 {sec} 均用零填充为两位数。秒小数部分是可选的,最多可达 9 位(即最多 1 纳秒分辨率)。“Z”后缀表示时区(“UTC”);时区是必需的。在打印时间戳类型时,proto3 JSON 序列化程序应始终使用 UTC(如“Z”所示),并且 proto3 JSON 解析器应该能够接受 UTC 和其他时区(如偏移量所示)。

例如,“2017-01-15T01:30:15.01Z”编码 2017 年 1 月 15 日 UTC 01:30 过后 15.01 秒。

这也可以在您收到的错误消息中看到:

解析时间“1590179525”为“2006-01-02T15:04:05.999999999Z07:00”:无法将“179525”解析为“-”

这是 Go 的标准时间解析,如Time.Parse中所述。

因此,您应该传递以下字符串,而不是传递自纪元以来的秒数字符串:

2020-05-22T20:32:05Z
Run Code Online (Sandbox Code Playgroud)

上述字符串是通过运行获得的:

fmt.Println(time.Unix(1590179525, 0).Format(time.RFC3339))
Run Code Online (Sandbox Code Playgroud)