CodeSampleX

Exemple

gopkg.in/yaml.v2 v2.3.0: yaml.Encoder

Échantillon vérifié pour golang gopkg.in/yaml.v2 v2.3.0: yaml.Encoder. Le contrat s'est exécuté sur go 1.26 · linux debian/x64 · docker et a réussi.

sha256:0af4fb50dcdd3e5976a44009113154cbeb34028b35d96c519f2d7092e97dfe5b

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-04

Cas

HOW
Objectif
verify yaml.Encoder in pkg:golang/gopkg.in/yaml.v2@v2.3.0
Paquets
Symboles
  • yaml.Encoder
Créé
2026-09-04T21:48:10Z

Contrat

  1. yaml.NewEncoder writes serialized YAML data to an io.Writer
  2. yaml.Encoder encodes multiple documents sequentially separated by document markers
  3. yaml.Encoder encodes struct fields observing yaml struct tags including omitempty and ignored fields
  4. yaml.Encoder Close flushes output and subsequent Encode calls return an error

Fichiers

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

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 yaml.Encoder in pkg:golang/gopkg.in/yaml.v2@v2.3.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/gopkg.in/yaml.v2@v2.3.0
Demonstrate these symbols/APIs:
  - yaml.Encoder

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:0fc79444eaa7b964f09001a7bbf69136c75877747960f835afb2d2fe1b36ec88","contract":["yaml.NewEncoder writes serialized YAML data to an io.Writer","yaml.Encoder encodes multiple documents sequentially separated by document markers","yaml.Encoder encodes struct fields observing yaml struct tags including omitempty and ignored fields","yaml.Encoder Close flushes output and subsequent Encode calls return an error"],"goal":"verify yaml.Encoder in pkg:golang/gopkg.in/yaml.v2@v2.3.0","kind":"HOW","packages":["pkg:golang/gopkg.in/yaml.v2@v2.3.0"],"schemaVersion":1,"symbols":["yaml.Encoder"]},"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/gopkg.in/yaml.v2@v2.3.0"],"schemaVersion":1,"subject":"pkg:golang/gopkg.in/yaml.v2@v2.3.0","symbols":["yaml.Encoder"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require gopkg.in/yaml.v2 v2.3.0
go.sum
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.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
main.go
package main

import (
	"bytes"
	"fmt"
	"io"
	"os"

	"gopkg.in/yaml.v2"
)

// Document represents a generic configuration entry to encode into YAML.
type Document struct {
	Kind     string            `yaml:"kind"`
	Name     string            `yaml:"name"`
	Metadata map[string]string `yaml:"metadata,omitempty"`
}

// EncodeDocuments serializes multiple Document values to the destination writer using yaml.Encoder.
func EncodeDocuments(w io.Writer, docs []Document) error {
	enc := yaml.NewEncoder(w)
	defer enc.Close()

	for _, doc := range docs {
		if err := enc.Encode(doc); err != nil {
			return fmt.Errorf("failed to encode document: %w", err)
		}
	}
	return nil
}

func main() {
	docs := []Document{
		{
			Kind: "Service",
			Name: "web-server",
			Metadata: map[string]string{
				"env": "production",
			},
		},
		{
			Kind: "ConfigMap",
			Name: "app-config",
		},
	}

	var buf bytes.Buffer
	if err := EncodeDocuments(&buf, docs); err != nil {
		fmt.Fprintf(os.Stderr, "Error: %v\n", err)
		os.Exit(1)
	}

	fmt.Print(buf.String())
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify yaml.Encoder in pkg:golang/gopkg.in/yaml.v2@v2.3.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/gopkg.in/yaml.v2@v2.3.0"
  ],
  "symbols": [
    "yaml.Encoder"
  ]
}
test/contract.go
package main

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

	"gopkg.in/yaml.v2"
)

type ResourceConfig struct {
	Title  string   `yaml:"title"`
	Port   int      `yaml:"port"`
	Tags   []string `yaml:"tags,omitempty"`
	Secret string   `yaml:"-"`
}

func main() {
	// Assertion 1: yaml.NewEncoder writes serialized YAML data to an io.Writer
	var buf1 bytes.Buffer
	enc1 := yaml.NewEncoder(&buf1)
	item := ResourceConfig{
		Title: "web",
		Port:  8080,
	}
	if err := enc1.Encode(item); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: assertion 1 encode failed: %v\n", err)
		os.Exit(1)
	}
	if err := enc1.Close(); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: assertion 1 close failed: %v\n", err)
		os.Exit(1)
	}
	expectedSingle := "title: web\nport: 8080\n"
	if buf1.String() != expectedSingle {
		fmt.Fprintf(os.Stderr, "FAIL: assertion 1 expected %q, got %q\n", expectedSingle, buf1.String())
		os.Exit(1)
	}

	// Assertion 2: yaml.Encoder encodes multiple documents sequentially separated by document markers
	var buf2 bytes.Buffer
	enc2 := yaml.NewEncoder(&buf2)
	doc1 := map[string]string{"name": "doc1"}
	doc2 := map[string]string{"name": "doc2"}
	if err := enc2.Encode(doc1); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: assertion 2 encode doc1 failed: %v\n", err)
		os.Exit(1)
	}
	if err := enc2.Encode(doc2); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: assertion 2 encode doc2 failed: %v\n", err)
		os.Exit(1)
	}
	if err := enc2.Close(); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: assertion 2 close failed: %v\n", err)
		os.Exit(1)
	}
	expectedMulti := "name: doc1\n---\nname: doc2\n"
	if buf2.String() != expectedMulti {
		fmt.Fprintf(os.Stderr, "FAIL: assertion 2 expected %q, got %q\n", expectedMulti, buf2.String())
		os.Exit(1)
	}

	// Assertion 3: yaml.Encoder encodes struct fields observing yaml struct tags including omitempty and ignored fields
	var buf3 bytes.Buffer
	enc3 := yaml.NewEncoder(&buf3)
	tagged := ResourceConfig{
		Title:  "gateway",
		Port:   443,
		Tags:   nil,
		Secret: "do-not-serialize",
	}
	if err := enc3.Encode(tagged); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: assertion 3 encode failed: %v\n", err)
		os.Exit(1)
	}
	if err := enc3.Close(); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: assertion 3 close failed: %v\n", err)
		os.Exit(1)
	}
	out3 := buf3.String()
	if strings.Contains(out3, "secret") || strings.Contains(out3, "do-not-serialize") {
		fmt.Fprintf(os.Stderr, "FAIL: assertion 3 secret was not ignored: %s\n", out3)
		os.Exit(1)
	}
	if strings.Contains(out3, "tags") {
		fmt.Fprintf(os.Stderr, "FAIL: assertion 3 empty tags was not omitted: %s\n", out3)
		os.Exit(1)
	}
	if !strings.Contains(out3, "title: gateway") || !strings.Contains(out3, "port: 443") {
		fmt.Fprintf(os.Stderr, "FAIL: assertion 3 expected fields missing: %s\n", out3)
		os.Exit(1)
	}

	// Assertion 4: yaml.Encoder Close flushes output and subsequent Encode calls return an error
	var buf4 bytes.Buffer
	enc4 := yaml.NewEncoder(&buf4)
	if err := enc4.Encode(map[string]string{"status": "ok"}); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: assertion 4 initial encode failed: %v\n", err)
		os.Exit(1)
	}
	if err := enc4.Close(); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: assertion 4 close failed: %v\n", err)
		os.Exit(1)
	}
	errAfterClose := enc4.Encode(map[string]string{"status": "another"})
	if errAfterClose == nil {
		fmt.Fprintf(os.Stderr, "FAIL: assertion 4 expected error encoding after close, got nil\n")
		os.Exit(1)
	}

	fmt.Println("PASS: yaml.Encoder contract passed")
}

Seeder d'origine

anonyme