fix(types): accept scalar values in jsonb cast/dump/load#118
Merged
Conversation
`cast(jsonb, ...)` only matched maps, lists, and binaries — any other
JSON-compatible scalar (integer, float, boolean) was rejected as
`cannot cast to jsonb`. That was inherited from a time when jsonb was
treated as "structured", but postgres accepts any valid JSON at the
root of a jsonb column, including scalars.
Add the missing clauses to `cast/2`, `dump/2`, and `load/2` so numbers
and booleans round-trip. `null` continues to land in the generic
`cast(_, null) -> {ok, undefined}` and `load(_, null) -> {ok, undefined}`
clauses higher up, keeping the kura convention of materialising SQL
NULLs as the `undefined` atom.
Found via barrow's bank-on-extract loop: scripts wanted
`game.storage.player_set(pid, "barrow", "run_loot", 5)` but had to
wrap as `{ n = 5 }` because the changeset cast rejected the scalar.
Pairs with the asobi_lua-side coercion fix in widgrensit/asobi_lua#46.
elvis.config was an untracked file in the worktree from an unrelated experiment that got swept into the jsonb commit via `git add -A`. It's not part of this PR's scope.
🟢 Code Coverage — 91.7%2175 of 2373 lines covered. ✅ ELP LintNo diagnostics. ℹ️ 11 OTP CVEs auto-ignored (already fixed in running version)These CVEs are patched in the installed OTP version but NVD data
|
3 tasks
Taure
added a commit
to widgrensit/asobi
that referenced
this pull request
May 14, 2026
Picks up Taure/kura#118 — `cast/dump/load(jsonb, ...)` now accept JSON scalars (integer/float/boolean) so callers no longer have to wrap them as `#{<<"n">> => N}` to land in a jsonb column.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
`cast(jsonb, ...)` only matched maps, lists, and binaries; any other JSON-compatible scalar (integer, float, boolean) was rejected as `cannot cast to jsonb`. Postgres jsonb accepts any valid JSON at the root of a jsonb column, including scalars, so this was an unnecessary restriction.
`null` continues to land in the generic `cast(, null) -> {ok, undefined}` and `load(, null) -> {ok, undefined}` clauses, preserving the existing convention that SQL NULL surfaces as the `undefined` atom.
Why
Found via barrow's bank-on-extract loop on top of asobi_lua. Scripts want to do:
```lua
game.storage.player_set(pid, "barrow", "run_loot", 5)
```
but currently have to wrap the integer as `{ n = 5 }` because the asobi_storage changeset rejects the scalar. The asobi_lua-side coercion was already widened in widgrensit/asobi_lua#46; this PR closes the kura-side gap so the wrapper can be dropped entirely.
Test plan
Downstream