Laravel Go✓ Human-reviewed

One app-owned JSON attribute in Laravel Go

Extract one literal JSON key with explicit keep/set/forget semantics, preserved siblings and guarded concurrent persistence.

Approved 2026-12-04T15:00:06.756903Z · Submission 101

One app-owned JSON attribute in Laravel Go

Scope

Extract one literal theme key's keep/set/forget behavior over an existing JSON object. Preserve unrelated values and keep input ownership explicit. Exclude arbitrary paths, wildcards, forwarded collection operations, model scopes and whole-map replacement. Preserve Spatie's MIT notice for adapted source. At the time of writing the catalog listed Go's built-in JSON support; check get-recipe's current port catalog before extracting. This guidance has not been exercised in your app; verify it below.

Source behaviour

src/SchemalessAttributes.php reads raw JSON into a collection with empty-array fallback for non-array roots. get delegates to data_get, set merges iterable input or calls data_set, and forget calls Arr::forget. Mutations assign the model attribute, not a database save. src/Casts/SchemalessAttributes.php has JSON-string and non-array/null handling. tests/HasSchemalessAttributesTest.php covers defaults, saved scalar types, dotted/wildcard access and forgetting.

This extraction deliberately requires an object root, literal keys and explicit operations, and serializes an emptied object as {}. It does not mimic the upstream collection cast or its invalid-root fallback. Setting JSON null and forgetting the key remain distinct. Reconcile historical data and dependent output before switching.

Implementation

  1. Inventory defaults, stored types, SQL-null/JSON-null behavior, root shape, mutation and query callers. Capture exact fixtures; keep excluded package consumers intact. In Go, use the app's existing JSON decoder policy; arbitrary large JSON numbers must not silently round through float64 if that changes stored siblings.
  2. Validate a bounded operation and nullable theme string at the HTTP/action boundary, including size/rules. Derive trusted actor/tenant and authorize the row/field. Never accept arbitrary JSON paths, SQL identifiers or a whole untrusted map as writable attributes.
  3. The pure helper below copies the outer map and changes one literal key. nil stored map is rejected rather than silently treated as {}. Values in existing nested maps/slices remain shared read-only; do not mutate them through the result. Use the installed framework's db.JSON support if it meets the app's object/type contract; it is not automatically an upstream compatibility layer.
  4. Read the authorized row inside the existing row-locked transaction before modifying and saving it, or retain an optimistic-version conditional update with stale-write rejection. A copied map does not solve concurrent database lost updates. Check save/affected-row results, propagate context/transaction errors, preserve post-commit effects and scope cache invalidation.
  5. Serialize only permitted output fields, not private arbitrary attributes. Avoid new migrations or caches. Keep connection/isolation and JSON path updates tied to verified driver semantics; never interpolate request keys into SQL.

Complete pure top-level transformation. Imports shown; value == nil with operation == "set" means present JSON null, not deletion. Nested siblings must be treated as immutable:

import "fmt"

func patchTheme(stored map[string]any, operation string, value *string) (map[string]any, error) {
    if stored == nil { return nil, fmt.Errorf("stored value must be object") }
    next := make(map[string]any, len(stored)+1)
    for key, item := range stored { next[key] = item }
    switch operation {
    case "keep":
    case "set":
        if value == nil { next["theme"] = nil } else { next["theme"] = *value }
    case "forget":
        delete(next, "theme")
    default:
        return nil, fmt.Errorf("unknown operation")
    }
    return next, nil
}

Acceptance checklist

  • From {"theme":"dark","density":0,"enabled":false,"nested":{"x":1}}, keep preserves all; set string "0"/empty string preserves string types; set nil preserves a present null key; forget removes only theme. The input's outer map is unchanged. Missing versus null reads must use the comma-ok result.
  • Forget the sole key and require JSON {}, not []/null. Reject nil roots and unknown operations without persistence. Check stored invalid roots against the deliberately different corruption policy and preserve large numeric siblings with the app's decoder policy.
  • Do not mutate shared nested siblings concurrently. Run go test ./... and relevant race tests; include typed JSON decoding/encoding, defaults, output projection and unauthorized/cross-tenant cases.
  • With two real database sessions patch different keys: retain both writes or return an explicit stale-version error. Exercise cancellation, save failures, rollback and cache invalidation after commit.
  • Search all casts/path queries/collection consumers and module dependencies before removing anything. Record app/framework/driver versions and exact results; these checks require the destination app.

Sources

  • MIT reference: src/SchemalessAttributes.php, src/Casts/SchemalessAttributes.php, tests/HasSchemalessAttributesTest.php, LICENSE.md.
  • Framework source checked: db/tx.go and JSON support in the database package. Reconcile this unversioned source with the app's actual toolchain and lockfile.
  • Port options: check get-recipe's current port catalog (sourced from Package Port) at implementation time.