CodeSampleX

示例

github.com/pelletier/go-toml/v2 v2.4.3

已验证示例 — golang github.com/pelletier/go-toml/v2 v2.4.3. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过: toml.Marshal serializes structs and maps into…

sha256:c8be58061ab1c0a01d0ed254c4f19fcce5950745fa38f9c72f51be2518b15a30

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 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-09-13

案例

HOW
目标
verify pkg:golang/github.com/pelletier/go-toml/v2@v2.4.3
包
创建时间
2026-09-13T19:22:45Z

契约

  1. toml.Marshal serializes structs and maps into valid TOML bytes
  2. toml.Unmarshal deserializes TOML bytes into target structs with matching fields
  3. toml.NewDecoder and toml.NewEncoder stream TOML content via io.Reader and io.Writer
  4. toml.Unmarshal returns an error when parsing malformed TOML syntax

文件

  • 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/pelletier/go-toml/v2@v2.4.3
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/pelletier/go-toml/v2@v2.4.3

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:009c7c94a3d384dc640b97d176115f670294dbe1fb76f70686bd905d2842694a","contract":["toml.Marshal serializes structs and maps into valid TOML bytes","toml.Unmarshal deserializes TOML bytes into target structs with matching fields","toml.NewDecoder and toml.NewEncoder stream TOML content via io.Reader and io.Writer","toml.Unmarshal returns an error when parsing malformed TOML syntax"],"goal":"verify pkg:golang/github.com/pelletier/go-toml/v2@v2.4.3","kind":"HOW","packages":["pkg:golang/github.com/pelletier/go-toml/v2@v2.4.3"],"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/pelletier/go-toml/v2@v2.4.3"],"schemaVersion":1,"subject":"pkg:golang/github.com/pelletier/go-toml/v2@v2.4.3","verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require github.com/pelletier/go-toml/v2 v2.4.3
go.sum
github.com/pelletier/go-toml/v2 v2.4.3 h1:GTRvJQutkOSftxIFD5xw9aepkYNuPWmVJpffdDPYVpY=
github.com/pelletier/go-toml/v2 v2.4.3/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
main.go
package main

import (
	"fmt"
	"os"

	"github.com/pelletier/go-toml/v2"
)

type Config struct {
	Title   string   `toml:"title"`
	Version string   `toml:"version"`
	Ports   []int    `toml:"ports"`
	Server  Server   `toml:"server"`
}

type Server struct {
	Host string `toml:"host"`
	Port int    `toml:"port"`
}

func main() {
	doc := `
title = "Sample Application"
version = "1.0.0"
ports = [8080, 8081]

[server]
host = "127.0.0.1"
port = 8080
`

	var cfg Config
	if err := toml.Unmarshal([]byte(doc), &cfg); err != nil {
		fmt.Fprintf(os.Stderr, "failed to unmarshal: %v\n", err)
		os.Exit(1)
	}

	fmt.Printf("Parsed config title: %s\n", cfg.Title)
	fmt.Printf("Server host: %s:%d\n", cfg.Server.Host, cfg.Server.Port)

	out, err := toml.Marshal(cfg)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to marshal: %v\n", err)
		os.Exit(1)
	}

	fmt.Printf("Marshaled output:\n%s\n", string(out))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/github.com/pelletier/go-toml/v2@v2.4.3",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/pelletier/go-toml/v2@v2.4.3"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"fmt"
	"os"
	"strings"

	"github.com/pelletier/go-toml/v2"
)

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

type AppConfig struct {
	Name     string         `toml:"name"`
	Database DatabaseConfig `toml:"database"`
	Tags     []string       `toml:"tags"`
}

func main() {
	// Assertion 1: toml.Marshal serializes structs and maps into valid TOML bytes
	{
		cfg := AppConfig{
			Name: "test-app",
			Database: DatabaseConfig{
				Enabled: true,
				Host:    "localhost",
				Port:    5432,
			},
			Tags: []string{"backend", "v2"},
		}

		data, err := toml.Marshal(cfg)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: unexpected marshal error: %v\n", err)
			os.Exit(1)
		}

		text := string(data)
		if !strings.Contains(text, "name = 'test-app'") && !strings.Contains(text, `name = "test-app"`) {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: name field missing or incorrect in %s\n", text)
			os.Exit(1)
		}
		if !strings.Contains(text, "[database]") {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: [database] section missing in %s\n", text)
			os.Exit(1)
		}
	}

	// Assertion 2: toml.Unmarshal deserializes TOML bytes into target structs with matching fields
	{
		raw := `
name = "demo-service"
tags = ["alpha", "beta"]

[database]
enabled = true
host = "127.0.0.1"
port = 3306
`
		var parsed AppConfig
		if err := toml.Unmarshal([]byte(raw), &parsed); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: unexpected unmarshal error: %v\n", err)
			os.Exit(1)
		}

		if parsed.Name != "demo-service" {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: expected name 'demo-service', got '%s'\n", parsed.Name)
			os.Exit(1)
		}
		if !parsed.Database.Enabled || parsed.Database.Host != "127.0.0.1" || parsed.Database.Port != 3306 {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: database mismatch: %+v\n", parsed.Database)
			os.Exit(1)
		}
		if len(parsed.Tags) != 2 || parsed.Tags[0] != "alpha" || parsed.Tags[1] != "beta" {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: tags mismatch: %+v\n", parsed.Tags)
			os.Exit(1)
		}
	}

	// Assertion 3: toml.NewDecoder and toml.NewEncoder stream TOML content via io.Reader and io.Writer
	{
		source := `
name = "streamed-app"

[database]
enabled = false
host = "remotehost"
port = 9000
`
		var cfg AppConfig
		decoder := toml.NewDecoder(strings.NewReader(source))
		if err := decoder.Decode(&cfg); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: decode error: %v\n", err)
			os.Exit(1)
		}
		if cfg.Name != "streamed-app" || cfg.Database.Port != 9000 {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: decoded config mismatch: %+v\n", cfg)
			os.Exit(1)
		}

		var buf bytes.Buffer
		encoder := toml.NewEncoder(&buf)
		if err := encoder.Encode(cfg); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: encode error: %v\n", err)
			os.Exit(1)
		}
		if buf.Len() == 0 || !strings.Contains(buf.String(), "streamed-app") {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: encoded buffer invalid: %s\n", buf.String())
			os.Exit(1)
		}
	}

	// Assertion 4: toml.Unmarshal returns an error when parsing malformed TOML syntax
	{
		malformed := `
name = "unclosed-string
invalid toml line here = = =
`
		var target map[string]any
		err := toml.Unmarshal([]byte(malformed), &target)
		if err == nil {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: expected error for malformed TOML, got nil\n")
			os.Exit(1)
		}
	}

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

原始种子者

匿名