샘플
github.com/jackc/pgx/v5 v5.11.0: StructArgs
검증된 샘플 — golang github.com/jackc/pgx/v5 v5.11.0: StructArgs. go 1.26 · linux debian/x64 · docker에서 contract를 실행해 통과했습니다: StructArgs converts struct value…
sha256:0a9b5ef7653d9e733760bb06526c81fab36bfeb4456a29845b1d13eb877f14af
이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다.
통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다.
MIT-0
실행 증거
선언된 환경과 서명된 실행을 분리해 두었습니다. 이 샘플이 무엇을 어디서 실행했는지 그대로 볼 수 있습니다.
- 증거 기준
- 서명된 컨트랙트 통과
- 검증 영수증
- 1
- 빌드한 서명 키
- 1
선언된 환경
go 1.26 linux 24 · ubuntu · glibc 2.39 x64 go 1.26 go go 1
검증 실행 환경
| 환경 | 컨트랙트 | 단계 | 실행일 |
|---|---|---|---|
| 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 |
케이스
HOW- 목표
- verify github.com/jackc/pgx/v5.StructArgs in pkg:golang/github.com/jackc/pgx/v5@v5.11.0
- 심벌
-
- github.com/jackc/pgx/v5.StructArgs
- 환경
- go 1.26.6
- 생성일
- 2026-09-08T20:46:01Z
컨트랙트
- 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
파일
- PROMPT.md
- csx.json
- go.mod
- go.sum
- sample.go
- spec.json
- test/contract.go
소스
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.
{"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"}
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
)
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=
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)
}
{
"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"
]
}
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.")
}
오리진 시더
익명