CodeSampleX

Exemplo

gopkg.in/yaml.v3 v3.0.1

Amostra verificada para golang gopkg.in/yaml.v3 v3.0.1. O contrato rodou em go 1.26 · linux alpine/x64 · docker e passou: yaml.Unmarshal parses YAML…

sha256:db6dca2a91841f6de8a86f243f1187ccbbe00fe3b89c7f173df4c169ec47a658

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
1
Chaves de assinatura que o compilaram
1
Ambiente declarado linux 24 · ubuntu · glibc 2.39 x64 go

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
go 1.26 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f…
2026-08-23

Caso

HOW
Objetivo
verify pkg:golang/gopkg.in/yaml.v3@v3.0.1
Pacotes
Criado
2026-08-23T06:05:53Z

Contrato

  1. yaml.Unmarshal parses YAML documents into Go struct fields with yaml tags
  2. yaml.Marshal serializes Go structs to YAML format
  3. yaml.Node decodes and preserves YAML document structure
  4. yaml.NewDecoder and yaml.NewEncoder stream YAML documents

Arquivos

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

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

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/gopkg.in/yaml.v3@v3.0.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/gopkg.in/yaml.v3@v3.0.1

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:a4062d32eee4a0819244bf0fe70ecf8462b6133f069f4488c9074d2320c1ad59","contract":["yaml.Unmarshal parses YAML documents into Go struct fields with yaml tags","yaml.Marshal serializes Go structs to YAML format","yaml.Node decodes and preserves YAML document structure","yaml.NewDecoder and yaml.NewEncoder stream YAML documents"],"goal":"verify pkg:golang/gopkg.in/yaml.v3@v3.0.1","kind":"HOW","packages":["pkg:golang/gopkg.in/yaml.v3@v3.0.1"],"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/gopkg.in/yaml.v3@v3.0.1"],"schemaVersion":1,"subject":"pkg:golang/gopkg.in/yaml.v3@v3.0.1","verifierAdapter":"golang@1"}
go.mod
module sample

go 1.21

require gopkg.in/yaml.v3 v3.0.1
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.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/gopkg.in/yaml.v3@v3.0.1",
  "kind": "HOW",
  "packages": [
    "pkg:golang/gopkg.in/yaml.v3@v3.0.1"
  ]
}
test/contract.go
package main

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

	"gopkg.in/yaml.v3"
)

type ServerConfig struct {
	Host    string            `yaml:"host"`
	Port    int               `yaml:"port"`
	Enabled bool              `yaml:"enabled"`
	Tags    []string          `yaml:"tags"`
	Meta    map[string]string `yaml:"meta"`
}

type AppConfig struct {
	Version string       `yaml:"version"`
	Server  ServerConfig `yaml:"server"`
}

func main() {
	if err := runTests(); err != nil {
		fmt.Fprintf(os.Stderr, "Contract test failed: %v\n", err)
		os.Exit(1)
	}
	fmt.Println("All contract tests passed successfully.")
}

func runTests() error {
	// 1. Test yaml.Unmarshal with struct tags, nested structs, slices, and maps
	yamlData := `
version: "1.0.0"
server:
  host: example.com
  port: 8080
  enabled: true
  tags:
    - web
    - api
  meta:
    env: production
`
	var cfg AppConfig
	if err := yaml.Unmarshal([]byte(yamlData), &cfg); err != nil {
		return fmt.Errorf("yaml.Unmarshal failed: %w", err)
	}

	if cfg.Version != "1.0.0" {
		return fmt.Errorf("expected version '1.0.0', got '%s'", cfg.Version)
	}
	if cfg.Server.Host != "example.com" || cfg.Server.Port != 8080 || !cfg.Server.Enabled {
		return fmt.Errorf("unexpected server config: %+v", cfg.Server)
	}
	if !reflect.DeepEqual(cfg.Server.Tags, []string{"web", "api"}) {
		return fmt.Errorf("unexpected tags: %+v", cfg.Server.Tags)
	}
	if cfg.Server.Meta["env"] != "production" {
		return fmt.Errorf("unexpected meta env: %s", cfg.Server.Meta["env"])
	}

	// 2. Test yaml.Marshal
	out, err := yaml.Marshal(&cfg)
	if err != nil {
		return fmt.Errorf("yaml.Marshal failed: %w", err)
	}
	if !strings.Contains(string(out), "example.com") || !strings.Contains(string(out), "8080") {
		return fmt.Errorf("marshaled YAML missing expected content: %s", string(out))
	}

	// 3. Test yaml.Node (AST parsing and manipulation)
	var node yaml.Node
	if err := yaml.Unmarshal([]byte(yamlData), &node); err != nil {
		return fmt.Errorf("yaml.Unmarshal into yaml.Node failed: %w", err)
	}
	if node.Kind != yaml.DocumentNode || len(node.Content) == 0 {
		return fmt.Errorf("expected DocumentNode with content, got kind=%v len=%d", node.Kind, len(node.Content))
	}
	rootMap := node.Content[0]
	if rootMap.Kind != yaml.MappingNode {
		return fmt.Errorf("expected root node to be MappingNode, got kind=%v", rootMap.Kind)
	}

	// 4. Test yaml.Decoder and yaml.Encoder with streams
	var buf bytes.Buffer
	enc := yaml.NewEncoder(&buf)
	if err := enc.Encode(&cfg); err != nil {
		return fmt.Errorf("yaml.NewEncoder Encode failed: %w", err)
	}
	if err := enc.Close(); err != nil {
		return fmt.Errorf("yaml.NewEncoder Close failed: %w", err)
	}

	var decodedCfg AppConfig
	dec := yaml.NewDecoder(&buf)
	if err := dec.Decode(&decodedCfg); err != nil {
		return fmt.Errorf("yaml.NewDecoder Decode failed: %w", err)
	}
	if !reflect.DeepEqual(cfg, decodedCfg) {
		return fmt.Errorf("encoder/decoder roundtrip mismatch: %+v vs %+v", cfg, decodedCfg)
	}

	return nil
}

Seeder de origem

anônimo