Firestore golang在将数据反序列化为proto类型时出现问题。

huangapple go评论88阅读模式
英文:

Firestore golang issue with unmarshalling to proto type

问题

我正在尝试理解为什么在将结构体的引用传递给protobuf中的DataTo()方法后,它没有返回所有字段,但是如果我运行几乎相同的Data()方法,我可以得到所有字段。

type Employee struct {
	StartDate   string 
	Id          string 
	Name        string
	Avatar      string
}
list, err := client.Collection(Company).Doc(user.CompanyID).Collection(Workers).Documents(ctx).GetAll()
if err != nil {
	log.Println(err.Error())
}
values := make([]*pb.Employee, len(list))
for i, v := range list {
	log.Println("By Data", &pb.Employee{
		StartDate: fmt.Sprint(b.Data()["startDate"]),
		Name:      b.Data()["name"].(string),
		Avatar:    b.Data()["avatar"].(string),
		Id:        b.Data()["id"].(string),
	})
	u := &pb.Employee{}
	b.DataTo(u)
	log.Println("by dataTo", u.StartDate, u.Name, u.Avatar, u.Id)
	values[i]= u
}
log.Println(values)

输出:

By Data start_date:"2022-07-08 12:37:47.132904 +0000 UTC" id:"DILBuRmxVzVpOVG4iPuUeb8A4tN2" name:"alap" avatar:"https://image.com"
by dataTo    
By Data start_date:"2022-07-08 12:37:39.901286 +0000 UTC" id:"bH6wuk0ooCMKsh7RQqlGWtXhIZr1" name:"Jack" avatar:"https://image3.com"
by dataTo  Jack https://image3.com
[avatar:"https://image.com" name:"Jakub"  avatar:"https://image3.com"] 

所以,如上所示,当尝试通过DataTo(p)方法获取文档时,缺少了一些内容。有人能看出我在这里做错了什么吗?

谢谢。

英文:

i am trying to understand why the DataTo() method is not returning me all fields after passing the reference of struct in protobuf, but if i run almost same method Data() i am getting all

type Employee struct {
	StartDate   string 
	Id          string 
	Name        string
	Avatar      string
}
list, err := client.Collection(Company).Doc(user.CompanyID).Collection(Workers).Documents(ctx).GetAll()
		if err != nil {
			log.Println(err.Error())
		}
		values := make([]*pb.Employee, len(list))
		for i, v := range list {
			log.Println("By Data", &pb.Employee{
				StartDate: fmt.Sprint(b.Data()["startDate"]),
				Name:      b.Data()["name"].(string),
				Avatar:    b.Data()["avatar"].(string),
				Id:        b.Data()["id"].(string),
			})
			u := &pb.Employee{}
			b.DataTo(u)
			log.Println("by dataTo", u.StartDate, u.Name, u.Avatar, u.Id)
			values[i]= u
		}
log.Println(values)

Output:

By Data start_date:"2022-07-08 12:37:47.132904 +0000 UTC" id:"DILBuRmxVzVpOVG4iPuUeb8A4tN2" name:"alap" avatar:"https://image.com"
by dataTo    
By Data start_date:"2022-07-08 12:37:39.901286 +0000 UTC" id:"bH6wuk0ooCMKsh7RQqlGWtXhIZr1" name:"Jack" avatar:"https://image3.com"
by dataTo  Jack https://image3.com
[avatar:"https://image.com" name:"Jakub"  avatar:"https://image3.com"] 

so as u could see above things are missing when trying to get document via DataTo(p) method. Anyone can see what i am doing wrong here ?

Regards.

答案1

得分: 1

这是一个很好的实践方法:

err := b.DataTo(&u)
if err != nil {
   fmt.Println(err)
}

也许在你的情况下,struct 中的某个类型数据与 Firestore 中的模式不匹配。如果我能猜测一下,StartDate 字段在 Firestore 的模式中是一个 timestamp 字段,所以你必须在你的 Golang 结构体中设置 time.Time 类型。

英文:

its good practice for using

err := b.DataTo(&u)
if err != nil {
   fmt.Println(err)
}

maybe in your case, there is type data in the struct that didn't match with the schema in firestore.
if I could guess the StartDate field. if in the firestore schema it is a timestamp field, so you must set time.Time in your golang struct.

huangapple
  • 本文由 发表于 2022年7月9日 00:58:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/72914675.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定