CodeSampleX

Beispiel

github.com/jackc/pgx/v5 v5.11.0: StructArgs

Verifiziertes Beispiel für golang github.com/jackc/pgx/v5 v5.11.0: StructArgs. Der Vertrag lief auf go 1.26 · linux debian/x64 · docker und bestand.

sha256:0a9b5ef7653d9e733760bb06526c81fab36bfeb4456a29845b1d13eb877f14af

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
1
Signaturschlüssel, die es gebaut haben
1
Deklarierte Umgebung go 1.26 linux 24 · ubuntu · glibc 2.39 x64 go 1.26 go go 1

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-09-08

Fall

HOW
Ziel
verify github.com/jackc/pgx/v5.StructArgs in pkg:golang/github.com/jackc/pgx/v5@v5.11.0
Pakete
Symbole
  • github.com/jackc/pgx/v5.StructArgs
Umgebung
go 1.26.6
Erstellt
2026-09-08T20:46:01Z

Contract

  1. StructArgs converts struct value into QueryRewriter using Go field names when db tag is absent
  2. StructArgs converts pointer to struct and maps db tag with comma-separated options to named query placeholders
  3. StructArgs ignores unexported fields, fields tagged with db:"-", and embedded anonymous structs
  4. StructArgs permits queries referencing a subset of struct fields without requiring all fields to be used
  5. StructArgs maps repeated named placeholders to the same positional argument ordinal
  6. StructArgs returns error QueryRewriter when passed nil, nil pointer, or non-struct value
  7. StructArgs returns error QueryRewriter when struct contains empty db tag or duplicate keys

Dateien

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • sample.go
  • spec.json
  • test/contract.go

Quellartefakt herunterladen (tar.gz)

Quelltext

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify github.com/jackc/pgx/v5.StructArgs in pkg:golang/github.com/jackc/pgx/v5@v5.11.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/jackc/pgx/v5@v5.11.0
Demonstrate these symbols/APIs:
  - github.com/jackc/pgx/v5.StructArgs

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:65502b6f8b31ac40886fcb7561ad16519d2f1b856d6e1d28c619aa06da24cdc4","contract":["StructArgs converts struct value into QueryRewriter using Go field names when db tag is absent","StructArgs converts pointer to struct and maps db tag with comma-separated options to named query placeholders","StructArgs ignores unexported fields, fields tagged with db:\"-\", and embedded anonymous structs","StructArgs permits queries referencing a subset of struct fields without requiring all fields to be used","StructArgs maps repeated named placeholders to the same positional argument ordinal","StructArgs returns error QueryRewriter when passed nil, nil pointer, or non-struct value","StructArgs returns error QueryRewriter when struct contains empty db tag or duplicate keys"],"goal":"verify github.com/jackc/pgx/v5.StructArgs in pkg:golang/github.com/jackc/pgx/v5@v5.11.0","kind":"HOW","packages":["pkg:golang/github.com/jackc/pgx/v5@v5.11.0"],"schemaVersion":1,"symbols":["github.com/jackc/pgx/v5.StructArgs"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","language":"go","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","packageManagerVersion":"1.26.6","runtime":"go","runtimeVersion":"1.26.6","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/jackc/pgx/v5@v5.11.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/jackc/pgx/v5@v5.11.0","symbols":["github.com/jackc/pgx/v5.StructArgs"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require github.com/jackc/pgx/v5 v5.11.0

require (
	github.com/jackc/pgpassfile v1.0.0 // indirect
	github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
	golang.org/x/text v0.29.0 // indirect
)
go.sum
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.11.0 h1:IzBBtyK9AHqf98cctWFifYSci2hgQR/cd56wB4p+ogg=
github.com/jackc/pgx/v5 v5.11.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk=
golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
sample.go
package sample

import (
	"context"
	"fmt"

	"github.com/jackc/pgx/v5"
)

// UserDefault demonstrates Go struct field names mapped directly to query parameters without db tags.
type UserDefault struct {
	UserID int64
	Role   string
}

// UserFilter demonstrates struct fields mapped to query parameters using db tags, comma options, and ignored fields.
type UserFilter struct {
	TenantID   int64  `db:"tenant_id"`
	Email      string `db:"email,omitempty"`
	Status     string `db:"status"`
	AuthToken  string `db:"-"`
	privateKey string
}

// RewriteStructQuery converts a struct or pointer to struct into a QueryRewriter using pgx.StructArgs and rewrites the query.
func RewriteStructQuery(ctx context.Context, sql string, criteria any) (string, []any, error) {
	if criteria == nil {
		return "", nil, fmt.Errorf("criteria cannot be nil")
	}
	rewriter := pgx.StructArgs(criteria)
	return rewriter.RewriteQuery(ctx, nil, sql, nil)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/jackc/pgx/v5.StructArgs in pkg:golang/github.com/jackc/pgx/v5@v5.11.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/jackc/pgx/v5@v5.11.0"
  ],
  "symbols": [
    "github.com/jackc/pgx/v5.StructArgs"
  ]
}
test/contract.go
package main

import (
	"context"
	"fmt"
	"os"
	"reflect"

	"github.com/jackc/pgx/v5"
	"sample"
)

func main() {
	ctx := context.Background()

	// 1. StructArgs converts struct value into QueryRewriter using Go field names when db tag is absent
	val1 := sample.UserDefault{
		UserID: 100,
		Role:   "admin",
	}
	rewriter1 := pgx.StructArgs(val1)
	if rewriter1 == nil {
		fmt.Fprintf(os.Stderr, "FAIL [1]: StructArgs returned nil rewriter\n")
		os.Exit(1)
	}
	sql1 := "SELECT * FROM users WHERE UserID = @UserID AND Role = @Role"
	newSQL1, newArgs1, err1 := rewriter1.RewriteQuery(ctx, nil, sql1, nil)
	if err1 != nil {
		fmt.Fprintf(os.Stderr, "FAIL [1]: unexpected error: %v\n", err1)
		os.Exit(1)
	}
	expectedSQL1 := "SELECT * FROM users WHERE UserID = $1 AND Role = $2"
	if newSQL1 != expectedSQL1 {
		fmt.Fprintf(os.Stderr, "FAIL [1]: SQL mismatch: want %q, got %q\n", expectedSQL1, newSQL1)
		os.Exit(1)
	}
	expectedArgs1 := []any{int64(100), "admin"}
	if !reflect.DeepEqual(newArgs1, expectedArgs1) {
		fmt.Fprintf(os.Stderr, "FAIL [1]: args mismatch: want %v, got %v\n", expectedArgs1, newArgs1)
		os.Exit(1)
	}

	// 2. StructArgs converts pointer to struct and maps db tag with comma-separated options to named query placeholders
	filter2 := &sample.UserFilter{
		TenantID: 42,
		Email:    "test@example.com",
		Status:   "active",
	}
	sql2 := "SELECT id FROM users WHERE tenant_id = @tenant_id AND email = @email AND status = @status"
	newSQL2, newArgs2, err2 := sample.RewriteStructQuery(ctx, sql2, filter2)
	if err2 != nil {
		fmt.Fprintf(os.Stderr, "FAIL [2]: unexpected error: %v\n", err2)
		os.Exit(1)
	}
	expectedSQL2 := "SELECT id FROM users WHERE tenant_id = $1 AND email = $2 AND status = $3"
	if newSQL2 != expectedSQL2 {
		fmt.Fprintf(os.Stderr, "FAIL [2]: SQL mismatch: want %q, got %q\n", expectedSQL2, newSQL2)
		os.Exit(1)
	}
	expectedArgs2 := []any{int64(42), "test@example.com", "active"}
	if !reflect.DeepEqual(newArgs2, expectedArgs2) {
		fmt.Fprintf(os.Stderr, "FAIL [2]: args mismatch: want %v, got %v\n", expectedArgs2, newArgs2)
		os.Exit(1)
	}

	// 3. StructArgs ignores unexported fields, fields tagged with db:"-", and embedded anonymous structs
	type Embedded struct {
		IgnoredEmbedded string `db:"ignored_embedded"`
	}
	type StructWithIgnored struct {
		Embedded
		Visible    string `db:"visible"`
		Skipped    string `db:"-"`
		unexported string
	}
	val3 := StructWithIgnored{
		Embedded:   Embedded{IgnoredEmbedded: "skip"},
		Visible:    "keep",
		Skipped:    "secret",
		unexported: "hidden",
	}
	rewriter3 := pgx.StructArgs(val3)
	_, newArgs3, err3 := rewriter3.RewriteQuery(ctx, nil, "SELECT @visible", nil)
	if err3 != nil || len(newArgs3) != 1 || newArgs3[0] != "keep" {
		fmt.Fprintf(os.Stderr, "FAIL [3]: failed to bind visible field: %v\n", err3)
		os.Exit(1)
	}
	_, ignoredArgs, _ := rewriter3.RewriteQuery(ctx, nil, "SELECT @Skipped, @unexported, @ignored_embedded", nil)
	for _, a := range ignoredArgs {
		if a != nil {
			fmt.Fprintf(os.Stderr, "FAIL [3]: expected nil for ignored field, got %v\n", a)
			os.Exit(1)
		}
	}

	// 4. StructArgs permits queries referencing a subset of struct fields without requiring all fields to be used
	subsetSQL := "SELECT id FROM users WHERE tenant_id = @tenant_id"
	newSQL4, newArgs4, err4 := sample.RewriteStructQuery(ctx, subsetSQL, filter2)
	if err4 != nil {
		fmt.Fprintf(os.Stderr, "FAIL [4]: unexpected error on subset query: %v\n", err4)
		os.Exit(1)
	}
	if newSQL4 != "SELECT id FROM users WHERE tenant_id = $1" {
		fmt.Fprintf(os.Stderr, "FAIL [4]: SQL mismatch: got %q\n", newSQL4)
		os.Exit(1)
	}
	if len(newArgs4) != 1 || newArgs4[0] != int64(42) {
		fmt.Fprintf(os.Stderr, "FAIL [4]: args mismatch: got %v\n", newArgs4)
		os.Exit(1)
	}

	// 5. StructArgs maps repeated named placeholders to the same positional argument ordinal
	repeatSQL := "SELECT * FROM users WHERE tenant_id = @tenant_id OR owner_tenant_id = @tenant_id"
	newSQL5, newArgs5, err5 := sample.RewriteStructQuery(ctx, repeatSQL, filter2)
	if err5 != nil {
		fmt.Fprintf(os.Stderr, "FAIL [5]: unexpected error on repeated placeholder: %v\n", err5)
		os.Exit(1)
	}
	expectedSQL5 := "SELECT * FROM users WHERE tenant_id = $1 OR owner_tenant_id = $1"
	if newSQL5 != expectedSQL5 {
		fmt.Fprintf(os.Stderr, "FAIL [5]: SQL mismatch: want %q, got %q\n", expectedSQL5, newSQL5)
		os.Exit(1)
	}
	if len(newArgs5) != 1 || newArgs5[0] != int64(42) {
		fmt.Fprintf(os.Stderr, "FAIL [5]: args mismatch: want [42], got %v\n", newArgs5)
		os.Exit(1)
	}

	// 6. StructArgs returns error QueryRewriter when passed nil, nil pointer, or non-struct value
	rewriterNil := pgx.StructArgs(nil)
	if _, _, errNil := rewriterNil.RewriteQuery(ctx, nil, "SELECT 1", nil); errNil == nil {
		fmt.Fprintf(os.Stderr, "FAIL [6]: expected error for nil input, got nil\n")
		os.Exit(1)
	}
	var nilPtr *sample.UserFilter
	rewriterNilPtr := pgx.StructArgs(nilPtr)
	if _, _, errNilPtr := rewriterNilPtr.RewriteQuery(ctx, nil, "SELECT 1", nil); errNilPtr == nil {
		fmt.Fprintf(os.Stderr, "FAIL [6]: expected error for nil pointer, got nil\n")
		os.Exit(1)
	}
	rewriterNonStruct := pgx.StructArgs(12345)
	if _, _, errNonStruct := rewriterNonStruct.RewriteQuery(ctx, nil, "SELECT 1", nil); errNonStruct == nil {
		fmt.Fprintf(os.Stderr, "FAIL [6]: expected error for non-struct input, got nil\n")
		os.Exit(1)
	}

	// 7. StructArgs returns error QueryRewriter when struct contains empty db tag or duplicate keys
	type EmptyTagStruct struct {
		Field string `db:""`
	}
	rewriterEmptyTag := pgx.StructArgs(EmptyTagStruct{Field: "bad"})
	if _, _, errEmptyTag := rewriterEmptyTag.RewriteQuery(ctx, nil, "SELECT 1", nil); errEmptyTag == nil {
		fmt.Fprintf(os.Stderr, "FAIL [7]: expected error for empty db tag, got nil\n")
		os.Exit(1)
	}

	type DuplicateKeyStruct struct {
		FieldA string `db:"same_key"`
		FieldB string `db:"same_key"`
	}
	rewriterDup := pgx.StructArgs(DuplicateKeyStruct{FieldA: "a", FieldB: "b"})
	if _, _, errDup := rewriterDup.RewriteQuery(ctx, nil, "SELECT 1", nil); errDup == nil {
		fmt.Fprintf(os.Stderr, "FAIL [7]: expected error for duplicate key, got nil\n")
		os.Exit(1)
	}

	fmt.Println("PASS: all pgx.StructArgs contract assertions verified successfully.")
}

Ursprungs-Seeder

anonym