CodeSampleX

示例

github.com/spf13/viper v1.3.2

已验证示例 — golang github.com/spf13/viper v1.3.2. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过: viper.New creates an isolated configuration instance…

sha256:901c0830e9411594e62338032762fbe7a8d1d1b1b106f1d9dde0cbc40426a9a6

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 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-10-27

案例

HOW
目标
verify pkg:golang/github.com/spf13/viper@v1.3.2
包
创建时间
2026-10-27T22:14:04Z

契约

  1. viper.New creates an isolated configuration instance where SetDefault provides fallback values when no higher-precedence source is set
  2. ReadConfig with SetConfigType("json") parses in-memory JSON configuration that overrides defaults while preserving unset default keys
  3. Explicit Set overrides values loaded via ReadConfig and SetDefault in hierarchical precedence order
  4. Unmarshal decodes the resolved configuration hierarchy into a Go struct

文件

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract.go

下载源代码构件 (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/viper@v1.3.2
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/spf13/viper@v1.3.2

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.
csx.json
{"case":{"caseId":"case:sha256:7ef1725760049613f1642c34d351e010a379be8ed0fac4a27619a968c134c48c","contract":["viper.New creates an isolated configuration instance where SetDefault provides fallback values when no higher-precedence source is set","ReadConfig with SetConfigType(\"json\") parses in-memory JSON configuration that overrides defaults while preserving unset default keys","Explicit Set overrides values loaded via ReadConfig and SetDefault in hierarchical precedence order","Unmarshal decodes the resolved configuration hierarchy into a Go struct"],"goal":"verify pkg:golang/github.com/spf13/viper@v1.3.2","kind":"HOW","packages":["pkg:golang/github.com/spf13/viper@v1.3.2"],"schemaVersion":1},"contractCommand":["go","run","./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/viper@v1.3.2"],"schemaVersion":1,"subject":"pkg:golang/github.com/spf13/viper@v1.3.2","verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require github.com/spf13/viper v1.3.2

require (
	github.com/BurntSushi/toml v1.6.0 // indirect
	github.com/fsnotify/fsnotify v1.4.7 // indirect
	github.com/hashicorp/hcl v1.0.0 // indirect
	github.com/magiconair/properties v1.8.0 // indirect
	github.com/mitchellh/mapstructure v1.1.2 // indirect
	github.com/pelletier/go-toml v1.2.0 // indirect
	github.com/spf13/afero v1.1.2 // indirect
	github.com/spf13/cast v1.3.0 // indirect
	github.com/spf13/jwalterweatherman v1.0.0 // indirect
	github.com/spf13/pflag v1.0.3 // indirect
	golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a // indirect
	golang.org/x/text v0.3.0 // indirect
	gopkg.in/yaml.v2 v2.2.2 // indirect
)
go.sum
github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk=
github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
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/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
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/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/viper v1.3.2 h1:VUFqw5KcqRf7i70GOzW7N+Q7+gxVBkSSqiXB12+JQ4M=
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a h1:1n5lsVfiQW3yfsRGu98756EH1YthsFqr/5mxHduZW2A=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
main.go
package main

import (
	"fmt"
	"strings"

	"github.com/spf13/viper"
)

type ServerConfig struct {
	Host string `mapstructure:"host"`
	Port int    `mapstructure:"port"`
	TLS  bool   `mapstructure:"tls"`
}

func main() {
	v := viper.New()
	v.SetDefault("host", "localhost")
	v.SetDefault("port", 8080)
	v.SetDefault("tls", false)

	v.SetConfigType("json")
	err := v.ReadConfig(strings.NewReader(`{"port": 9000, "tls": true}`))
	if err != nil {
		panic(err)
	}

	v.Set("host", "example.com")

	var cfg ServerConfig
	if err := v.Unmarshal(&cfg); err != nil {
		panic(err)
	}

	fmt.Printf("host=%s port=%d tls=%t\n", cfg.Host, cfg.Port, cfg.TLS)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/github.com/spf13/viper@v1.3.2",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/spf13/viper@v1.3.2"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"
	"strings"

	"github.com/spf13/viper"
)

type AppConfig struct {
	Host    string `mapstructure:"host"`
	Port    int    `mapstructure:"port"`
	Enabled bool   `mapstructure:"enabled"`
}

func main() {
	v := viper.New()

	// Assertion 1: SetDefault provides fallback values when no higher-precedence source is set
	v.SetDefault("host", "localhost")
	v.SetDefault("port", 8080)
	v.SetDefault("enabled", false)

	if got := v.GetString("host"); got != "localhost" {
		fmt.Fprintf(os.Stderr, "assertion 1 failed: expected default host %q, got %q\n", "localhost", got)
		os.Exit(1)
	}
	if got := v.GetInt("port"); got != 8080 {
		fmt.Fprintf(os.Stderr, "assertion 1 failed: expected default port 8080, got %d\n", got)
		os.Exit(1)
	}

	// Assertion 2: ReadConfig with SetConfigType("json") overrides defaults while preserving unset default keys
	v.SetConfigType("json")
	if err := v.ReadConfig(strings.NewReader(`{"port": 9000, "enabled": true}`)); err != nil {
		fmt.Fprintf(os.Stderr, "assertion 2 failed: ReadConfig returned error: %v\n", err)
		os.Exit(1)
	}
	if got := v.GetInt("port"); got != 9000 {
		fmt.Fprintf(os.Stderr, "assertion 2 failed: expected config port 9000, got %d\n", got)
		os.Exit(1)
	}
	if got := v.GetBool("enabled"); !got {
		fmt.Fprintf(os.Stderr, "assertion 2 failed: expected config enabled=true, got %t\n", got)
		os.Exit(1)
	}
	if got := v.GetString("host"); got != "localhost" {
		fmt.Fprintf(os.Stderr, "assertion 2 failed: expected preserved default host %q, got %q\n", "localhost", got)
		os.Exit(1)
	}

	// Assertion 3: Explicit Set overrides values loaded via ReadConfig and SetDefault
	v.Set("port", 9090)
	v.Set("host", "example.com")
	if got := v.GetInt("port"); got != 9090 {
		fmt.Fprintf(os.Stderr, "assertion 3 failed: expected Set port 9090, got %d\n", got)
		os.Exit(1)
	}
	if got := v.GetString("host"); got != "example.com" {
		fmt.Fprintf(os.Stderr, "assertion 3 failed: expected Set host %q, got %q\n", "example.com", got)
		os.Exit(1)
	}

	// Assertion 4: Unmarshal decodes the resolved configuration hierarchy into a Go struct
	var cfg AppConfig
	if err := v.Unmarshal(&cfg); err != nil {
		fmt.Fprintf(os.Stderr, "assertion 4 failed: Unmarshal returned error: %v\n", err)
		os.Exit(1)
	}
	if cfg.Host != "example.com" || cfg.Port != 9090 || !cfg.Enabled {
		fmt.Fprintf(os.Stderr, "assertion 4 failed: unexpected struct %+v\n", cfg)
		os.Exit(1)
	}

	fmt.Println("All contract assertions passed.")
}

原始种子者

匿名