func UpdateByID(m CrudResource) httperr.Handler { return func(w http.ResponseWriter, r *http.Request) error { dbmap, err := getDB() defer dbmap.Db.Close() if err != nil { return err } id := r.URL.Query().Get(":id") mCopy := copyResource(m) if err := GetID(dbmap, mCopy, id); err != nil { return err } updateCopy := copyResource(m) if err := json.NewDecoder(r.Body).Decode(updateCopy); err != nil { message := fmt.Sprintf("%s's json could not parsed.", m.TableName()) return httperr.New(http.StatusBadRequest, message, err) } merge.TagWl(updateCopy, mCopy) if _, err := dbmap.Update(mCopy); err != nil { message := fmt.Sprintf("%s did not pass validation.", m.TableName()) return httperr.New(http.StatusBadRequest, message, err) } return json.NewEncoder(w).Encode(mCopy) } }
It("should merge the struct whitelisted fields", func() { from := &TestStruct{"from", 1, "a"} to := &TestStruct{"to", 0, "b"} merge.Wl(from, to, "Two", "Three") Expect(to.One).To(Equal("to")) Expect(to.Two).To(Equal(1)) Expect(to.Three).To(Equal("a")) }) }) Describe("TagWl", func() { It("should merge the struct whitelisted fields", func() { from := &TestStruct{"from", 1, "a"} to := &TestStruct{"to", 0, "b"} merge.TagWl(from, to) Expect(to.One).To(Equal("from")) Expect(to.Two).To(Equal(0)) Expect(to.Three).To(Equal("b")) }) }) Describe("Bl", func() { It("should not merge the struct blacklisted fields", func() { from := &TestStruct{"from", 1, "a"} to := &TestStruct{"to", 0, "b"} merge.Bl(from, to, "Two") Expect(to.One).To(Equal("from")) Expect(to.Two).To(Equal(0)) Expect(to.Three).To(Equal("a"))