CodeSampleX

サンプル

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

検証済みサンプル — golang github.com/spf13/cast v1.10.0: ToString. go 1.26 · linux debian/x64 · docker で contract を実行し、成功しました: cast.ToString converts string values…

sha256:aacb9bbfa3e51d0acaa8d8cc9cc3faaa35cd99e768f2316f9b12e7cba933f1e9

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 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.ToString
作成日
2026-09-06T16:21:04Z

コントラクト

  1. cast.ToString converts string values and byte slices to strings
  2. cast.ToString converts integer, unsigned integer, and floating-point numeric values to formatted strings
  3. cast.ToString converts boolean values to string representations 'true' and 'false'
  4. cast.ToString returns an empty string for nil values
  5. cast.ToString unwraps pointer values before converting their underlying values to strings
  6. cast.ToString converts types implementing fmt.Stringer, errors, and time.Duration to their string representations
  7. cast.ToString returns an empty string and cast.ToStringE returns an error for unsupported types
  8. ToString converts the given value to string using cast.ToString

ファイル

  • PROMPT.md
  • cast_string.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
Demonstrate these symbols/APIs:
  - github.com/spf13/cast.ToString

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

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

// ToString converts an arbitrary value into a string using cast.ToString.
// It returns an empty string if conversion cannot be performed.
func ToString(v any) string {
	return cast.ToString(v)
}

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

import (
	"fmt"
	"testing"
	"time"

	"github.com/spf13/cast"
)

type customStringer struct {
	val string
}

func (c customStringer) String() string {
	return "stringer:" + c.val
}

func TestToString_StringsAndBytes(t *testing.T) {
	if got := cast.ToString("hello"); got != "hello" {
		t.Fatalf("cast.ToString(\"hello\") = %q; want \"hello\"", got)
	}
	if got := cast.ToString([]byte("byte slice")); got != "byte slice" {
		t.Fatalf("cast.ToString([]byte) = %q; want \"byte slice\"", got)
	}
}

func TestToString_Numbers(t *testing.T) {
	if got := cast.ToString(42); got != "42" {
		t.Fatalf("cast.ToString(42) = %q; want \"42\"", got)
	}
	if got := cast.ToString(int64(-100)); got != "-100" {
		t.Fatalf("cast.ToString(int64(-100)) = %q; want \"-100\"", got)
	}
	if got := cast.ToString(uint(50)); got != "50" {
		t.Fatalf("cast.ToString(uint(50)) = %q; want \"50\"", got)
	}
	if got := cast.ToString(3.14); got != "3.14" {
		t.Fatalf("cast.ToString(3.14) = %q; want \"3.14\"", got)
	}
}

func TestToString_Booleans(t *testing.T) {
	if got := cast.ToString(true); got != "true" {
		t.Fatalf("cast.ToString(true) = %q; want \"true\"", got)
	}
	if got := cast.ToString(false); got != "false" {
		t.Fatalf("cast.ToString(false) = %q; want \"false\"", got)
	}
}

func TestToString_Nil(t *testing.T) {
	if got := cast.ToString(nil); got != "" {
		t.Fatalf("cast.ToString(nil) = %q; want \"\"", got)
	}
}

func TestToString_Pointers(t *testing.T) {
	num := 123
	if got := cast.ToString(&num); got != "123" {
		t.Fatalf("cast.ToString(&num) = %q; want \"123\"", got)
	}
	str := "pointed"
	if got := cast.ToString(&str); got != "pointed" {
		t.Fatalf("cast.ToString(&str) = %q; want \"pointed\"", got)
	}
	b := true
	if got := cast.ToString(&b); got != "true" {
		t.Fatalf("cast.ToString(&b) = %q; want \"true\"", got)
	}
}

func TestToString_StringerAndErrors(t *testing.T) {
	s := customStringer{val: "hello"}
	if got := cast.ToString(s); got != "stringer:hello" {
		t.Fatalf("cast.ToString(customStringer) = %q; want \"stringer:hello\"", got)
	}

	errVal := fmt.Errorf("custom error")
	if got := cast.ToString(errVal); got != "custom error" {
		t.Fatalf("cast.ToString(errVal) = %q; want \"custom error\"", got)
	}

	dur := 5 * time.Second
	if got := cast.ToString(dur); got != "5s" {
		t.Fatalf("cast.ToString(dur) = %q; want \"5s\"", got)
	}
}

func TestToString_Helper(t *testing.T) {
	cases := []any{
		"test",
		999,
		true,
		nil,
		[]byte("abc"),
	}
	for _, c := range cases {
		expected := cast.ToString(c)
		if got := ToString(c); got != expected {
			t.Fatalf("ToString(%v) = %q; want %q", c, got, expected)
		}
	}
}

func TestToString_UnsupportedTypesReturnEmptyString(t *testing.T) {
	unsupported := []any{
		[]int{1, 2, 3},
		map[string]string{"a": "b"},
		func() {},
	}
	for _, val := range unsupported {
		if got := cast.ToString(val); got != "" {
			t.Fatalf("cast.ToString(%v) = %q; want \"\"", val, got)
		}
		if _, err := cast.ToStringE(val); err == nil {
			t.Fatalf("cast.ToStringE(%v) expected error, got nil", val)
		}
	}
}
csx.json
{"case":{"caseId":"case:sha256:e0943535a6326b5cffd7c01219f0fa8de41578bbdb71b15bbd93b734f27633c8","contract":["cast.ToString converts string values and byte slices to strings","cast.ToString converts integer, unsigned integer, and floating-point numeric values to formatted strings","cast.ToString converts boolean values to string representations 'true' and 'false'","cast.ToString returns an empty string for nil values","cast.ToString unwraps pointer values before converting their underlying values to strings","cast.ToString converts types implementing fmt.Stringer, errors, and time.Duration to their string representations","cast.ToString returns an empty string and cast.ToStringE returns an error for unsupported types","ToString converts the given value to string using cast.ToString"],"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.ToString"]},"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.ToString"],"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"
  ],
  "symbols": [
    "github.com/spf13/cast.ToString"
  ]
}

オリジンシーダー

匿名