Exemplo
github.com/spf13/cast v1.10.0: ToDuration
Amostra verificada para golang github.com/spf13/cast v1.10.0: ToDuration. O contrato rodou em go 1.26 · linux debian/x64 · docker e passou.
sha256:6a538eccaf509af44f646b6ee27b914379cbec3e1b662bba9f9a6ab4da0af4ea
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
linux 24 · ubuntu · glibc 2.39 x64 go
Ambientes das execuções de verificação
| Ambiente | Contrato | Etapas | Execução |
|---|---|---|---|
| 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
- Pacotes
- Símbolos
-
- github.com/spf13/cast.ToDuration
- Criado
- 2026-09-06T16:34:34Z
Contrato
- cast.ToDuration returns the unchanged duration when passed a time.Duration value
- cast.ToDuration and cast.ToDurationE parse duration strings with units including ns, us, ms, s, m, and h
- cast.ToDuration parses pure numeric strings without units as nanoseconds into time.Duration
- cast.ToDuration converts signed and unsigned integers and floating-point nanoseconds into time.Duration
- cast.ToDuration returns 0 and cast.ToDurationE returns 0 with nil error for nil values
- cast.ToDuration unwraps pointer values before converting their underlying values to time.Duration
- cast.ToDuration returns 0 and cast.ToDurationE returns an error for invalid duration strings and unsupported types
- ToDuration converts the given value to time.Duration using cast.ToDuration
Arquivos
- PROMPT.md
- cast_duration.go
- contract_test.go
- csx.json
- go.mod
- go.sum
- spec.json
Código-fonte
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.
package sample
import (
"time"
"github.com/spf13/cast"
)
// ToDuration converts an arbitrary value into a time.Duration using cast.ToDuration.
// It returns 0 if conversion cannot be performed.
func ToDuration(v any) time.Duration {
return cast.ToDuration(v)
}
// ToDurationE converts an arbitrary value into a time.Duration using cast.ToDurationE,
// returning an error if conversion cannot be performed.
func ToDurationE(v any) (time.Duration, error) {
return cast.ToDurationE(v)
}
package sample
import (
"testing"
"time"
"github.com/spf13/cast"
)
func TestToDuration_Duration(t *testing.T) {
expected := 42 * time.Second
if got := cast.ToDuration(expected); got != expected {
t.Fatalf("cast.ToDuration(expected) = %v; want %v", got, expected)
}
val, err := cast.ToDurationE(100 * time.Millisecond)
if err != nil || val != 100*time.Millisecond {
t.Fatalf("cast.ToDurationE(100ms) = (%v, %v); want (100ms, nil)", val, err)
}
}
func TestToDuration_Strings(t *testing.T) {
cases := []struct {
input string
expected time.Duration
}{
{"10s", 10 * time.Second},
{"1m30s", 90 * time.Second},
{"500ms", 500 * time.Millisecond},
{"2h", 2 * time.Hour},
{"250us", 250 * time.Microsecond},
{"1000ns", 1000 * time.Nanosecond},
{"300µs", 300 * time.Microsecond},
}
for _, tc := range cases {
if got := cast.ToDuration(tc.input); got != tc.expected {
t.Fatalf("cast.ToDuration(%q) = %v; want %v", tc.input, got, tc.expected)
}
val, err := cast.ToDurationE(tc.input)
if err != nil || val != tc.expected {
t.Fatalf("cast.ToDurationE(%q) = (%v, %v); want (%v, nil)", tc.input, val, err, tc.expected)
}
}
}
func TestToDuration_NumericStrings(t *testing.T) {
// Strings without duration units are parsed as nanoseconds by cast
if got := cast.ToDuration("5000"); got != 5000*time.Nanosecond {
t.Fatalf("cast.ToDuration(\"5000\") = %v; want 5000ns", got)
}
if got := cast.ToDuration("0"); got != 0 {
t.Fatalf("cast.ToDuration(\"0\") = %v; want 0", got)
}
val, err := cast.ToDurationE("2000000000")
if err != nil || val != 2*time.Second {
t.Fatalf("cast.ToDurationE(\"2000000000\") = (%v, %v); want (2s, nil)", val, err)
}
}
func TestToDuration_NumericValues(t *testing.T) {
if got := cast.ToDuration(int(5000000000)); got != 5*time.Second {
t.Fatalf("cast.ToDuration(int) = %v; want 5s", got)
}
if got := cast.ToDuration(int8(10)); got != 10*time.Nanosecond {
t.Fatalf("cast.ToDuration(int8) = %v; want 10ns", got)
}
if got := cast.ToDuration(int16(100)); got != 100*time.Nanosecond {
t.Fatalf("cast.ToDuration(int16) = %v; want 100ns", got)
}
if got := cast.ToDuration(int32(1000)); got != 1000*time.Nanosecond {
t.Fatalf("cast.ToDuration(int32) = %v; want 1000ns", got)
}
if got := cast.ToDuration(int64(2000000000)); got != 2*time.Second {
t.Fatalf("cast.ToDuration(int64) = %v; want 2s", got)
}
if got := cast.ToDuration(uint(5000000000)); got != 5*time.Second {
t.Fatalf("cast.ToDuration(uint) = %v; want 5s", got)
}
if got := cast.ToDuration(uint8(20)); got != 20*time.Nanosecond {
t.Fatalf("cast.ToDuration(uint8) = %v; want 20ns", got)
}
if got := cast.ToDuration(uint16(200)); got != 200*time.Nanosecond {
t.Fatalf("cast.ToDuration(uint16) = %v; want 200ns", got)
}
if got := cast.ToDuration(uint32(2000)); got != 2000*time.Nanosecond {
t.Fatalf("cast.ToDuration(uint32) = %v; want 2000ns", got)
}
if got := cast.ToDuration(uint64(3000000000)); got != 3*time.Second {
t.Fatalf("cast.ToDuration(uint64) = %v; want 3s", got)
}
if got := cast.ToDuration(float64(4000000000)); got != 4*time.Second {
t.Fatalf("cast.ToDuration(float64) = %v; want 4s", got)
}
if got := cast.ToDuration(float32(1000000)); got != 1*time.Millisecond {
t.Fatalf("cast.ToDuration(float32) = %v; want 1ms", got)
}
}
func TestToDuration_Nil(t *testing.T) {
if got := cast.ToDuration(nil); got != 0 {
t.Fatalf("cast.ToDuration(nil) = %v; want 0", got)
}
val, err := cast.ToDurationE(nil)
if err != nil || val != 0 {
t.Fatalf("cast.ToDurationE(nil) = (%v, %v); want (0, nil)", val, err)
}
}
func TestToDuration_Pointers(t *testing.T) {
d := 5 * time.Second
if got := cast.ToDuration(&d); got != 5*time.Second {
t.Fatalf("cast.ToDuration(&d) = %v; want 5s", got)
}
s := "10s"
if got := cast.ToDuration(&s); got != 10*time.Second {
t.Fatalf("cast.ToDuration(&s) = %v; want 10s", got)
}
n := int64(1000000)
if got := cast.ToDuration(&n); got != 1*time.Millisecond {
t.Fatalf("cast.ToDuration(&n) = %v; want 1ms", got)
}
}
func TestToDuration_InvalidAndUnsupported(t *testing.T) {
invalidCases := []any{
"not_a_duration",
[]int{1, 2, 3},
map[string]int{"a": 1},
struct{}{},
}
for _, c := range invalidCases {
if got := cast.ToDuration(c); got != 0 {
t.Fatalf("cast.ToDuration(%v) = %v; want 0", c, got)
}
if _, err := cast.ToDurationE(c); err == nil {
t.Fatalf("cast.ToDurationE(%v) expected error, got nil", c)
}
}
}
func TestToDuration_Helper(t *testing.T) {
cases := []struct {
input any
expected time.Duration
}{
{"15s", 15 * time.Second},
{5 * time.Second, 5 * time.Second},
{int64(3000000000), 3 * time.Second},
{nil, 0},
}
for _, c := range cases {
if got := ToDuration(c.input); got != c.expected {
t.Fatalf("ToDuration(%v) = %v; want %v", c.input, got, c.expected)
}
val, err := ToDurationE(c.input)
if err != nil || val != c.expected {
t.Fatalf("ToDurationE(%v) = (%v, %v); want (%v, nil)", c.input, val, err, c.expected)
}
}
if _, err := ToDurationE("invalid"); err == nil {
t.Fatal("ToDurationE(\"invalid\") expected error, got nil")
}
}
{"case":{"caseId":"case:sha256:6cce1a3dccc69bad67835bf7079c2c9d6150585bd61151b64b4479162241992a","contract":["cast.ToDuration returns the unchanged duration when passed a time.Duration value","cast.ToDuration and cast.ToDurationE parse duration strings with units including ns, us, ms, s, m, and h","cast.ToDuration parses pure numeric strings without units as nanoseconds into time.Duration","cast.ToDuration converts signed and unsigned integers and floating-point nanoseconds into time.Duration","cast.ToDuration returns 0 and cast.ToDurationE returns 0 with nil error for nil values","cast.ToDuration unwraps pointer values before converting their underlying values to time.Duration","cast.ToDuration returns 0 and cast.ToDurationE returns an error for invalid duration strings and unsupported types","ToDuration converts the given value to time.Duration using cast.ToDuration"],"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.ToDuration"]},"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.ToDuration"],"verifierAdapter":"golang@1"}
module example.com/sample
go 1.26.6
require github.com/spf13/cast v1.10.0
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=
{
"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 origem
anônimo