Address Gadfly review on #10: clearable color/props, column lists, dedup
Build image / build-and-push (push) Successful in 7s
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:
+66
-37
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user