CodeSampleX

Exemple

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

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

sha256:f708ce68278e3e5e047d9c6403e27ec805b59e6e6698363a9e00e1a66f463384

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é go 1.26 linux 24 · ubuntu · glibc 2.39 x64 go 1.26 go go 1

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.NewEncoder in pkg:golang/gopkg.in/yaml.v2@v2.3.0
Paquets
Symboles
  • yaml.NewEncoder
Environnement
go 1.26.6
Créé
2026-09-04T21:54:36Z

Contrat

  1. yaml.NewEncoder encodes Go structs into YAML output and flushes on Close
  2. yaml.NewEncoder encodes multiple documents sequentially separated by document markers
  3. yaml.NewEncoder respects struct field tags for custom keys and omitempty behavior
  4. yaml.NewEncoder returns an error when writing to an error-returning io.Writer

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

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:ef8dcd2cb9dd0a6907d1a06ce2d5b0c2a2b28f06fb15d677af483290bdfac366","contract":["yaml.NewEncoder encodes Go structs into YAML output and flushes on Close","yaml.NewEncoder encodes multiple documents sequentially separated by document markers","yaml.NewEncoder respects struct field tags for custom keys and omitempty behavior","yaml.NewEncoder returns an error when writing to an error-returning io.Writer"],"goal":"verify yaml.NewEncoder 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.NewEncoder"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","language":"go","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","packageManagerVersion":"1.26.6","runtime":"go","runtimeVersion":"1.26.6","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.NewEncoder"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.22.0

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"
	"os"

	"gopkg.in/yaml.v2"
)

// Config holds sample service configuration.
type Config struct {
	AppName string   `yaml:"app_name"`
	Port    int      `yaml:"port"`
	Tags    []string `yaml:"tags,omitempty"`
}

func main() {
	var buf bytes.Buffer
	enc := yaml.NewEncoder(&buf)

	cfg1 := Config{
		AppName: "sample-service",
		Port:    8080,
		Tags:    []string{"web", "production"},
	}

	if err := enc.Encode(cfg1); err != nil {
		fmt.Fprintf(os.Stderr, "failed to encode doc 1: %v\n", err)
		os.Exit(1)
	}

	cfg2 := Config{
		AppName: "worker-service",
		Port:    9090,
	}

	if err := enc.Encode(cfg2); err != nil {
		fmt.Fprintf(os.Stderr, "failed to encode doc 2: %v\n", err)
		os.Exit(1)
	}

	if err := enc.Close(); err != nil {
		fmt.Fprintf(os.Stderr, "failed to close encoder: %v\n", err)
		os.Exit(1)
	}

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

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

	"gopkg.in/yaml.v2"
)

type testConfig struct {
	Title string `yaml:"title"`
	Port  int    `yaml:"port"`
}

type taggedItem struct {
	VisibleName string `yaml:"custom_name"`
	Ignored     string `yaml:"-"`
	OmitIfEmpty string `yaml:"optional_field,omitempty"`
}

type failingWriter struct{}

func (failingWriter) Write([]byte) (int, error) {
	return 0, fmt.Errorf("mock write failure")
}

func main() {
	// Assertion 1: yaml.NewEncoder encodes Go structs into YAML output and flushes on Close
	{
		var buf bytes.Buffer
		enc := yaml.NewEncoder(&buf)
		item := testConfig{
			Title: "Test Service",
			Port:  8080,
		}
		if err := enc.Encode(item); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: encode error: %v\n", err)
			os.Exit(1)
		}
		if err := enc.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: close error: %v\n", err)
			os.Exit(1)
		}
		expected := "title: Test Service\nport: 8080\n"
		if buf.String() != expected {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: expected %q, got %q\n", expected, buf.String())
			os.Exit(1)
		}
	}

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

	// Assertion 3: yaml.NewEncoder respects struct field tags for custom keys and omitempty behavior
	{
		var buf bytes.Buffer
		enc := yaml.NewEncoder(&buf)
		val := taggedItem{
			VisibleName: "hello",
			Ignored:     "secret",
			OmitIfEmpty: "",
		}
		if err := enc.Encode(val); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: encode error: %v\n", err)
			os.Exit(1)
		}
		if err := enc.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: close error: %v\n", err)
			os.Exit(1)
		}
		expected := "custom_name: hello\n"
		if buf.String() != expected {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: expected %q, got %q\n", expected, buf.String())
			os.Exit(1)
		}
	}

	// Assertion 4: yaml.NewEncoder returns an error when writing to an error-returning io.Writer
	{
		enc := yaml.NewEncoder(failingWriter{})
		err := enc.Encode(map[string]string{"key": "value"})
		if err == nil {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: expected write error, got nil\n")
			os.Exit(1)
		}
		if !strings.Contains(err.Error(), "mock write failure") {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: expected error containing 'mock write failure', got %q\n", err.Error())
			os.Exit(1)
		}
	}

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

Seeder d'origine

anonyme