CodeSampleX

샘플

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

검증된 샘플 — golang github.com/spf13/cast v1.10.0: ToInt. go 1.26 · linux debian/x64 · docker에서 contract를 실행해 통과했습니다: cast.ToInt converts integer, unsigned…

sha256:ae4c33c6f55347312c95d3a6f33bb549662141519ca4ed918e6dcb265fc7c8cb

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. MIT-0

실행 증거

선언된 환경과 서명된 실행을 분리해 두었습니다. 이 샘플이 무엇을 어디서 실행했는지 그대로 볼 수 있습니다.

증거 기준
서명된 컨트랙트 통과
검증 영수증
1
빌드한 서명 키
1
선언된 환경 linux 24 · ubuntu · glibc 2.39 x64 go

검증 실행 환경

환경 컨트랙트 단계 실행일
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

케이스

HOW
목표
verify pkg:golang/github.com/spf13/cast@v1.10.0
패키지
심벌
  • github.com/spf13/cast.ToInt
생성일
2026-09-06T16:26:49Z

컨트랙트

  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

파일

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

소스 아티팩트 내려받기 (tar.gz)

소스

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"
  ]
}

오리진 시더

익명