paulmach / orb

Types and utilities for working with 2d geometry in Golang
MIT License
886 stars 103 forks source link

Mongo/BSON: no decoder found for orb.Geometry #122

Closed jclaessens97 closed 1 year ago

jclaessens97 commented 1 year ago

I wanted to add a struct containing a featureCollection to mongoDB. Inserting the document isn't an issue, but as soon as I want to fetch the document it fails with the following message: error decoding key routes.0.legs.features.0.geometry: no decoder found for orb.Geometry.

My inserted document looks like this: image

type SimulationInput struct {
    Status    SimulationStatus `json:"status" default:"RUNNING"`
    CreatedAt time.Time        `json:"createdAt" bson:"createdAt"`

    Ship                 Ship                 `json:"ship"`
    Routes               []Route              `json:"routes"`
}

type Route struct {
    Name                string                     `json:"name"`
    TwoWay              bool                       `json:"twoWay"`
    Legs                *geojson.FeatureCollection `json:"legs"`
}

func (s *MongoStore) SaveSimulationInput(input *data.SimulationInput) interface{} {
    result, err := s.inputCollection.InsertOne(context.Background(), input)
    if err != nil {
        s.logger.Error(err)
    }
    return result.InsertedID
}

func (s *MongoStore) GetSimulationInput(id string) (*data.SimulationInput, error) {
    objectId, err := primitive.ObjectIDFromHex(id)
    if err != nil {
        return nil, err
    }
    filterQuery := map[string]interface{}{
        "_id": objectId,
    }

    var simulationInput *data.SimulationInput
    if err := s.inputCollection.FindOne(context.Background(), filterQuery).Decode(&simulationInput); err != nil {
        return nil, err
    }
    return simulationInput, nil
}

Is this a bug? Or is there anything I can do differently to encode/decode orb.Geometry?

paulmach commented 1 year ago

Weird that it works for insert but not find.

I would guess the FeatureCollection type needs to implement MarshalBSON and UnmarshalBSON. I'm surprised the driver doesn't fallback to MarshalJSON is those aren't available.

jclaessens97 commented 1 year ago

Yeah it's really weird... It does fallback to MarshalJSON. But if I wrap MarshalJSON in a function like follows

func (fc FeatureCollection) MarshalBSON() ([]byte, error) {
    return fc.MarshalJSON()
}

I get runtime error: invalid memory address or nil pointer dereference

And if I implement UnmarshalBSON like follows:

func (fc FeatureCollection) UnmarshalBSON(data []byte) error {
    return fc.UnmarshalJSON(data)
}

I get error decoding key routes.0.legs: invalid character 'î' looking for beginning of value

This is my first bigger project in Go, so I'm not entirely sure how I should go about this, but I can't imagine I'm the first who tries to insert a geoJSON in a mongoDB using Go? 😄 Any chance you can point me in the right direction?

ghost commented 1 year ago

Weird that it works for insert but not find.

Find decodes the data to Go values. Decode fails because the codec does not know the type to create for an orb.Geometry.

I would guess the FeatureCollection type needs to implement MarshalBSON and UnmarshalBSON.

Yes.

I'm surprised the driver doesn't fallback to MarshalJSON is those aren't available.

BSON and JSON are different data encodings. MarshalJSON returns JSON, not BSON.

paulmach commented 1 year ago

I have PR to add support for BSON. Please have a look https://github.com/paulmach/orb/pull/123

jclaessens97 commented 1 year ago

Thanks @paulmach, I'll check it on friday!

paulmach commented 1 year ago

merged https://github.com/paulmach/orb/pull/123 and published https://github.com/paulmach/orb/releases/tag/v0.9.0 with the change