Search code examples
gogoogle-cloud-platformgoogle-cloud-firestore

Pulling Map object from Firestore, getting random garbage


I'm using Go to try and pull some documents from Firestore, but have run into a pretty big wall: no matter how hard I try, I can't pull data from the query itself. When I print the doc.Data() or whatever it is, it works fine:

map[Ext: ID:107677035991703552 Kind:link Start:0.0 Stop:8.0 URL:ONHPHmNV0m4]

I do:

iter := client.Collection("intro-requests").Documents(ctx)
for {
    doc, err := iter.Next()
    if err == iterator.Done {
        break
    }
    var temp Request
    doc.DataTo(&temp)
    fmt.Println(temp)
    requests = append(requests, temp)
    for _, request := range requests {
        fmt.Println(doc.Data())
        createVote(s, request)
    }
    fmt.Println(doc.Data())
}

as per the Google example, but printing temp which, I get this:

{ link 0 0  }

but also sometimes this:

{  0 0  }

and sometimes this:

{ link 0 0 ONHPHmNV0m4 }

What gives? I can't even fathom what's going on here. I also can't find any other valid examples, because all of Google's examples don't go any further just printing the map, which works 100% of the time. It almost feels like the map is being turned into a byte array or something, and the program is just trying to salvage some numbers out of it.

Here's my class:

type Request struct {
    ID    string  `json:"ID"`
    Kind  string  `json:"Kind"`
    Start float64 `json:"Start"`
    Stop  float64 `json:"Stop"`
    URL   string  `json:"URL"`
    Ext   string  `json:"Ext"`
}

I've tried using third party deserialization packages, but nothing changes... Any clues?


Solution

  • Turns out that Go can't deserialize Float64, only Nil, Bool, Int64, and String. Weird that something like a type error would cause such behavior.

    I switched my float64s out for strings, then just used strconv.ParseFloat(). Problem solved