Пример
github.com/spf13/cast v1.4.1: ToString, ToStringE, ToInt
Проверенный пример — golang github.com/spf13/cast v1.4.1: ToString, ToStringE, ToInt. Контракт выполнен на go 1.26 · linux alpine/x64 · docker и пройден.
sha256:cba37c07a4d587d73fe97a37753196b9ff074b65ff64f0c0532b25922f2f3147
Эта сеть предлагает одно: образец, который собирается. Она запустила его в песочнице и сохранила подписанную квитанцию. Она ничего не оценивает и ничего не гарантирует — собирается ли тот же код у вас, она не измеряла.
Сколько различных ключей подписи подали пройденную квитанцию контракта. Один — только автор; больше одного — значит, кто-то ещё тоже собрал. Ключ создаётся сам и не имеет зарегистрированной личности, поэтому считаются ключи, а не люди.
MIT-0
Свидетельства выполнения
Заявленное окружение и подписанные запуски разделены, чтобы вы точно видели, что этот образец запускал и где.
- Основа свидетельства
- Подписанный контракт пройден
- Квитанции проверки
- 1
- Ключи подписи, собравшие его
- 1
Заявленная среда
linux 24 · ubuntu · glibc 2.39 x64 go
Среды запусков проверки
| Окружение | Контракт | Этапы | Запуск |
|---|---|---|---|
| go 1.26 · linux alpine/x64 · docker ed25519:c1973797be207ac4 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f… |
2026-08-28 |
Кейс
HOW- Цель
- verify pkg:golang/github.com/spf13/cast@v1.4.1
- Пакеты
- Символы
-
- 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
- Создан
- 2026-08-28T09:04:39Z
Контракт
- 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 slice collections of arbitrary types 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
Файлы
- PROMPT.md
- cast_sample.go
- cast_sample_test.go
- csx.json
- go.mod
- go.sum
- spec.json
Исходный код
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.4.1
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:golang/github.com/spf13/cast@v1.4.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.
package sample
import (
"time"
"github.com/spf13/cast"
)
// SafeToString converts an arbitrary value to string, returning an error if conversion fails.
func SafeToString(val interface{}) (string, error) {
return cast.ToStringE(val)
}
// ConvertToString converts an arbitrary value to string or returns empty string if invalid.
func ConvertToString(val interface{}) string {
return cast.ToString(val)
}
// SafeToInt converts an arbitrary value to int, returning an error if conversion fails.
func SafeToInt(val interface{}) (int, error) {
return cast.ToIntE(val)
}
// ConvertToInt converts an arbitrary value to int or returns 0 if invalid.
func ConvertToInt(val interface{}) int {
return cast.ToInt(val)
}
// SafeToInt64 converts an arbitrary value to int64, returning an error if conversion fails.
func SafeToInt64(val interface{}) (int64, error) {
return cast.ToInt64E(val)
}
// ConvertToInt64 converts an arbitrary value to int64 or returns 0 if invalid.
func ConvertToInt64(val interface{}) int64 {
return cast.ToInt64(val)
}
// SafeToBool converts an arbitrary value to bool, returning an error if conversion fails.
func SafeToBool(val interface{}) (bool, error) {
return cast.ToBoolE(val)
}
// ConvertToBool converts an arbitrary value to bool or returns false if invalid.
func ConvertToBool(val interface{}) bool {
return cast.ToBool(val)
}
// SafeToFloat64 converts an arbitrary value to float64, returning an error if conversion fails.
func SafeToFloat64(val interface{}) (float64, error) {
return cast.ToFloat64E(val)
}
// ConvertToFloat64 converts an arbitrary value to float64 or returns 0.0 if invalid.
func ConvertToFloat64(val interface{}) float64 {
return cast.ToFloat64(val)
}
// SafeToDuration converts an arbitrary value to time.Duration, returning an error if conversion fails.
func SafeToDuration(val interface{}) (time.Duration, error) {
return cast.ToDurationE(val)
}
// ConvertToDuration converts an arbitrary value to time.Duration or returns 0 if invalid.
func ConvertToDuration(val interface{}) time.Duration {
return cast.ToDuration(val)
}
// SafeToStringSlice converts an arbitrary slice/interface to []string, returning an error if conversion fails.
func SafeToStringSlice(val interface{}) ([]string, error) {
return cast.ToStringSliceE(val)
}
// ConvertToStringSlice converts an arbitrary slice/interface to []string or returns empty slice if invalid.
func ConvertToStringSlice(val interface{}) []string {
return cast.ToStringSlice(val)
}
// SafeToStringMapString converts an arbitrary map/interface to map[string]string, returning an error if conversion fails.
func SafeToStringMapString(val interface{}) (map[string]string, error) {
return cast.ToStringMapStringE(val)
}
// ConvertToStringMapString converts an arbitrary map/interface to map[string]string or returns empty map if invalid.
func ConvertToStringMapString(val interface{}) map[string]string {
return cast.ToStringMapString(val)
}
// SafeToTime converts an arbitrary value to time.Time, returning an error if conversion fails.
func SafeToTime(val interface{}) (time.Time, error) {
return cast.ToTimeE(val)
}
// ConvertToTime converts an arbitrary value to time.Time or returns zero time.Time if invalid.
func ConvertToTime(val interface{}) time.Time {
return cast.ToTime(val)
}
package sample
import (
"reflect"
"testing"
"time"
"github.com/spf13/cast"
)
func TestToString(t *testing.T) {
str, err := cast.ToStringE(12345)
if err != nil || str != "12345" {
t.Fatalf("expected '12345', got %q, err: %v", str, err)
}
if s := cast.ToString(true); s != "true" {
t.Fatalf("expected 'true', got %q", s)
}
if s := cast.ToString([]byte("hello")); s != "hello" {
t.Fatalf("expected 'hello', got %q", s)
}
if s := ConvertToString(42); s != "42" {
t.Fatalf("ConvertToString failed, got %q", s)
}
if _, err := SafeToString(struct{ Name string }{"test"}); err == nil {
t.Fatal("expected error converting complex struct to string with ToStringE")
}
}
func TestToInt(t *testing.T) {
num, err := cast.ToIntE("100")
if err != nil || num != 100 {
t.Fatalf("expected 100, got %d, err: %v", num, err)
}
if n := cast.ToInt(true); n != 1 {
t.Fatalf("expected 1 for true, got %d", n)
}
if n := cast.ToInt(123.45); n != 123 {
t.Fatalf("expected 123 for float, got %d", n)
}
if n := ConvertToInt("50"); n != 50 {
t.Fatalf("ConvertToInt failed, got %d", n)
}
if _, err := SafeToInt("not-a-number"); err == nil {
t.Fatal("expected error converting non-numeric string with ToIntE")
}
}
func TestToInt64(t *testing.T) {
num, err := cast.ToInt64E("9223372036854775807")
if err != nil || num != int64(9223372036854775807) {
t.Fatalf("expected 9223372036854775807, got %d, err: %v", num, err)
}
if n := cast.ToInt64(true); n != 1 {
t.Fatalf("expected 1 for true, got %d", n)
}
if n := ConvertToInt64("1234567890123"); n != 1234567890123 {
t.Fatalf("ConvertToInt64 failed, got %d", n)
}
if _, err := SafeToInt64("invalid"); err == nil {
t.Fatal("expected error converting invalid int64")
}
}
func TestToBool(t *testing.T) {
b, err := cast.ToBoolE("true")
if err != nil || !b {
t.Fatalf("expected true, got %v, err: %v", b, err)
}
b2, err := cast.ToBoolE("1")
if err != nil || !b2 {
t.Fatalf("expected true for '1', got %v, err: %v", b2, err)
}
b3, err := cast.ToBoolE(0)
if err != nil || b3 {
t.Fatalf("expected false for 0, got %v, err: %v", b3, err)
}
if !ConvertToBool("true") {
t.Fatal("expected true for 'true'")
}
if _, err := SafeToBool("unrecognized-bool"); err == nil {
t.Fatal("expected error converting invalid bool string with ToBoolE")
}
}
func TestToFloat64(t *testing.T) {
f, err := cast.ToFloat64E("3.1415")
if err != nil || f != 3.1415 {
t.Fatalf("expected 3.1415, got %v, err: %v", f, err)
}
if val := cast.ToFloat64(10); val != 10.0 {
t.Fatalf("expected 10.0, got %v", val)
}
if val := ConvertToFloat64("2.718"); val != 2.718 {
t.Fatalf("ConvertToFloat64 failed, got %v", val)
}
if _, err := SafeToFloat64("not-a-float"); err == nil {
t.Fatal("expected error converting invalid float")
}
}
func TestToDuration(t *testing.T) {
d, err := cast.ToDurationE("5s")
if err != nil || d != 5*time.Second {
t.Fatalf("expected 5s, got %v, err: %v", d, err)
}
d2, err := cast.ToDurationE(5000000000)
if err != nil || d2 != 5*time.Second {
t.Fatalf("expected 5s from int, got %v, err: %v", d2, err)
}
if res := ConvertToDuration("10m"); res != 10*time.Minute {
t.Fatalf("ConvertToDuration failed, got %v", res)
}
if _, err := SafeToDuration("invalid-duration"); err == nil {
t.Fatal("expected error converting invalid duration string with ToDurationE")
}
}
func TestToStringSlice(t *testing.T) {
input := []interface{}{"apple", 123, true}
expected := []string{"apple", "123", "true"}
slice, err := cast.ToStringSliceE(input)
if err != nil || !reflect.DeepEqual(slice, expected) {
t.Fatalf("expected %v, got %v, err: %v", expected, slice, err)
}
res := ConvertToStringSlice([]int{1, 2, 3})
if !reflect.DeepEqual(res, []string{"1", "2", "3"}) {
t.Fatalf("ConvertToStringSlice failed, got %v", res)
}
}
func TestToStringMapString(t *testing.T) {
input := map[string]interface{}{
"host": "localhost",
"port": 8080,
}
expected := map[string]string{
"host": "localhost",
"port": "8080",
}
resMap, err := cast.ToStringMapStringE(input)
if err != nil || !reflect.DeepEqual(resMap, expected) {
t.Fatalf("expected %v, got %v, err: %v", expected, resMap, err)
}
helperMap := ConvertToStringMapString(input)
if !reflect.DeepEqual(helperMap, expected) {
t.Fatalf("ConvertToStringMapString failed, got %v", helperMap)
}
}
func TestToTime(t *testing.T) {
timeStr := "2026-08-28T09:00:00Z"
parsed, err := cast.ToTimeE(timeStr)
if err != nil {
t.Fatalf("expected to parse RFC3339 time, got err: %v", err)
}
if parsed.Year() != 2026 || parsed.Month() != time.August || parsed.Day() != 28 {
t.Fatalf("unexpected parsed date: %v", parsed)
}
tm := ConvertToTime("2026-01-01T00:00:00Z")
if tm.Year() != 2026 {
t.Fatalf("ConvertToTime failed, got %v", tm)
}
if _, err := SafeToTime("invalid-time-format"); err == nil {
t.Fatal("expected error converting invalid time string with ToTimeE")
}
}
{"case":{"caseId":"case:sha256:08d292b1d9d851a74700d13c978c0c382224021851a5716edf1a39ee7013da79","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 slice collections of arbitrary types 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.4.1","kind":"HOW","packages":["pkg:golang/github.com/spf13/cast@v1.4.1"],"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","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.4.1"],"schemaVersion":1,"subject":"pkg:golang/github.com/spf13/cast@v1.4.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"],"verifierAdapter":"golang@1"}
module sample
go 1.26.6
require github.com/spf13/cast v1.4.1
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.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA=
github.com/spf13/cast v1.4.1/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=
{
"schemaVersion": 1,
"goal": "verify pkg:golang/github.com/spf13/cast@v1.4.1",
"kind": "HOW",
"packages": [
"pkg:golang/github.com/spf13/cast@v1.4.1"
]
}
Исходный сидер
аноним