CodeSampleX

Exemple

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

Échantillon vérifié pour golang github.com/spf13/cast v1.10.0: ToStringSlice. Le contrat s'est exécuté sur go 1.26 · linux debian/x64 · docker et a réussi.

sha256:34b1997defe0933578391f1f518e98bf94574779a59b7b3c979651881f441552

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
1
Clés de signature qui l’ont compilé
1
Environnement déclaré linux 24 · ubuntu · glibc 2.39 x64 go

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
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

Cas

HOW
Objectif
verify pkg:golang/github.com/spf13/cast@v1.10.0
Paquets
Symboles
  • github.com/spf13/cast.ToStringSlice
Créé
2026-09-06T16:46:24Z

Contrat

  1. cast.ToStringSlice returns unchanged string slice when passed []string
  2. cast.ToStringSlice converts elements of generic slices and typed slices to []string
  3. cast.ToStringSlice converts single scalar values into single-element string slices
  4. cast.ToStringSlice unwraps pointer values before converting their underlying values to []string
  5. cast.ToStringSlice returns nil and cast.ToStringSliceE returns an error for nil values
  6. cast.ToStringSlice returns nil and cast.ToStringSliceE returns an error for unsupported types
  7. ToStringSlice converts the given value to a slice of strings using cast.ToStringSlice

Fichiers

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

Télécharger l’artefact source (tar.gz)

Code source

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.ToStringSlice

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

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

// ToStringSlice converts an arbitrary value into a slice of strings using cast.ToStringSlice.
// It returns nil if conversion cannot be performed.
func ToStringSlice(v any) []string {
	return cast.ToStringSlice(v)
}

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

import (
	"reflect"
	"testing"

	"github.com/spf13/cast"
)

func TestToStringSlice_StringSlice(t *testing.T) {
	orig := []string{"apple", "banana", "cherry"}
	got := cast.ToStringSlice(orig)
	if !reflect.DeepEqual(got, orig) {
		t.Fatalf("cast.ToStringSlice(%v) = %v; want %v", orig, got, orig)
	}

	gotE, err := cast.ToStringSliceE(orig)
	if err != nil {
		t.Fatalf("cast.ToStringSliceE(%v) unexpected error: %v", orig, err)
	}
	if !reflect.DeepEqual(gotE, orig) {
		t.Fatalf("cast.ToStringSliceE(%v) = %v; want %v", orig, gotE, orig)
	}
}

func TestToStringSlice_GenericAndTypedSlices(t *testing.T) {
	anySlice := []any{"hello", 42, true, 3.14}
	wantAny := []string{"hello", "42", "true", "3.14"}
	if got := cast.ToStringSlice(anySlice); !reflect.DeepEqual(got, wantAny) {
		t.Fatalf("cast.ToStringSlice(%v) = %v; want %v", anySlice, got, wantAny)
	}

	intSlice := []int{10, 20, -30}
	wantInt := []string{"10", "20", "-30"}
	if got := cast.ToStringSlice(intSlice); !reflect.DeepEqual(got, wantInt) {
		t.Fatalf("cast.ToStringSlice(%v) = %v; want %v", intSlice, got, wantInt)
	}

	boolSlice := []bool{true, false}
	wantBool := []string{"true", "false"}
	if got := cast.ToStringSlice(boolSlice); !reflect.DeepEqual(got, wantBool) {
		t.Fatalf("cast.ToStringSlice(%v) = %v; want %v", boolSlice, got, wantBool)
	}
}

func TestToStringSlice_Scalars(t *testing.T) {
	str := "standalone"
	if got := cast.ToStringSlice(str); !reflect.DeepEqual(got, []string{"standalone"}) {
		t.Fatalf("cast.ToStringSlice(%q) = %v; want [\"standalone\"]", str, got)
	}

	num := 999
	if got := cast.ToStringSlice(num); !reflect.DeepEqual(got, []string{"999"}) {
		t.Fatalf("cast.ToStringSlice(%d) = %v; want [\"999\"]", num, got)
	}

	b := true
	if got := cast.ToStringSlice(b); !reflect.DeepEqual(got, []string{"true"}) {
		t.Fatalf("cast.ToStringSlice(%v) = %v; want [\"true\"]", b, got)
	}
}

func TestToStringSlice_Pointers(t *testing.T) {
	sliceVal := []string{"foo", "bar"}
	if got := cast.ToStringSlice(&sliceVal); !reflect.DeepEqual(got, sliceVal) {
		t.Fatalf("cast.ToStringSlice(&%v) = %v; want %v", sliceVal, got, sliceVal)
	}

	scalarStr := "pointer_test"
	if got := cast.ToStringSlice(&scalarStr); !reflect.DeepEqual(got, []string{"pointer_test"}) {
		t.Fatalf("cast.ToStringSlice(&%q) = %v; want [\"pointer_test\"]", scalarStr, got)
	}

	scalarInt := 123
	if got := cast.ToStringSlice(&scalarInt); !reflect.DeepEqual(got, []string{"123"}) {
		t.Fatalf("cast.ToStringSlice(&%d) = %v; want [\"123\"]", scalarInt, got)
	}
}

func TestToStringSlice_Nil(t *testing.T) {
	if got := cast.ToStringSlice(nil); got != nil {
		t.Fatalf("cast.ToStringSlice(nil) = %v; want nil", got)
	}
	val, err := cast.ToStringSliceE(nil)
	if err == nil {
		t.Fatalf("cast.ToStringSliceE(nil) expected error, got nil error with val %v", val)
	}
	if val != nil {
		t.Fatalf("cast.ToStringSliceE(nil) = %v; want nil", val)
	}
}

func TestToStringSlice_UnsupportedTypes(t *testing.T) {
	unsupported := []any{
		map[string]int{"a": 1},
		struct{ Name string }{Name: "example"},
		func() {},
	}
	for _, val := range unsupported {
		if got := cast.ToStringSlice(val); got != nil {
			t.Fatalf("cast.ToStringSlice(%v) = %v; want nil", val, got)
		}
		if _, err := cast.ToStringSliceE(val); err == nil {
			t.Fatalf("cast.ToStringSliceE(%v) expected error, got nil", val)
		}
	}
}

func TestToStringSlice_Helper(t *testing.T) {
	cases := []any{
		[]string{"x", "y"},
		[]int{1, 2},
		"hello",
		123,
	}
	for _, c := range cases {
		expected := cast.ToStringSlice(c)
		if got := ToStringSlice(c); !reflect.DeepEqual(got, expected) {
			t.Fatalf("ToStringSlice(%v) = %v; want %v", c, got, expected)
		}
		gotE, err := ToStringSliceE(c)
		if err != nil {
			t.Fatalf("ToStringSliceE(%v) unexpected error: %v", c, err)
		}
		if !reflect.DeepEqual(gotE, expected) {
			t.Fatalf("ToStringSliceE(%v) = %v; want %v", c, gotE, expected)
		}
	}
}
csx.json
{"case":{"caseId":"case:sha256:ea3fed646a65b910ad00164a84db6cd1f77bfbb5d8e50ec495f31f42553653bf","contract":["cast.ToStringSlice returns unchanged string slice when passed []string","cast.ToStringSlice converts elements of generic slices and typed slices to []string","cast.ToStringSlice converts single scalar values into single-element string slices","cast.ToStringSlice unwraps pointer values before converting their underlying values to []string","cast.ToStringSlice returns nil and cast.ToStringSliceE returns an error for nil values","cast.ToStringSlice returns nil and cast.ToStringSliceE returns an error for unsupported types","ToStringSlice converts the given value to a slice of strings using cast.ToStringSlice"],"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.ToStringSlice"]},"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.ToStringSlice"],"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.ToStringSlice"
  ]
}

Seeder d'origine

anonyme