diff --git a/languages/golang/CHANGELOG.md b/languages/golang/CHANGELOG.md index bd7ebcd19..c56b720eb 100644 --- a/languages/golang/CHANGELOG.md +++ b/languages/golang/CHANGELOG.md @@ -13,6 +13,14 @@ 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 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 ### Upgrading from rc.6 diff --git a/languages/golang/cmd/stashgen/README.md b/languages/golang/cmd/stashgen/README.md index 5c4b1800a..da08a6709 100644 --- a/languages/golang/cmd/stashgen/README.md +++ b/languages/golang/cmd/stashgen/README.md @@ -339,6 +339,18 @@ 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. +`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. 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 6e67a1e8a..73a716b00 100644 --- a/languages/golang/encrypt/README.md +++ b/languages/golang/encrypt/README.md @@ -787,12 +787,55 @@ 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) +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 + if err := rows.Scan(&e); err != nil { + return err + } + 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 + if err := rows.Scan(&e); err != nil { + return err + } + picked = append(picked, e) +} +if err := rows.Err(); err != nil { + return err +} +plans, err := sel.Open(ctx, picked) // one encrypt.JSONValue for each user +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 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 +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 +887,34 @@ 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) +if err != nil { + return err +} +defer rows.Close() +var entries []eql.JSONEntry +for rows.Next() { + var e eql.JSONEntry + if err := rows.Scan(&e); err != nil { + return err + } + entries = append(entries, e) +} +if err := rows.Err(); err != nil { + return err +} // 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 f0e70ba84..994be226c 100644 --- a/languages/golang/encrypt/example/json/README.md +++ b/languages/golang/encrypt/example/json/README.md @@ -75,8 +75,30 @@ 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 +defer rows.Close() +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) +} +if err := rows.Err(); err != nil { + return fmt.Errorf("iterate visit entries: %w", err) +} +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