CodeSampleX

Ejemplo

github.com/spf13/cast v1.10.0: ToInt

Muestra verificada para golang github.com/spf13/cast v1.10.0: ToInt. El contrato se ejecutó en go 1.26 · linux debian/x64 · docker y pasó.

sha256:ae4c33c6f55347312c95d3a6f33bb549662141519ca4ed918e6dcb265fc7c8cb

Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido. Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas. MIT-0

Evidencia de ejecución

El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.

Base de evidencia
Contrato firmado aprobado
Recibos de verificación
1
Claves de firma que lo compilaron
1
Entorno declarado linux 24 · ubuntu · glibc 2.39 x64 go

Entornos de las ejecuciones de verificación

Entorno Contrato Etapas Ejecución
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-06

Caso

HOW
Objetivo
verify pkg:golang/github.com/spf13/cast@v1.10.0
Paquetes
Símbolos
  • github.com/spf13/cast.ToInt
Creado
2026-09-06T16:26:49Z

Contrato

  1. cast.ToInt converts integer, unsigned integer, and floating-point numeric values to int
  2. cast.ToInt parses decimal and signed numeric strings into int
  3. cast.ToInt converts boolean values to 1 for true and 0 for false
  4. cast.ToInt returns 0 for nil values
  5. cast.ToInt unwraps pointer values before converting their underlying values to int
  6. cast.ToInt returns 0 and cast.ToIntE returns an error for invalid numeric strings and unsupported types
  7. ToInt converts the given value to an int using cast.ToInt

Archivos

  • PROMPT.md
  • cast_int.go
  • contract_test.go
  • csx.json
  • go.mod
  • go.sum
  • spec.json

Descargar el artefacto de código fuente (tar.gz)

Código fuente

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/spf13/cast@v1.10.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/spf13/cast@v1.10.0

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.
cast_int.go
package sample

import (
	"github.com/spf13/cast"
)

// ToInt converts an arbitrary value into an int using cast.ToInt.
// It returns 0 if conversion cannot be performed.
func ToInt(v any) int {
	return cast.ToInt(v)
}

// ToIntE converts an arbitrary value into an int using cast.ToIntE,
// returning an error if conversion cannot be performed.
func ToIntE(v any) (int, error) {
	return cast.ToIntE(v)
}
contract_test.go
package sample

import (
	"testing"

	"github.com/spf13/cast"
)

func TestToInt_NumericValues(t *testing.T) {
	if got := cast.ToInt(42); got != 42 {
		t.Fatalf("cast.ToInt(42) = %d; want 42", got)
	}
	if got := cast.ToInt(int8(-8)); got != -8 {
		t.Fatalf("cast.ToInt(int8(-8)) = %d; want -8", got)
	}
	if got := cast.ToInt(int16(-16)); got != -16 {
		t.Fatalf("cast.ToInt(int16(-16)) = %d; want -16", got)
	}
	if got := cast.ToInt(int32(-32)); got != -32 {
		t.Fatalf("cast.ToInt(int32(-32)) = %d; want -32", got)
	}
	if got := cast.ToInt(int64(-64)); got != -64 {
		t.Fatalf("cast.ToInt(int64(-64)) = %d; want -64", got)
	}
	if got := cast.ToInt(uint(100)); got != 100 {
		t.Fatalf("cast.ToInt(uint(100)) = %d; want 100", got)
	}
	if got := cast.ToInt(uint8(200)); got != 200 {
		t.Fatalf("cast.ToInt(uint8(200)) = %d; want 200", got)
	}
	if got := cast.ToInt(uint16(300)); got != 300 {
		t.Fatalf("cast.ToInt(uint16(300)) = %d; want 300", got)
	}
	if got := cast.ToInt(uint32(400)); got != 400 {
		t.Fatalf("cast.ToInt(uint32(400)) = %d; want 400", got)
	}
	if got := cast.ToInt(uint64(500)); got != 500 {
		t.Fatalf("cast.ToInt(uint64(500)) = %d; want 500", got)
	}
	if got := cast.ToInt(3.14); got != 3 {
		t.Fatalf("cast.ToInt(3.14) = %d; want 3", got)
	}
	if got := cast.ToInt(float32(9.99)); got != 9 {
		t.Fatalf("cast.ToInt(float32(9.99)) = %d; want 9", got)
	}
}

func TestToInt_Strings(t *testing.T) {
	if got := cast.ToInt("12345"); got != 12345 {
		t.Fatalf("cast.ToInt(\"12345\") = %d; want 12345", got)
	}
	if got := cast.ToInt("-6789"); got != -6789 {
		t.Fatalf("cast.ToInt(\"-6789\") = %d; want -6789", got)
	}
	if got := cast.ToInt("0"); got != 0 {
		t.Fatalf("cast.ToInt(\"0\") = %d; want 0", got)
	}
}

func TestToInt_Booleans(t *testing.T) {
	if got := cast.ToInt(true); got != 1 {
		t.Fatalf("cast.ToInt(true) = %d; want 1", got)
	}
	if got := cast.ToInt(false); got != 0 {
		t.Fatalf("cast.ToInt(false) = %d; want 0", got)
	}
}

func TestToInt_Nil(t *testing.T) {
	if got := cast.ToInt(nil); got != 0 {
		t.Fatalf("cast.ToInt(nil) = %d; want 0", got)
	}
	val, err := cast.ToIntE(nil)
	if err != nil || val != 0 {
		t.Fatalf("cast.ToIntE(nil) = (%d, %v); want (0, nil)", val, err)
	}
}

func TestToInt_Pointers(t *testing.T) {
	n := 777
	if got := cast.ToInt(&n); got != 777 {
		t.Fatalf("cast.ToInt(&n) = %d; want 777", got)
	}
	s := "888"
	if got := cast.ToInt(&s); got != 888 {
		t.Fatalf("cast.ToInt(&s) = %d; want 888", got)
	}
	b := true
	if got := cast.ToInt(&b); got != 1 {
		t.Fatalf("cast.ToInt(&b) = %d; want 1", got)
	}
}

func TestToInt_InvalidAndUnsupported(t *testing.T) {
	invalidCases := []any{
		"not_a_number",
		[]int{1, 2, 3},
		map[string]int{"a": 1},
		struct{}{},
	}
	for _, c := range invalidCases {
		if got := cast.ToInt(c); got != 0 {
			t.Fatalf("cast.ToInt(%v) = %d; want 0", c, got)
		}
		if _, err := cast.ToIntE(c); err == nil {
			t.Fatalf("cast.ToIntE(%v) expected error, got nil", c)
		}
	}
}

func TestToInt_Helper(t *testing.T) {
	cases := []struct {
		input    any
		expected int
	}{
		{"42", 42},
		{-100, -100},
		{true, 1},
		{false, 0},
		{nil, 0},
		{3.14, 3},
	}
	for _, c := range cases {
		if got := ToInt(c.input); got != c.expected {
			t.Fatalf("ToInt(%v) = %d; want %d", c.input, got, c.expected)
		}
		val, err := ToIntE(c.input)
		if err != nil || val != c.expected {
			t.Fatalf("ToIntE(%v) = (%d, %v); want (%d, nil)", c.input, val, err, c.expected)
		}
	}
}
csx.json
{"case":{"caseId":"case:sha256:7cbcd21a788a6ae87d25cd8c458838d2411d5b7c6d15c143ccb787567d9f3cd7","contract":["cast.ToInt converts integer, unsigned integer, and floating-point numeric values to int","cast.ToInt parses decimal and signed numeric strings into int","cast.ToInt converts boolean values to 1 for true and 0 for false","cast.ToInt returns 0 for nil values","cast.ToInt unwraps pointer values before converting their underlying values to int","cast.ToInt returns 0 and cast.ToIntE returns an error for invalid numeric strings and unsupported types","ToInt converts the given value to an int using cast.ToInt"],"goal":"verify pkg:golang/github.com/spf13/cast@v1.10.0","kind":"HOW","packages":["pkg:golang/github.com/spf13/cast@v1.10.0"],"schemaVersion":1,"symbols":["github.com/spf13/cast.ToInt"]},"contractCommand":["go","test","./..."],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/spf13/cast@v1.10.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/spf13/cast@v1.10.0","symbols":["github.com/spf13/cast.ToInt"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require github.com/spf13/cast v1.10.0
go.sum
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY=
github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/github.com/spf13/cast@v1.10.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/spf13/cast@v1.10.0"
  ]
}

Seeder de origen

anónimo