Address Gadfly review on #10: clearable color/props, column lists, dedup
Build image / build-and-push (push) Successful in 7s

Fixes from the PR #28 adversarial review (considered; not graded).

Correctness / API
- PATCH /objects/:id can now clear nullable color/props back to NULL: the
  request takes them as json.RawMessage, and ObjectPatch carries an explicit
  Set flag so an explicit `null` (clear) is distinguished from an absent
  field (unchanged) — the strongest cross-model finding (6 hits). New test.

Maintainability
- store/plantings.go + plants.go use explicit qualified column lists
  (qualifyColumns helper) instead of SELECT *, matching gardens/objects and
  surviving a future column add.
- Consolidated objectKinds + plantableByDefault into one kind→traits map.
- objectForRole factors the fetch-then-authorize shared by UpdateObject and
  DeleteObject; dropped the redundant kind check in CreateObject
  (finalizeObject is the single validation point).
- Request→service mapping via toInput()/toPatch() methods (matches gardens).
- Renamed handler gardenFull → getGardenFull (verbNoun); test helper
  decodeGarden → decodeMap; generic bind-error messages.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01JdQpdYYsTgtkJBxbcpAszi
This commit is contained in:
2026-07-18 19:47:10 -04:00
co-authored by Claude Opus 4.8
parent 3dd935fb19
commit 0793fef17c
8 changed files with 183 additions and 93 deletions
+1 -1
View File
@@ -81,7 +81,7 @@ func New(cfg *config.Config, svc *service.Service) *gin.Engine {
gardens.GET("/:id", h.getGarden)
gardens.PATCH("/:id", h.updateGarden)
gardens.DELETE("/:id", h.deleteGarden)
gardens.GET("/:id/full", h.gardenFull) // one-shot editor load
gardens.GET("/:id/full", h.getGardenFull) // one-shot editor load
gardens.POST("/:id/objects", h.createObject)
// Objects are addressed by their own id; the service resolves the owning
+11 -10
View File
@@ -20,13 +20,14 @@ func registerAndCookie(t *testing.T, r *gin.Engine, email string) *http.Cookie {
return sessionCookieFrom(t, w)
}
func decodeGarden(t *testing.T, body []byte) map[string]any {
// decodeMap unmarshals any JSON object response into a generic map.
func decodeMap(t *testing.T, body []byte) map[string]any {
t.Helper()
var g map[string]any
if err := json.Unmarshal(body, &g); err != nil {
t.Fatalf("decode garden: %v (body %s)", err, body)
var m map[string]any
if err := json.Unmarshal(body, &m); err != nil {
t.Fatalf("decode json object: %v (body %s)", err, body)
}
return g
return m
}
func TestGardenCRUDFlow(t *testing.T) {
@@ -38,7 +39,7 @@ func TestGardenCRUDFlow(t *testing.T) {
if w.Code != http.StatusCreated {
t.Fatalf("create status = %d, body %s", w.Code, w.Body.String())
}
created := decodeGarden(t, w.Body.Bytes())
created := decodeMap(t, w.Body.Bytes())
id := int64(created["id"].(float64))
if created["widthCm"].(float64) != 1000 || created["unitPref"].(string) != "metric" {
t.Errorf("defaults not applied: %+v", created)
@@ -69,7 +70,7 @@ func TestGardenCRUDFlow(t *testing.T) {
if w.Code != http.StatusOK {
t.Fatalf("patch status = %d, body %s", w.Code, w.Body.String())
}
patched := decodeGarden(t, w.Body.Bytes())
patched := decodeMap(t, w.Body.Bytes())
if patched["name"].(string) != "Front" || patched["version"].(float64) != 2 {
t.Errorf("patch didn't persist/bump: %+v", patched)
}
@@ -90,7 +91,7 @@ func TestGardenVersionConflictEnvelope(t *testing.T) {
cookie := registerAndCookie(t, r, "[email protected]")
w := doJSON(t, r, http.MethodPost, "/api/v1/gardens", map[string]any{"name": "Yard"}, cookie)
id := int64(decodeGarden(t, w.Body.Bytes())["id"].(float64))
id := int64(decodeMap(t, w.Body.Bytes())["id"].(float64))
body := map[string]any{"name": "Yard2", "widthCm": 1000, "heightCm": 1000, "unitPref": "metric", "version": 1}
// First patch at version 1 succeeds (→ version 2).
@@ -123,7 +124,7 @@ func TestGardenCrossUserIsNotFound(t *testing.T) {
bob := registerAndCookie(t, r, "[email protected]")
w := doJSON(t, r, http.MethodPost, "/api/v1/gardens", map[string]any{"name": "Alice's"}, alice)
id := int64(decodeGarden(t, w.Body.Bytes())["id"].(float64))
id := int64(decodeMap(t, w.Body.Bytes())["id"].(float64))
// Bob sees a 404 (existence masked), not a 403.
if w := doJSON(t, r, http.MethodGet, gardenPath(id), nil, bob); w.Code != http.StatusNotFound {
@@ -166,7 +167,7 @@ func TestGardenUpdateRejectsBadVersion(t *testing.T) {
r := authEngine(t, localCfg())
cookie := registerAndCookie(t, r, "[email protected]")
w := doJSON(t, r, http.MethodPost, "/api/v1/gardens", map[string]any{"name": "Yard"}, cookie)
id := int64(decodeGarden(t, w.Body.Bytes())["id"].(float64))
id := int64(decodeMap(t, w.Body.Bytes())["id"].(float64))
// A negative (or zero) version is invalid input (400), not a version conflict.
for _, v := range []int{0, -1} {
+66 -37
View File
@@ -30,9 +30,19 @@ type objectCreateRequest struct {
Notes string `json:"notes"`
}
func (r objectCreateRequest) toInput() service.ObjectInput {
return service.ObjectInput{
Kind: r.Kind, Name: r.Name, Shape: r.Shape,
XCM: r.XCM, YCM: r.YCM, WidthCM: r.WidthCM, HeightCM: r.HeightCM,
RotationDeg: r.RotationDeg, ZIndex: r.ZIndex, Plantable: r.Plantable,
Color: r.Color, Props: propsFromRaw(r.Props), Notes: r.Notes,
}
}
// objectUpdateRequest is the body for PATCH /objects/:id: every field optional
// (absent = unchanged), plus the required current version. Kind and shape are
// immutable and not accepted.
// immutable and not accepted. color/props are json.RawMessage so an explicit
// null (clear the override) is distinguishable from an absent field (unchanged).
type objectUpdateRequest struct {
Name *string `json:"name"`
XCM *float64 `json:"xCm"`
@@ -42,20 +52,60 @@ type objectUpdateRequest struct {
RotationDeg *float64 `json:"rotationDeg"`
ZIndex *int `json:"zIndex"`
Plantable *bool `json:"plantable"`
Color *string `json:"color"`
Color json.RawMessage `json:"color"`
Props json.RawMessage `json:"props"`
Notes *string `json:"notes"`
Version int64 `json:"version" binding:"required,min=1"`
}
// propsFromRaw maps a JSON props value to the stored *string: a present, non-null
// value becomes its JSON text; absent or null becomes nil (leave/none).
func (r objectUpdateRequest) toPatch() (service.ObjectPatch, error) {
color, setColor, err := parseNullableString(r.Color)
if err != nil {
return service.ObjectPatch{}, err
}
props, setProps := parseNullableJSON(r.Props)
return service.ObjectPatch{
Name: r.Name, XCM: r.XCM, YCM: r.YCM, WidthCM: r.WidthCM, HeightCM: r.HeightCM,
RotationDeg: r.RotationDeg, ZIndex: r.ZIndex, Plantable: r.Plantable,
SetColor: setColor, Color: color, SetProps: setProps, Props: props, Notes: r.Notes,
}, nil
}
// propsFromRaw maps a JSON props value to the stored *string for create: a
// present, non-null value becomes its JSON text; absent or null becomes nil.
func propsFromRaw(raw json.RawMessage) *string {
if len(raw) == 0 || string(raw) == "null" {
v, set := parseNullableJSON(raw)
if !set {
return nil
}
return v
}
// parseNullableString decodes a JSON string|null field into (value, present).
// Absent → (nil, false); null → (nil, true); "x" → (&"x", true). A non-string
// value is an error.
func parseNullableString(raw json.RawMessage) (value *string, present bool, err error) {
if len(raw) == 0 {
return nil, false, nil
}
if err := json.Unmarshal(raw, &value); err != nil {
return nil, false, err
}
return value, true, nil
}
// parseNullableJSON decodes any JSON value into (text, present) for a nullable
// TEXT column. Absent → (nil, false); null → (nil, true); anything else → the
// raw JSON text (&, true).
func parseNullableJSON(raw json.RawMessage) (value *string, present bool) {
if len(raw) == 0 {
return nil, false
}
if string(raw) == "null" {
return nil, true
}
s := string(raw)
return &s
return &s, true
}
func (h *handlers) createObject(c *gin.Context) {
@@ -65,24 +115,10 @@ func (h *handlers) createObject(c *gin.Context) {
}
var req objectCreateRequest
if err := c.ShouldBindJSON(&req); err != nil {
writeAPIError(c, http.StatusBadRequest, "INVALID_INPUT", "kind is required")
writeAPIError(c, http.StatusBadRequest, "INVALID_INPUT", "invalid object: kind is required")
return
}
o, err := h.svc.CreateObject(c.Request.Context(), mustActor(c).ID, gardenID, service.ObjectInput{
Kind: req.Kind,
Name: req.Name,
Shape: req.Shape,
XCM: req.XCM,
YCM: req.YCM,
WidthCM: req.WidthCM,
HeightCM: req.HeightCM,
RotationDeg: req.RotationDeg,
ZIndex: req.ZIndex,
Plantable: req.Plantable,
Color: req.Color,
Props: propsFromRaw(req.Props),
Notes: req.Notes,
})
o, err := h.svc.CreateObject(c.Request.Context(), mustActor(c).ID, gardenID, req.toInput())
if err != nil {
writeServiceError(c, err)
return
@@ -97,22 +133,15 @@ func (h *handlers) updateObject(c *gin.Context) {
}
var req objectUpdateRequest
if err := c.ShouldBindJSON(&req); err != nil {
writeAPIError(c, http.StatusBadRequest, "INVALID_INPUT", "a current version is required")
writeAPIError(c, http.StatusBadRequest, "INVALID_INPUT", "invalid update: a current version is required")
return
}
o, err := h.svc.UpdateObject(c.Request.Context(), mustActor(c).ID, id, service.ObjectPatch{
Name: req.Name,
XCM: req.XCM,
YCM: req.YCM,
WidthCM: req.WidthCM,
HeightCM: req.HeightCM,
RotationDeg: req.RotationDeg,
ZIndex: req.ZIndex,
Plantable: req.Plantable,
Color: req.Color,
Props: propsFromRaw(req.Props),
Notes: req.Notes,
}, req.Version)
patch, err := req.toPatch()
if err != nil {
writeAPIError(c, http.StatusBadRequest, "INVALID_INPUT", "invalid update payload")
return
}
o, err := h.svc.UpdateObject(c.Request.Context(), mustActor(c).ID, id, patch, req.Version)
if err != nil {
if errors.Is(err, domain.ErrVersionConflict) {
writeVersionConflict(c, o)
@@ -136,7 +165,7 @@ func (h *handlers) deleteObject(c *gin.Context) {
c.Status(http.StatusNoContent)
}
func (h *handlers) gardenFull(c *gin.Context) {
func (h *handlers) getGardenFull(c *gin.Context) {
gardenID, ok := parseIDParam(c, "id")
if !ok {
return
+42 -6
View File
@@ -16,7 +16,7 @@ func createGardenAPI(t *testing.T, r *gin.Engine, cookie *http.Cookie, name stri
if w.Code != http.StatusCreated {
t.Fatalf("create garden: status %d, body %s", w.Code, w.Body.String())
}
return int64(decodeGarden(t, w.Body.Bytes())["id"].(float64))
return int64(decodeMap(t, w.Body.Bytes())["id"].(float64))
}
func objectPath(id int64) string { return "/api/v1/objects/" + strconv.FormatInt(id, 10) }
@@ -36,7 +36,7 @@ func TestObjectCRUDAndFull(t *testing.T) {
if w.Code != http.StatusCreated {
t.Fatalf("create object: status %d, body %s", w.Code, w.Body.String())
}
obj := decodeGarden(t, w.Body.Bytes())
obj := decodeMap(t, w.Body.Bytes())
oid := int64(obj["id"].(float64))
if obj["plantable"].(bool) != true {
t.Error("bed should default plantable=true")
@@ -68,7 +68,7 @@ func TestObjectCRUDAndFull(t *testing.T) {
if w.Code != http.StatusOK {
t.Fatalf("patch: status %d, body %s", w.Code, w.Body.String())
}
patched := decodeGarden(t, w.Body.Bytes())
patched := decodeMap(t, w.Body.Bytes())
if patched["xCm"].(float64) != 500 || patched["version"].(float64) != 2 {
t.Errorf("patch didn't apply/bump: %+v", patched)
}
@@ -90,7 +90,7 @@ func TestObjectVersionConflict(t *testing.T) {
gid := createGardenAPI(t, r, cookie, "Yard")
w := doJSON(t, r, http.MethodPost, objectsPath(gid),
map[string]any{"kind": "bed", "xCm": 300, "yCm": 300, "widthCm": 100, "heightCm": 100}, cookie)
oid := int64(decodeGarden(t, w.Body.Bytes())["id"].(float64))
oid := int64(decodeMap(t, w.Body.Bytes())["id"].(float64))
body := map[string]any{"xCm": 400, "version": 1}
if w := doJSON(t, r, http.MethodPatch, objectPath(oid), body, cookie); w.Code != http.StatusOK {
@@ -117,7 +117,7 @@ func TestObjectCrossUserIsNotFound(t *testing.T) {
gid := createGardenAPI(t, r, alice, "Alice's")
w := doJSON(t, r, http.MethodPost, objectsPath(gid),
map[string]any{"kind": "bed", "xCm": 300, "yCm": 300, "widthCm": 100, "heightCm": 100}, alice)
oid := int64(decodeGarden(t, w.Body.Bytes())["id"].(float64))
oid := int64(decodeMap(t, w.Body.Bytes())["id"].(float64))
// Bob can't see the garden or its objects — all 404.
if w := doJSON(t, r, http.MethodGet, fullPath(gid), nil, bob); w.Code != http.StatusNotFound {
@@ -134,6 +134,42 @@ func TestObjectCrossUserIsNotFound(t *testing.T) {
}
}
func TestObjectPatchClearsColorAndProps(t *testing.T) {
r := authEngine(t, localCfg())
cookie := registerAndCookie(t, r, "[email protected]")
gid := createGardenAPI(t, r, cookie, "Yard")
// Create with a color override and props.
w := doJSON(t, r, http.MethodPost, objectsPath(gid),
map[string]any{"kind": "bed", "xCm": 300, "yCm": 300, "widthCm": 100, "heightCm": 100,
"color": "#3f8f4f", "props": map[string]any{"heightCm": 40}}, cookie)
obj := decodeMap(t, w.Body.Bytes())
oid := int64(obj["id"].(float64))
if obj["color"].(string) != "#3f8f4f" {
t.Fatalf("color not set on create: %v", obj["color"])
}
// PATCH color:null props:null clears both back to absent (omitempty).
w = doJSON(t, r, http.MethodPatch, objectPath(oid),
map[string]any{"color": nil, "props": nil, "version": 1}, cookie)
if w.Code != http.StatusOK {
t.Fatalf("clear patch: status %d, body %s", w.Code, w.Body.String())
}
cleared := decodeMap(t, w.Body.Bytes())
if _, present := cleared["color"]; present {
t.Errorf("color should be cleared (absent), got %v", cleared["color"])
}
if _, present := cleared["props"]; present {
t.Errorf("props should be cleared (absent), got %v", cleared["props"])
}
// A patch that omits color leaves it unchanged (still absent here).
w = doJSON(t, r, http.MethodPatch, objectPath(oid), map[string]any{"xCm": 350, "version": 2}, cookie)
if w.Code != http.StatusOK {
t.Fatalf("move patch: %d", w.Code)
}
}
func TestObjectValidationRejects(t *testing.T) {
r := authEngine(t, localCfg())
cookie := registerAndCookie(t, r, "[email protected]")
@@ -154,7 +190,7 @@ func TestObjectValidationRejects(t *testing.T) {
if w.Code != http.StatusCreated {
t.Fatalf("props create = %d, body %s", w.Code, w.Body.String())
}
if props, _ := decodeGarden(t, w.Body.Bytes())["props"].(string); props == "" {
if props, _ := decodeMap(t, w.Body.Bytes())["props"].(string); props == "" {
t.Error("props should be stored and returned as a JSON string")
}
}