Firestore golang issue with unmarshalling to proto type

352 Views Asked by At

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

There are 1 best solutions below

0
Rio Fernando On

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.