CodeSampleX

Exemplo

github.com/jackc/pgx/v5 v5.10.0: CopyFromFunc, CopyFromRows, CopyFromSlice

Amostra verificada para golang github.com/jackc/pgx/v5 v5.10.0: CopyFromFunc, CopyFromRows, CopyFromSlice. O contrato rodou em go 1.26 · linux alpine/x64 …

sha256:ef45ad1a3c1b03e5f41ea67ccb089e5d1e2e6c1d661f758b90a589250be2bc38

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
1
Chaves de assinatura que o compilaram
1
Ambiente declarado go 1.26 windows 11 x64 go 1.26 go go 1

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
go 1.26 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1
2026-08-19

Caso

HOW
Objetivo
verify pkg:golang/github.com/jackc/pgx/v5@v5.10.0
Pacotes
Símbolos
  • github.com/jackc/pgx/v5.CopyFromFunc
  • github.com/jackc/pgx/v5.CopyFromRows
  • github.com/jackc/pgx/v5.CopyFromSlice
  • github.com/jackc/pgx/v5.CopyFromSource
Ambiente
go 1.26.5
Criado
2026-08-18T21:25:39Z

Contrato

  1. pgx.CopyFromRows wraps raw row matrix into CopyFromSource with sequential Next and Values access
  2. pgx.CopyFromRows yields false on Next after all rows are consumed without errors
  3. pgx.CopyFromSlice generates row values from index generator function for specified length
  4. pgx.CopyFromSlice records error in Values and exposes it via Err when index generator fails
  5. pgx.CopyFromFunc streams row values sequentially until receiving nil row and nil error
  6. pgx.CopyFromFunc terminates iteration on Next and exposes error via Err when stream generator fails

Arquivos

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

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

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 pkg:golang/github.com/jackc/pgx/v5@v5.10.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/jackc/pgx/v5@v5.10.0
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@11.8.0
  - runtime: node@24.13.1

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:5f0375635e79de1407c6eb35323c5ab1b9a52370612f5db9a4fd65260b3c1083","contract":["pgx.CopyFromRows wraps raw row matrix into CopyFromSource with sequential Next and Values access","pgx.CopyFromRows yields false on Next after all rows are consumed without errors","pgx.CopyFromSlice generates row values from index generator function for specified length","pgx.CopyFromSlice records error in Values and exposes it via Err when index generator fails","pgx.CopyFromFunc streams row values sequentially until receiving nil row and nil error","pgx.CopyFromFunc terminates iteration on Next and exposes error via Err when stream generator fails"],"goal":"verify pkg:golang/github.com/jackc/pgx/v5@v5.10.0","kind":"HOW","packages":["pkg:golang/github.com/jackc/pgx/v5@v5.10.0"],"schemaVersion":1,"symbols":["github.com/jackc/pgx/v5.CopyFromFunc","github.com/jackc/pgx/v5.CopyFromRows","github.com/jackc/pgx/v5.CopyFromSlice","github.com/jackc/pgx/v5.CopyFromSource"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","ecosystem":"golang","language":"go","moduleSystem":"go","os":"windows","osVersionBucket":"11","packageManager":"go","packageManagerVersion":"1.26.5","runtime":"go","runtimeVersion":"1.26.5","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/jackc/pgx/v5@v5.10.0"],"schemaVersion":1,"symbols":["github.com/jackc/pgx/v5.CopyFromFunc","github.com/jackc/pgx/v5.CopyFromRows","github.com/jackc/pgx/v5.CopyFromSlice","github.com/jackc/pgx/v5.CopyFromSource"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.5

require (
	github.com/jackc/pgpassfile v1.0.0 // indirect
	github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
	github.com/jackc/pgx/v5 v5.10.0 // 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.10.0 h1:VhSvgU2jSli8o3AqIEOTJr7rZwAEUVo4E4XhR94Zfr0=
github.com/jackc/pgx/v5 v5.10.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=
main.go
package main

import (
	"fmt"

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

// User represents a sample record for bulk copy operations.
type User struct {
	ID   int
	Name string
	Role string
}

func main() {
	// 1. CopyFromRows: wrap 2D slice of values
	rawRows := [][]any{
		{1, "Alice", "admin"},
		{2, "Bob", "developer"},
	}
	sourceRows := pgx.CopyFromRows(rawRows)
	fmt.Println("Iterating pgx.CopyFromRows:")
	for sourceRows.Next() {
		values, err := sourceRows.Values()
		if err != nil {
			panic(err)
		}
		fmt.Printf("  row: %v\n", values)
	}

	// 2. CopyFromSlice: adapt typed slice with generator function
	users := []User{
		{ID: 3, Name: "Charlie", Role: "designer"},
		{ID: 4, Name: "Diana", Role: "tester"},
	}
	sourceSlice := pgx.CopyFromSlice(len(users), func(i int) ([]any, error) {
		return []any{users[i].ID, users[i].Name, users[i].Role}, nil
	})
	fmt.Println("Iterating pgx.CopyFromSlice:")
	for sourceSlice.Next() {
		values, err := sourceSlice.Values()
		if err != nil {
			panic(err)
		}
		fmt.Printf("  row: %v\n", values)
	}

	// 3. CopyFromFunc: dynamically stream rows until nil, nil
	counter := 5
	sourceFunc := pgx.CopyFromFunc(func() ([]any, error) {
		if counter > 6 {
			return nil, nil // signals end of stream
		}
		row := []any{counter, fmt.Sprintf("StreamUser%d", counter), "guest"}
		counter++
		return row, nil
	})
	fmt.Println("Iterating pgx.CopyFromFunc:")
	for sourceFunc.Next() {
		values, err := sourceFunc.Values()
		if err != nil {
			panic(err)
		}
		fmt.Printf("  row: %v\n", values)
	}
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/github.com/jackc/pgx/v5@v5.10.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/jackc/pgx/v5@v5.10.0"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@11.8.0",
    "runtime": "node@24.13.1"
  }
}
test/contract.go
package main

import (
	"errors"
	"fmt"
	"os"

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

type User struct {
	ID   int
	Name string
}

func main() {
	// Assertion 1: pgx.CopyFromRows wraps raw row matrix into CopyFromSource with sequential Next and Values access
	matrix := [][]any{
		{101, "Alpha"},
		{102, "Beta"},
	}
	srcRows := pgx.CopyFromRows(matrix)
	if !srcRows.Next() {
		fmt.Fprintf(os.Stderr, "FAIL: expected Next() to return true on first row\n")
		os.Exit(1)
	}
	v1, err := srcRows.Values()
	if err != nil || len(v1) != 2 || v1[0] != 101 || v1[1] != "Alpha" {
		fmt.Fprintf(os.Stderr, "FAIL: first row values mismatch: %v (err: %v)\n", v1, err)
		os.Exit(1)
	}
	if !srcRows.Next() {
		fmt.Fprintf(os.Stderr, "FAIL: expected Next() to return true on second row\n")
		os.Exit(1)
	}
	v2, err := srcRows.Values()
	if err != nil || len(v2) != 2 || v2[0] != 102 || v2[1] != "Beta" {
		fmt.Fprintf(os.Stderr, "FAIL: second row values mismatch: %v (err: %v)\n", v2, err)
		os.Exit(1)
	}

	// Assertion 2: pgx.CopyFromRows yields false on Next after all rows are consumed without errors
	if srcRows.Next() {
		fmt.Fprintf(os.Stderr, "FAIL: expected Next() to return false after matrix exhausted\n")
		os.Exit(1)
	}
	if srcRows.Err() != nil {
		fmt.Fprintf(os.Stderr, "FAIL: expected Err() to be nil, got: %v\n", srcRows.Err())
		os.Exit(1)
	}

	// Assertion 3: pgx.CopyFromSlice generates row values from index generator function for specified length
	users := []User{
		{ID: 201, Name: "Gamma"},
		{ID: 202, Name: "Delta"},
		{ID: 203, Name: "Epsilon"},
	}
	srcSlice := pgx.CopyFromSlice(len(users), func(i int) ([]any, error) {
		return []any{users[i].ID, users[i].Name}, nil
	})
	var sliceCollected [][]any
	for srcSlice.Next() {
		vals, err := srcSlice.Values()
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: CopyFromSlice Values returned unexpected error: %v\n", err)
			os.Exit(1)
		}
		sliceCollected = append(sliceCollected, vals)
	}
	if srcSlice.Err() != nil {
		fmt.Fprintf(os.Stderr, "FAIL: CopyFromSlice Err returned unexpected error: %v\n", srcSlice.Err())
		os.Exit(1)
	}
	if len(sliceCollected) != 3 || sliceCollected[0][0] != 201 || sliceCollected[2][1] != "Epsilon" {
		fmt.Fprintf(os.Stderr, "FAIL: CopyFromSlice collected data mismatch: %v\n", sliceCollected)
		os.Exit(1)
	}

	// Assertion 4: pgx.CopyFromSlice records error in Values and exposes it via Err when index generator fails
	genErr := errors.New("generator failed on index 2")
	srcSliceErr := pgx.CopyFromSlice(5, func(i int) ([]any, error) {
		if i == 2 {
			return nil, genErr
		}
		return []any{i, "name"}, nil
	})
	processed := 0
	for srcSliceErr.Next() {
		_, err := srcSliceErr.Values()
		if err != nil {
			break
		}
		processed++
	}
	if !errors.Is(srcSliceErr.Err(), genErr) {
		fmt.Fprintf(os.Stderr, "FAIL: expected generator error %v, got: %v\n", genErr, srcSliceErr.Err())
		os.Exit(1)
	}
	if processed != 2 {
		fmt.Fprintf(os.Stderr, "FAIL: expected 2 rows processed before error, got: %d\n", processed)
		os.Exit(1)
	}

	// Assertion 5: pgx.CopyFromFunc streams row values sequentially until receiving nil row and nil error
	streamCount := 0
	srcFunc := pgx.CopyFromFunc(func() ([]any, error) {
		if streamCount >= 3 {
			return nil, nil // signal end of stream
		}
		streamCount++
		return []any{streamCount, fmt.Sprintf("val%d", streamCount)}, nil
	})
	var funcCollected [][]any
	for srcFunc.Next() {
		vals, err := srcFunc.Values()
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: CopyFromFunc Values returned unexpected error: %v\n", err)
			os.Exit(1)
		}
		funcCollected = append(funcCollected, vals)
	}
	if srcFunc.Err() != nil {
		fmt.Fprintf(os.Stderr, "FAIL: CopyFromFunc Err returned unexpected error: %v\n", srcFunc.Err())
		os.Exit(1)
	}
	if len(funcCollected) != 3 || funcCollected[0][0] != 1 || funcCollected[2][0] != 3 {
		fmt.Fprintf(os.Stderr, "FAIL: CopyFromFunc collected data mismatch: %v\n", funcCollected)
		os.Exit(1)
	}

	// Assertion 6: pgx.CopyFromFunc terminates iteration on Next and exposes error via Err when stream generator fails
	streamErr := errors.New("stream producer error")
	srcFuncErr := pgx.CopyFromFunc(func() ([]any, error) {
		return nil, streamErr
	})
	if srcFuncErr.Next() {
		fmt.Fprintf(os.Stderr, "FAIL: CopyFromFunc Next returned true on immediate error\n")
		os.Exit(1)
	}
	if !errors.Is(srcFuncErr.Err(), streamErr) {
		fmt.Fprintf(os.Stderr, "FAIL: expected stream error %v, got: %v\n", streamErr, srcFuncErr.Err())
		os.Exit(1)
	}

	fmt.Println("PASS: all contract assertions passed for pkg:golang/github.com/jackc/pgx/v5@v5.10.0")
}

Seeder de origem

anônimo