[gogf/gf][建议] gconv.Scan 支持 protobuf.TimeStamppb 转换到 gtime.Time

2024-06-25 612 views
7
1. 您使用的是什么版本Go和系统类型/架构?

go 版本 go1.19.3 darwin/amd64

2. 您使用的是什么版本GoFrame

v2.2.2

3. 最新版本的版本还会重现这个问题吗?

是的

4. 你做了什么?
package tbase

import (
    "bytes"
    "encoding/json"
    "fmt"
    "testing"

    "github.com/gogf/gf/v2/os/gtime"
    "github.com/gogf/gf/v2/util/gconv"
    protoimpl "google.golang.org/protobuf/runtime/protoimpl"
    "google.golang.org/protobuf/types/known/timestamppb"
)

type AuthRes1 struct {
    Name     string      `dc:"用户姓名"`
    Nick     string      `dc:"用户昵称"`
    Avatar   string      `dc:"头像"   `                            //
    Sex      int         `dc:"性别 0女 1男 2未知" json:"Sex"      `    //
    Status   int         `dc:"状态 -1禁用 1正常 0异常" json:"Status"   ` // -1禁用,1正常
    Channel  string      `dc:"渠道" json:"Channel"  `              //
    PUID     int64       `dc:"推介人" json:"PUID"     `             //
    CNum     int64       `dc:"团队成员数" json:"CNum"     `           //
    Birthday *gtime.Time `dc:"生日" json:"Birthday" `              //
    UID      uint64      `dc:"用户号"`
    Units    []SimUnit   `dc:"用户组织编号"`
    AID      uint64      `dc:"应用编号"`
    //当前企业
    GID uint64 `dc:"企业或分组编号"`
    Key string `dc:"SaaS应用分割"`
    //角色 SysAdmin,SysOrgAdmin,SysMember,
    Rule string `dc:"角色"`
    // 用户组清单
    Groups    []SimUserGroup `dc:"所在用户组数组"`
    Prefix    string         ``
    JWT       string         `dc:"JWT认证的字符串,用于跟第三方系统交互"`
    JWTExpire *gtime.Time    `dc:"JWT过期时间"`
}

type AuthRes2 struct {
    state         protoimpl.MessageState
    sizeCache     protoimpl.SizeCache
    unknownFields protoimpl.UnknownFields

    // Tags: dc:"用户姓名"
    Name string `protobuf:"bytes,1,opt,name=Name,proto3" json:"Name,omitempty"`
    // Tags: dc:"用户昵称"
    Nick string `protobuf:"bytes,2,opt,name=Nick,proto3" json:"Nick,omitempty"`
    // Tags: dc:"头像"
    Avatar string `protobuf:"bytes,3,opt,name=Avatar,proto3" json:"Avatar,omitempty"`
    // Tags: dc:"性别 0女 1男 2未知" json:"Sex"
    Sex int32 `protobuf:"varint,4,opt,name=Sex,proto3" json:"Sex,omitempty"`
    // Tags: dc:"状态 -1禁用 1正常 0异常" json:"Status"
    Status int32 `protobuf:"varint,5,opt,name=Status,proto3" json:"Status,omitempty"`
    // Tags: dc:"渠道" json:"Channel"
    Channel string `protobuf:"bytes,6,opt,name=Channel,proto3" json:"Channel,omitempty"`
    // Tags: dc:"推介人" json:"PUID"
    PUID int64 `protobuf:"varint,7,opt,name=PUID,proto3" json:"PUID,omitempty"`
    // Tags: dc:"团队成员数" json:"CNum"
    CNum int64 `protobuf:"varint,8,opt,name=CNum,proto3" json:"CNum,omitempty"`
    // Tags: dc:"生日" json:"Birthday"
    Birthday *timestamppb.Timestamp `protobuf:"bytes,9,opt,name=Birthday,proto3" json:"Birthday,omitempty"`
    // Tags: dc:"用户号"
    UID uint64 `protobuf:"varint,10,opt,name=UID,proto3" json:"UID,omitempty"`
    // Tags: dc:"用户组织编号"
    Units []*SimUnit `protobuf:"bytes,11,rep,name=Units,proto3" json:"Units,omitempty"`
    // Tags: dc:"应用编号"
    AID uint64 `protobuf:"varint,12,opt,name=AID,proto3" json:"AID,omitempty"`
    // Tags: dc:"企业或分组编号"
    GID uint64 `protobuf:"varint,13,opt,name=GID,proto3" json:"GID,omitempty"`
    // Tags: dc:"SaaS应用分割"
    Key string `protobuf:"bytes,14,opt,name=Key,proto3" json:"Key,omitempty"`
    // Tags: dc:"角色"
    Rule string `protobuf:"bytes,15,opt,name=Rule,proto3" json:"Rule,omitempty"`
    // Tags: dc:"所在用户组数组"
    Groups []*SimUserGroup `protobuf:"bytes,16,rep,name=Groups,proto3" json:"Groups,omitempty"`
    // Tags:
    Prefix string `protobuf:"bytes,17,opt,name=Prefix,proto3" json:"Prefix,omitempty"`
    // Tags: dc:"JWT认证的字符串,用于跟第三方系统交互"
    JWT string `protobuf:"bytes,18,opt,name=JWT,proto3" json:"JWT,omitempty"`
    // Tags: dc:"JWT过期时间"
    JWTExpire *timestamppb.Timestamp `protobuf:"bytes,19,opt,name=JWTExpire,proto3" json:"JWTExpire,omitempty"`
}

func TestScan(t *testing.T) {
    s := `{
        "Name": "真难什元处便半",
        "Nick": "赵伟",
        "Avatar": "http://dummyimage.com/100x100",
        "Sex": 18,
        "Status": 1,
        "Channel": "",
        "PUID": 0,
        "CNum": 0,
        "Birthday": "2007-02-18 00:00:00",
        "UID": 19,
        "Units": [],
        "AID": 0,
        "GID": 0,
        "Key": "",
        "Rule": "",
        "Groups": [],
        "Prefix": "",
        "JWT": "",
        "JWTExpire": null
    }`
    buf := bytes.NewBufferString(s)
    auth := AuthRes1{}
    json.Unmarshal(buf.Bytes(), &auth)
    bs, _ := json.Marshal(auth)
    bstr := fmt.Sprintf("%s", bs)
    n := 0
    for i := 0; i < 1000; i++ {
        tmp := &AuthRes2{}
        gconv.Scan(auth, tmp)
        tstr, _ := json.Marshal(*tmp)
        if bstr != fmt.Sprintf("%s", tstr) {
            fmt.Println("Name", tmp.Name)
            n++
        } else {
            // fmt.Println("Match")
        }
    }
    fmt.Println("n=", n)
}
5. 你期望看到什么?

扫描结果应等于源

6. 你看到的是什么?

有时候会损失赔偿金

7. 日志记录 图像

回答

7

测试中补充所有json标签吗?

9

补充标签后的AuthRes1结构如下:

type AuthRes1 struct {
    Name     string      `dc:"用户姓名" json:"Name"`
    Nick     string      `dc:"用户昵称" json:"Nick"`
    Avatar   string      `dc:"头像"  json:"Avatar" `               //
    Sex      int         `dc:"性别 0女 1男 2未知" json:"Sex"      `    //
    Status   int         `dc:"状态 -1禁用 1正常 0异常" json:"Status"   ` // -1禁用,1正常
    Channel  string      `dc:"渠道" json:"Channel"  `              //
    PUID     int64       `dc:"推介人" json:"PUID"     `             //
    CNum     int64       `dc:"团队成员数" json:"CNum"     `           //
    Birthday *gtime.Time `dc:"生日" json:"Birthday" `              //
    UID      uint64      `dc:"用户号" json:"UID"`
    Units    []SimUnit   `dc:"用户组织编号" json:"Units"`
    AID      uint64      `dc:"应用编号" json:"AID"`
    //当前企业
    GID uint64 `dc:"企业或分组编号" json:"GID"`
    Key string `dc:"SaaS应用分割" json:"Key"`
    //角色 SysAdmin,SysOrgAdmin,SysMember,
    Rule string `dc:"角色" json:"Rule"`
    // 用户组清单
    Groups    []SimUserGroup `dc:"所在用户组数组" json:"Groups"`
    Prefix    string         `json:"Prefix"`
    JWT       string         `dc:"JWT认证的字符串,用于跟第三方系统交互" json:"JWT"`
    JWTExpire *gtime.Time    `dc:"JWT过期时间" json:"JWTExpire"`
}

报告全文,问题依旧存在

0
图像
6

更改 protobuf.TimeStamppb 为 gtime.Time 后问题解决。