From 83153f2726cb01f5c8c29420716ad5b54808f73f Mon Sep 17 00:00:00 2001 From: Lindsay Holmwood Date: Fri, 27 Nov 2026 03:16:54 +1100 Subject: [PATCH 1/2] docs(golang): show the Go type to scan a JSON entry into No README named the Go type a returned JSON entry scans into, so people had to read the generated code to find it. The reads return the SQL type `public.eql_v4_json_entry`. It scans into `eql.JSONEntry`. The `->` operator returns SQL NULL for a row with no node at the path. So its result scans into a `*eql.JSONEntry`, and `JSONSelection.Open` takes a `[]*eql.JSONEntry`. The encrypt README's subtree and children samples now run their query and scan each row. A new Selector sample shows the pointer scan. The stashgen README lists what each Open takes. The JSON example's README shows the scan loop from main.go. The README samples were compiled in a throwaway package against the example's generated code. Refs #1888 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01CtEQzxj55RUn35dkbhdwQk --- languages/golang/CHANGELOG.md | 10 ++++ languages/golang/cmd/stashgen/README.md | 11 +++++ languages/golang/encrypt/README.md | 49 +++++++++++++++++-- .../golang/encrypt/example/json/README.md | 22 ++++++++- 4 files changed, 85 insertions(+), 7 deletions(-) diff --git a/languages/golang/CHANGELOG.md b/languages/golang/CHANGELOG.md index bd7ebcd199..afaf56fd6d 100644 --- a/languages/golang/CHANGELOG.md +++ b/languages/golang/CHANGELOG.md @@ -13,6 +13,16 @@ and `languages/golang/encrypt/encrypttest/vX.Y.Z` on the same commit. ## [Unreleased] +### Fixed + +- The JSON read samples show the Go type to scan each returned entry into. + This change is to the documentation only. Scan each entry into an + `eql.JSONEntry`. `->` returns SQL `NULL` for a row with no node at the + path, so scan its result into a `*eql.JSONEntry`. The `encrypt` README's + subtree and children samples now run their queries and scan the rows. It + also has a new `Selector` sample. The `stashgen` README lists what each + `Open` takes. The JSON example's README shows its scan loop. + ## [0.1.0-rc.7] - 2026-11-25 ### Upgrading from rc.6 diff --git a/languages/golang/cmd/stashgen/README.md b/languages/golang/cmd/stashgen/README.md index 5c4b1800a4..f9f85fd178 100644 --- a/languages/golang/cmd/stashgen/README.md +++ b/languages/golang/cmd/stashgen/README.md @@ -339,6 +339,17 @@ Give a needle struct pointer fields with `omitempty`, as `ProfileNeedle` above, `Contains` refuses a needle of the field's own type when that type holds a struct, since that is the likeliest way to make the mistake; a type defined over it (`type wholeProfile Profile`) states that you mean every field. That includes a slice or map of structs: a `[]Visit` document refuses a `[]Visit` needle, since each `Visit` writes its zero fields; pass a `[]VisitNeedle`, a slice of a needle type with pointer fields and `omitempty`. `ContainedBy` takes the field's own type: there a zero field widens the query. + +The database returns each entry of a selection or read as the SQL type `public.eql_v4_json_entry`. +Scan each entry into an `eql.JSONEntry`. +`->` returns SQL `NULL` for a row with no node at the path. +So scan the result of `->` into a `*eql.JSONEntry`. + +`JSONSelection.Open` takes a `[]*eql.JSONEntry`, one entry for each row. +A `nil` entry opens to no value. +`JSONRead.Open` takes a `[][]eql.JSONEntry`, one group of entries for each row. +`JSONChildren.Open` takes a `[]eql.JSONEntry`. + Each value a selection or read opens is JSON text; `encrypt.DecodeJSON[T]` reads one into a Go type, with the same rules as a whole document. A position in a path is refused for `EqAt` and `Selector`, whose selector reads every position as the wildcard; read a position with `Subtree`. diff --git a/languages/golang/encrypt/README.md b/languages/golang/encrypt/README.md index 6e67a1e8a8..b5cbfbdbc8 100644 --- a/languages/golang/encrypt/README.md +++ b/languages/golang/encrypt/README.md @@ -787,12 +787,38 @@ q, err := users.Fields.Profile.Contains(ctx, cipher, map[string]any{"plan": "pro rows, err := db.QueryContext(ctx, `SELECT id FROM users WHERE profile @> $1::eql_v4.query_json`, q) read, err := users.Fields.Profile.Subtree(ctx, cipher, "$.visits[*].at", encrypt.JSONReadOptions{}) -// SELECT e FROM users, eql_v4.json_tree_subtree(profile, $1::text[], 64) AS e WHERE id = $2 -// with read.Tokens() as $1; collect each row's entries, then: -values, err := read.Open(ctx, entriesPerRow) +rows, err = db.QueryContext(ctx, `SELECT e FROM users, eql_v4.json_tree_subtree(profile, $1::text[], 64) AS e WHERE id = $2`, read.Tokens(), id) +var entries []eql.JSONEntry // one user's entries +for rows.Next() { + var e eql.JSONEntry // each row is one public.eql_v4_json_entry + if err := rows.Scan(&e); err != nil { + return err + } + entries = append(entries, e) +} +values, err := read.Open(ctx, [][]eql.JSONEntry{entries}) // one []eql.JSONEntry for each user at, err := encrypt.DecodeJSON[int64](values[0][0]) + +sel, err := users.Fields.Profile.Selector(ctx, cipher, "$.plan") +rows, err = db.QueryContext(ctx, `SELECT profile -> $1::text FROM users ORDER BY id`, sel.Selector()) +var picked []*eql.JSONEntry +for rows.Next() { + var e *eql.JSONEntry // nil where the user's document has no $.plan + if err := rows.Scan(&e); err != nil { + return err + } + picked = append(picked, e) +} +plans, err := sel.Open(ctx, picked) // one encrypt.JSONValue for each user +plan, err := encrypt.DecodeJSON[string](plans[0].JSON) ``` +Each read returns encrypted entries of the SQL type +`public.eql_v4_json_entry`. Scan each one into an `eql.JSONEntry`, from +package `encrypt/eql`. `->` returns SQL `NULL` for a row whose document has +no node at the path. So scan its result into a `*eql.JSONEntry`. `Open` then +reports that row as a `JSONValue` whose `Found` is false. + A needle or a value is a `json.RawMessage` of JSON text, or any Go value `encoding/json` writes. A document contains a needle when it holds every member the needle writes, and `encoding/json` writes a zero struct field @@ -844,14 +870,27 @@ whole subtree. It takes three steps. // Step 1: derive the parent's token. children, err := users.Fields.Profile.Children(ctx, cipher, "$.address") -// Step 2: run this query with children.Token() as $1, and collect every entry. -// SELECT e FROM users, eql_v4.json_tree_children(profile, $1::text) AS e WHERE id = $2 +// Step 2: run the query, and scan each entry into an eql.JSONEntry. +rows, err := db.QueryContext(ctx, `SELECT e FROM users, eql_v4.json_tree_children(profile, $1::text) AS e WHERE id = $2`, children.Token(), id) +var entries []eql.JSONEntry +for rows.Next() { + var e eql.JSONEntry + if err := rows.Scan(&e); err != nil { + return err + } + entries = append(entries, e) +} // Step 3: decrypt the entries. members, err := children.Open(ctx, entries) +address := make(map[string]string) for _, member := range members { key, _ := member.Key() // "city", "street", ... value, err := encrypt.DecodeJSON[string](member.JSON) + if err != nil { + return err + } + address[key] = value } ``` diff --git a/languages/golang/encrypt/example/json/README.md b/languages/golang/encrypt/example/json/README.md index f0e70ba842..8589607f62 100644 --- a/languages/golang/encrypt/example/json/README.md +++ b/languages/golang/encrypt/example/json/README.md @@ -75,8 +75,26 @@ compares case- and accent-folded. A row with more visits than `JSONReadOptions.WildcardBound` (64 by default) fails the query with SQLSTATE 22023. A containment needle made from a struct includes every field without `omitempty`, including zero values; -`main.go` uses a map to name only the member being queried. The prepared -subtree read returns JSON text, which `encrypt.DecodeJSON[int64]` decodes. +`main.go` uses a map to name only the member being queried. + +The prepared subtree read returns one encrypted entry in each database row. +`main.go` scans each entry into an `eql.JSONEntry`. `Open` takes a +`[][]eql.JSONEntry`, one group of entries for each customer. `main.go` reads +one customer, so it passes one group: + +```go +var entries []eql.JSONEntry +for rows.Next() { + var entry eql.JSONEntry + if err := rows.Scan(&entry); err != nil { + return fmt.Errorf("scan visit entry: %w", err) + } + entries = append(entries, entry) +} +values, err := read.Open(ctx, [][]eql.JSONEntry{entries}) +``` + +`Open` returns JSON text. `encrypt.DecodeJSON[int64]` decodes each value. The example's `SELECT` statements cast parameters to EQL's operand types. The age comparison uses the selector derived from the same ordered path as From ab9b56bf727786b8992cf83e51c63aca5198f511 Mon Sep 17 00:00:00 2001 From: Lindsay Holmwood Date: Fri, 27 Nov 2026 03:34:10 +1100 Subject: [PATCH 2/2] docs(golang): check errors and Found in the JSON read samples Readers copy these samples. A failed query returns nil rows, so the next rows.Next call panics. A database error during iteration ends the loop early, and Open accepts the partial entries without an error. A user with no node at the path opens to a JSONValue that is not Found, and DecodeJSON refuses its empty JSON. The samples now check each query's error, close the rows, check rows.Err before Open, and decode a selection only when it is Found. The README says "selection or read" for the two kinds of request. The stashgen README also names what JSONRead.OpenOne takes. The CHANGELOG entry is shorter and cites the issue. Refs #1888 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01CtEQzxj55RUn35dkbhdwQk --- languages/golang/CHANGELOG.md | 10 +++---- languages/golang/cmd/stashgen/README.md | 1 + languages/golang/encrypt/README.md | 28 +++++++++++++++++-- .../golang/encrypt/example/json/README.md | 4 +++ 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/languages/golang/CHANGELOG.md b/languages/golang/CHANGELOG.md index afaf56fd6d..c56b720eb7 100644 --- a/languages/golang/CHANGELOG.md +++ b/languages/golang/CHANGELOG.md @@ -15,12 +15,10 @@ and `languages/golang/encrypt/encrypttest/vX.Y.Z` on the same commit. ### Fixed -- The JSON read samples show the Go type to scan each returned entry into. - This change is to the documentation only. Scan each entry into an - `eql.JSONEntry`. `->` returns SQL `NULL` for a row with no node at the - path, so scan its result into a `*eql.JSONEntry`. The `encrypt` README's - subtree and children samples now run their queries and scan the rows. It - also has a new `Selector` sample. The `stashgen` README lists what each +- **The JSON read samples show the Go type to scan each entry into** + (#1888). This change is to the documentation only. The `encrypt` README's + subtree and children samples now run their queries and scan the rows. A + new `Selector` sample is added there too. The `stashgen` README lists what each `Open` takes. The JSON example's README shows its scan loop. ## [0.1.0-rc.7] - 2026-11-25 diff --git a/languages/golang/cmd/stashgen/README.md b/languages/golang/cmd/stashgen/README.md index f9f85fd178..da08a67097 100644 --- a/languages/golang/cmd/stashgen/README.md +++ b/languages/golang/cmd/stashgen/README.md @@ -348,6 +348,7 @@ So scan the result of `->` into a `*eql.JSONEntry`. `JSONSelection.Open` takes a `[]*eql.JSONEntry`, one entry for each row. A `nil` entry opens to no value. `JSONRead.Open` takes a `[][]eql.JSONEntry`, one group of entries for each row. +`JSONRead.OpenOne` takes one row's `[]eql.JSONEntry`. `JSONChildren.Open` takes a `[]eql.JSONEntry`. Each value a selection or read opens is JSON text; `encrypt.DecodeJSON[T]` reads one into a Go type, with the same rules as a whole document. diff --git a/languages/golang/encrypt/README.md b/languages/golang/encrypt/README.md index b5cbfbdbc8..73a716b00e 100644 --- a/languages/golang/encrypt/README.md +++ b/languages/golang/encrypt/README.md @@ -788,6 +788,10 @@ rows, err := db.QueryContext(ctx, `SELECT id FROM users WHERE profile @> $1::eql read, err := users.Fields.Profile.Subtree(ctx, cipher, "$.visits[*].at", encrypt.JSONReadOptions{}) rows, err = db.QueryContext(ctx, `SELECT e FROM users, eql_v4.json_tree_subtree(profile, $1::text[], 64) AS e WHERE id = $2`, read.Tokens(), id) +if err != nil { + return err +} +defer rows.Close() var entries []eql.JSONEntry // one user's entries for rows.Next() { var e eql.JSONEntry // each row is one public.eql_v4_json_entry @@ -796,11 +800,18 @@ for rows.Next() { } entries = append(entries, e) } +if err := rows.Err(); err != nil { + return err +} values, err := read.Open(ctx, [][]eql.JSONEntry{entries}) // one []eql.JSONEntry for each user at, err := encrypt.DecodeJSON[int64](values[0][0]) sel, err := users.Fields.Profile.Selector(ctx, cipher, "$.plan") rows, err = db.QueryContext(ctx, `SELECT profile -> $1::text FROM users ORDER BY id`, sel.Selector()) +if err != nil { + return err +} +defer rows.Close() var picked []*eql.JSONEntry for rows.Next() { var e *eql.JSONEntry // nil where the user's document has no $.plan @@ -809,11 +820,17 @@ for rows.Next() { } picked = append(picked, e) } +if err := rows.Err(); err != nil { + return err +} plans, err := sel.Open(ctx, picked) // one encrypt.JSONValue for each user -plan, err := encrypt.DecodeJSON[string](plans[0].JSON) +var plan string +if plans[0].Found { // false where the user's document has no $.plan + plan, err = encrypt.DecodeJSON[string](plans[0].JSON) +} ``` -Each read returns encrypted entries of the SQL type +Each selection or read returns encrypted entries of the SQL type `public.eql_v4_json_entry`. Scan each one into an `eql.JSONEntry`, from package `encrypt/eql`. `->` returns SQL `NULL` for a row whose document has no node at the path. So scan its result into a `*eql.JSONEntry`. `Open` then @@ -872,6 +889,10 @@ children, err := users.Fields.Profile.Children(ctx, cipher, "$.address") // Step 2: run the query, and scan each entry into an eql.JSONEntry. rows, err := db.QueryContext(ctx, `SELECT e FROM users, eql_v4.json_tree_children(profile, $1::text) AS e WHERE id = $2`, children.Token(), id) +if err != nil { + return err +} +defer rows.Close() var entries []eql.JSONEntry for rows.Next() { var e eql.JSONEntry @@ -880,6 +901,9 @@ for rows.Next() { } entries = append(entries, e) } +if err := rows.Err(); err != nil { + return err +} // Step 3: decrypt the entries. members, err := children.Open(ctx, entries) diff --git a/languages/golang/encrypt/example/json/README.md b/languages/golang/encrypt/example/json/README.md index 8589607f62..994be226c6 100644 --- a/languages/golang/encrypt/example/json/README.md +++ b/languages/golang/encrypt/example/json/README.md @@ -83,6 +83,7 @@ The prepared subtree read returns one encrypted entry in each database row. one customer, so it passes one group: ```go +defer rows.Close() var entries []eql.JSONEntry for rows.Next() { var entry eql.JSONEntry @@ -91,6 +92,9 @@ for rows.Next() { } entries = append(entries, entry) } +if err := rows.Err(); err != nil { + return fmt.Errorf("iterate visit entries: %w", err) +} values, err := read.Open(ctx, [][]eql.JSONEntry{entries}) ```