rethinkdb / rethinkdb-go

Go language driver for RethinkDB
Apache License 2.0
1.65k stars 182 forks source link

Invalid JSON being returned for .Delete(db.DeleteOpts{ReturnChanges: true}) #441

Open cstetson opened 6 years ago

cstetson commented 6 years ago

I am working with a microservice application where I need to get back the values for a record that I delete and using the "ReturnChanges" feature of rethink is perfect for this. It works for update and replace, but fails for delete.

resp, err = db.DB("content").Table("posts").Get(articleId).Delete(db.DeleteOpts{ReturnChanges: true}).Run(env.Session)
    if err != nil {
        fmt.Print(err)
        return StatusError{500, err}
    }

    result , _ := resp.NextResponse()
    spew.Dump(result)

    deRefedJson := (*json.RawMessage)(&result)
    spew.Dump(result)

    var jsonResult interface{}
    err = json.Unmarshal(*deRefedJson, &jsonResult)
    spew.Dump(jsonResult)
    var changes Changes
    changes = jsonResult.(Changes)

The return values for the changes come as

{
 "changes": {
  {
   "new_val": null,
   "old_val": {
    "author": "Christopher Stetson",
    "date": {
     "$reql_type$": "TIME",
     "epoch_time": 1.527033878861e+09,
     "timezone": "+00:00"
    },
    "location": "Oakland CA",
    "title": "Test Post 1.4",
    "album_id": 38,
    "body": " This is the body",
    "extract": "This is the extract",
    "id": "b17caf88-c449-4e2a-8fee-f0e498f3d91c",
    "photo": "http://fake-s3:4569/mra-images/uploads/photos/8abe3ad9-50d1-4c91-bbb7-6ca27fbad24c/medium.jpg"
   }
  }
 },
 "deleted": 1,
 "errors": 0,
 "inserted": 0,
 "replaced": 0,
 "skipped": 0,
 "unchanged": 0
}

Where it should be returning the changes as an array:

 "changes": [
  {
   "new_val": null,
   "old_val": {
    "author": "Christopher Stetson",
    "date": {
     "$reql_type$": "TIME",
     "epoch_time": 1.527033878861e+09,
     "timezone": "+00:00"
    },
    "location": "Oakland CA",
    "title": "Test Post 1.4",
    "album_id": 38,
    "body": " This is the body",
    "extract": "This is the extract",
    "id": "b17caf88-c449-4e2a-8fee-f0e498f3d91c",
    "photo": "http://fake-s3:4569/mra-images/uploads/photos/8abe3ad9-50d1-4c91-bbb7-6ca27fbad24c/medium.jpg"
   }
  }
],
 "deleted": 1,
 "errors": 0,
 "inserted": 0,
 "replaced": 0,
 "skipped": 0,
 "unchanged": 0
}

Rethink returns the proper json structure.

pohzipohzi commented 6 years ago

Hi @cstetson. You should be using RunWrite instead of Run for write operations (see this). This will return a WriteResponse struct where you can simply access its Changes property. So it may look something like this:

    resp, _ = db.DB("content").Table("posts").Get(articleId).Delete(r.DeleteOpts{ReturnChanges: true}).RunWrite(session)
    fmt.Printf("%+v\n", resp)
    fmt.Println(reflect.TypeOf(resp.Changes)) // []gorethink.ChangeResponse
CMogilko commented 6 years ago

@pohzipohzi suggested the right way to get changes, but why json unmarshals to a single struct is needed to be researched.