CodeSampleX

Ejemplo

github.com/spf13/cast v1.3.0: ToString, ToStringE, ToInt

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

sha256:479b327b7e7fc430d4f21258e3910d93bbb8bbf2f8749ba38f562e9ebc494598

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-10-27

Caso

HOW
Objetivo
verify pkg:golang/github.com/spf13/cast@v1.3.0
Paquetes
Símbolos
  • cast.ToString
  • cast.ToStringE
  • cast.ToInt
  • cast.ToIntE
  • cast.ToInt64
  • cast.ToInt64E
  • cast.ToBool
  • cast.ToBoolE
  • cast.ToFloat64
  • cast.ToFloat64E
  • cast.ToDuration
  • cast.ToDurationE
  • cast.ToStringSlice
  • cast.ToStringSliceE
  • cast.ToStringMapString
  • cast.ToStringMapStringE
  • cast.ToTime
  • cast.ToTimeE
Creado
2026-10-27T17:06:17Z

Contrato

  1. cast.ToString and cast.ToStringE convert numeric values, booleans, and byte slices to string representations
  2. cast.ToInt and cast.ToIntE safely parse numeric strings, booleans, and floats into integer values
  3. cast.ToInt64 and cast.ToInt64E convert large integer strings and booleans to int64 values
  4. cast.ToBool and cast.ToBoolE parse string and numeric representations into boolean values
  5. cast.ToFloat64 and cast.ToFloat64E convert numeric strings and integer values to float64
  6. cast.ToDuration and cast.ToDurationE parse formatted duration strings and numeric nanoseconds into time.Duration
  7. cast.ToStringSlice and cast.ToStringSliceE convert []interface{} slices and whitespace-separated strings into []string
  8. cast.ToStringMapString and cast.ToStringMapStringE convert map structures with arbitrary value types into map[string]string
  9. cast.ToTime and cast.ToTimeE parse RFC3339 formatted date/time strings into time.Time

Archivos

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

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.3.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/spf13/cast@v1.3.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_sample.go
package sample

import (
	"time"

	"github.com/spf13/cast"
)

// FormatAsString converts an arbitrary value to string, returning an error if conversion fails.
func FormatAsString(val interface{}) (string, error) {
	return cast.ToStringE(val)
}

// CoerceString converts an arbitrary value to string or returns empty string on failure.
func CoerceString(val interface{}) string {
	return cast.ToString(val)
}

// ParseInt converts an arbitrary value to int, returning an error if conversion fails.
func ParseInt(val interface{}) (int, error) {
	return cast.ToIntE(val)
}

// CoerceInt converts an arbitrary value to int or returns 0 on failure.
func CoerceInt(val interface{}) int {
	return cast.ToInt(val)
}

// ParseInt64 converts an arbitrary value to int64, returning an error if conversion fails.
func ParseInt64(val interface{}) (int64, error) {
	return cast.ToInt64E(val)
}

// CoerceInt64 converts an arbitrary value to int64 or returns 0 on failure.
func CoerceInt64(val interface{}) int64 {
	return cast.ToInt64(val)
}

// ParseBool converts an arbitrary value to bool, returning an error if conversion fails.
func ParseBool(val interface{}) (bool, error) {
	return cast.ToBoolE(val)
}

// CoerceBool converts an arbitrary value to bool or returns false on failure.
func CoerceBool(val interface{}) bool {
	return cast.ToBool(val)
}

// ParseFloat64 converts an arbitrary value to float64, returning an error if conversion fails.
func ParseFloat64(val interface{}) (float64, error) {
	return cast.ToFloat64E(val)
}

// CoerceFloat64 converts an arbitrary value to float64 or returns 0.0 on failure.
func CoerceFloat64(val interface{}) float64 {
	return cast.ToFloat64(val)
}

// ParseDuration converts an arbitrary value to time.Duration, returning an error if conversion fails.
func ParseDuration(val interface{}) (time.Duration, error) {
	return cast.ToDurationE(val)
}

// CoerceDuration converts an arbitrary value to time.Duration or returns 0 on failure.
func CoerceDuration(val interface{}) time.Duration {
	return cast.ToDuration(val)
}

// ParseStringSlice converts a slice value to []string, returning an error if conversion fails.
func ParseStringSlice(val interface{}) ([]string, error) {
	return cast.ToStringSliceE(val)
}

// CoerceStringSlice converts a slice value to []string or returns empty slice on failure.
func CoerceStringSlice(val interface{}) []string {
	return cast.ToStringSlice(val)
}

// ParseStringMapString converts a map value to map[string]string, returning an error if conversion fails.
func ParseStringMapString(val interface{}) (map[string]string, error) {
	return cast.ToStringMapStringE(val)
}

// CoerceStringMapString converts a map value to map[string]string or returns empty map on failure.
func CoerceStringMapString(val interface{}) map[string]string {
	return cast.ToStringMapString(val)
}

// ParseTime converts a value to time.Time, returning an error if conversion fails.
func ParseTime(val interface{}) (time.Time, error) {
	return cast.ToTimeE(val)
}

// CoerceTime converts a value to time.Time or returns zero time.Time on failure.
func CoerceTime(val interface{}) time.Time {
	return cast.ToTime(val)
}
csx.json
{"case":{"caseId":"case:sha256:1d1bdf9c3380bf6cc89066270b64efab2af6fbeac3877fe506b08ecb1d72f741","contract":["cast.ToString and cast.ToStringE convert numeric values, booleans, and byte slices to string representations","cast.ToInt and cast.ToIntE safely parse numeric strings, booleans, and floats into integer values","cast.ToInt64 and cast.ToInt64E convert large integer strings and booleans to int64 values","cast.ToBool and cast.ToBoolE parse string and numeric representations into boolean values","cast.ToFloat64 and cast.ToFloat64E convert numeric strings and integer values to float64","cast.ToDuration and cast.ToDurationE parse formatted duration strings and numeric nanoseconds into time.Duration","cast.ToStringSlice and cast.ToStringSliceE convert []interface{} slices and whitespace-separated strings into []string","cast.ToStringMapString and cast.ToStringMapStringE convert map structures with arbitrary value types into map[string]string","cast.ToTime and cast.ToTimeE parse RFC3339 formatted date/time strings into time.Time"],"goal":"verify pkg:golang/github.com/spf13/cast@v1.3.0","kind":"HOW","packages":["pkg:golang/github.com/spf13/cast@v1.3.0"],"schemaVersion":1,"symbols":["cast.ToString","cast.ToStringE","cast.ToInt","cast.ToIntE","cast.ToInt64","cast.ToInt64E","cast.ToBool","cast.ToBoolE","cast.ToFloat64","cast.ToFloat64E","cast.ToDuration","cast.ToDurationE","cast.ToStringSlice","cast.ToStringSliceE","cast.ToStringMapString","cast.ToStringMapStringE","cast.ToTime","cast.ToTimeE"]},"contractCommand":["go","run","./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.3.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/spf13/cast@v1.3.0","symbols":["cast.ToString","cast.ToStringE","cast.ToInt","cast.ToIntE","cast.ToInt64","cast.ToInt64E","cast.ToBool","cast.ToBoolE","cast.ToFloat64","cast.ToFloat64E","cast.ToDuration","cast.ToDurationE","cast.ToStringSlice","cast.ToStringSliceE","cast.ToStringMapString","cast.ToStringMapStringE","cast.ToTime","cast.ToTimeE"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.21

require github.com/spf13/cast v1.3.0
go.sum
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/github.com/spf13/cast@v1.3.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/spf13/cast@v1.3.0"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"
	"reflect"
	"time"

	"sample"

	"github.com/spf13/cast"
)

func fail(msg string, args ...interface{}) {
	fmt.Fprintf(os.Stderr, "contract failure: "+msg+"\n", args...)
	os.Exit(1)
}

func main() {
	// 1. ToString / ToStringE
	s, err := sample.FormatAsString(9876)
	if err != nil || s != "9876" {
		fail("FormatAsString(9876) = (%q, %v)", s, err)
	}
	if cast.ToString(true) != "true" {
		fail("ToString(true) != 'true'")
	}
	if sample.CoerceString([]byte("data")) != "data" {
		fail("CoerceString([]byte('data')) != 'data'")
	}
	if _, err := sample.FormatAsString(struct{ X int }{1}); err == nil {
		fail("expected error from ToStringE on arbitrary struct")
	}

	// 2. ToInt / ToIntE
	n, err := sample.ParseInt("42")
	if err != nil || n != 42 {
		fail("ParseInt('42') = (%d, %v)", n, err)
	}
	if cast.ToInt(true) != 1 || cast.ToInt(false) != 0 {
		fail("ToInt(bool) failed")
	}
	if sample.CoerceInt(19.99) != 19 {
		fail("CoerceInt(19.99) != 19")
	}
	if _, err := sample.ParseInt("bad-int"); err == nil {
		fail("expected error from ToIntE('bad-int')")
	}

	// 3. ToInt64 / ToInt64E
	i64, err := sample.ParseInt64("9223372036854775807")
	if err != nil || i64 != 9223372036854775807 {
		fail("ParseInt64 failed: (%d, %v)", i64, err)
	}
	if sample.CoerceInt64(true) != 1 {
		fail("CoerceInt64(true) != 1")
	}
	if _, err := sample.ParseInt64("bad-int64"); err == nil {
		fail("expected error from ToInt64E('bad-int64')")
	}

	// 4. ToBool / ToBoolE
	b, err := sample.ParseBool("true")
	if err != nil || !b {
		fail("ParseBool('true') = (%v, %v)", b, err)
	}
	if !sample.CoerceBool(1) || sample.CoerceBool(0) {
		fail("CoerceBool numeric conversion failed")
	}
	if _, err := sample.ParseBool("not-a-bool"); err == nil {
		fail("expected error from ToBoolE('not-a-bool')")
	}

	// 5. ToFloat64 / ToFloat64E
	f, err := sample.ParseFloat64("3.5")
	if err != nil || f != 3.5 {
		fail("ParseFloat64('3.5') = (%v, %v)", f, err)
	}
	if sample.CoerceFloat64(25) != 25.0 {
		fail("CoerceFloat64(25) != 25.0")
	}
	if _, err := sample.ParseFloat64("not-a-float"); err == nil {
		fail("expected error from ToFloat64E('not-a-float')")
	}

	// 6. ToDuration / ToDurationE
	d, err := sample.ParseDuration("15s")
	if err != nil || d != 15*time.Second {
		fail("ParseDuration('15s') = (%v, %v)", d, err)
	}
	if sample.CoerceDuration(int64(2*time.Second)) != 2*time.Second {
		fail("CoerceDuration(2s nanoseconds) failed")
	}
	if _, err := sample.ParseDuration("invalid-dur"); err == nil {
		fail("expected error from ToDurationE('invalid-dur')")
	}

	// 7. ToStringSlice / ToStringSliceE
	sl, err := sample.ParseStringSlice([]interface{}{"alpha", 7, true})
	if err != nil || !reflect.DeepEqual(sl, []string{"alpha", "7", "true"}) {
		fail("ParseStringSlice = (%v, %v)", sl, err)
	}
	if !reflect.DeepEqual(sample.CoerceStringSlice("ten twenty"), []string{"ten", "twenty"}) {
		fail("CoerceStringSlice('ten twenty') failed")
	}

	// 8. ToStringMapString / ToStringMapStringE
	mp, err := sample.ParseStringMapString(map[string]interface{}{"k1": "v1", "k2": 200})
	if err != nil || !reflect.DeepEqual(mp, map[string]string{"k1": "v1", "k2": "200"}) {
		fail("ParseStringMapString = (%v, %v)", mp, err)
	}
	if !reflect.DeepEqual(sample.CoerceStringMapString(map[string]interface{}{"ok": true}), map[string]string{"ok": "true"}) {
		fail("CoerceStringMapString failed")
	}

	// 9. ToTime / ToTimeE
	tm, err := sample.ParseTime("2026-05-10T12:30:00Z")
	if err != nil || tm.Year() != 2026 || tm.Month() != time.May || tm.Day() != 10 {
		fail("ParseTime = (%v, %v)", tm, err)
	}
	if sample.CoerceTime("2026-01-01T00:00:00Z").Year() != 2026 {
		fail("CoerceTime failed")
	}
	if _, err := sample.ParseTime("not-a-timestamp"); err == nil {
		fail("expected error from ToTimeE('not-a-timestamp')")
	}
}

Seeder de origen

anónimo