CodeSampleX

Beispiel

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

Verifiziertes Beispiel für golang gopkg.in/yaml.v2 v2.3.0: yaml.Unmarshal. Der Vertrag lief auf go 1.26 · linux debian/x64 · docker und bestand.

sha256:74ce4922348a26be83ac04cd48ef0edcad5dfb520bfd61f8c9bc4bcc1c9a60d2

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
1
Signaturschlüssel, die es gebaut haben
1
Deklarierte Umgebung linux 24 · ubuntu · glibc 2.39 x64 go

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
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

Fall

HOW
Ziel
verify pkg:golang/gopkg.in/yaml.v2@v2.3.0
Pakete
Symbole
  • yaml.Unmarshal
Erstellt
2026-09-04T19:41:52Z

Contract

  1. yaml.Unmarshal unmarshals valid YAML bytes into a struct with yaml struct tags
  2. yaml.Unmarshal unmarshals nested maps and slices into typed struct fields
  3. yaml.Unmarshal returns an error when parsing malformed YAML syntax
  4. yaml.Unmarshal unmarshals custom type implementing yaml.Unmarshaler interface
  5. yaml.Unmarshal returns TypeError when unmarshaling incompatible types

Dateien

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

Quellartefakt herunterladen (tar.gz)

Quelltext

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.v2@v2.3.0
Kind: HOW

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

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:5d8ad612b9bd8f33ea8be54f51a658b94c584a93c13e0e74d652f0f1c995f75f","contract":["yaml.Unmarshal unmarshals valid YAML bytes into a struct with yaml struct tags","yaml.Unmarshal unmarshals nested maps and slices into typed struct fields","yaml.Unmarshal returns an error when parsing malformed YAML syntax","yaml.Unmarshal unmarshals custom type implementing yaml.Unmarshaler interface","yaml.Unmarshal returns TypeError when unmarshaling incompatible types"],"goal":"verify 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.Unmarshal"]},"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.Unmarshal"],"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 (
	"fmt"
	"os"

	"gopkg.in/yaml.v2"
)

// ServerConfig holds the application configuration parsed from YAML.
type ServerConfig struct {
	Host     string            `yaml:"host"`
	Port     int               `yaml:"port"`
	Enabled  bool              `yaml:"enabled"`
	Tags     []string          `yaml:"tags,omitempty"`
	Settings map[string]string `yaml:"settings,omitempty"`
}

// ParseConfig decodes YAML bytes into a ServerConfig.
func ParseConfig(data []byte) (*ServerConfig, error) {
	var cfg ServerConfig
	if err := yaml.Unmarshal(data, &cfg); err != nil {
		return nil, fmt.Errorf("failed to unmarshal yaml: %w", err)
	}
	return &cfg, nil
}

func main() {
	rawYAML := []byte(`
host: localhost
port: 8080
enabled: true
tags:
  - web
  - production
settings:
  timeout: "30s"
`)

	cfg, err := ParseConfig(rawYAML)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error parsing configuration: %v\n", err)
		os.Exit(1)
	}

	fmt.Printf("Parsed ServerConfig: host=%s, port=%d, enabled=%t\n", cfg.Host, cfg.Port, cfg.Enabled)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/gopkg.in/yaml.v2@v2.3.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/gopkg.in/yaml.v2@v2.3.0"
  ],
  "symbols": [
    "yaml.Unmarshal"
  ]
}
test/contract.go
package main

import (
	"errors"
	"fmt"
	"os"

	"gopkg.in/yaml.v2"
)

type ServerConfig struct {
	Host     string            `yaml:"host"`
	Port     int               `yaml:"port"`
	Enabled  bool              `yaml:"enabled"`
	Tags     []string          `yaml:"tags,omitempty"`
	Settings map[string]string `yaml:"settings,omitempty"`
}

type PortNumber int

func (p *PortNumber) UnmarshalYAML(unmarshal func(interface{}) error) error {
	var raw int
	if err := unmarshal(&raw); err != nil {
		return err
	}
	if raw <= 0 || raw > 65535 {
		return fmt.Errorf("port must be between 1 and 65535, got %d", raw)
	}
	*p = PortNumber(raw)
	return nil
}

type CustomService struct {
	Name string     `yaml:"name"`
	Port PortNumber `yaml:"port"`
}

func main() {
	// Assertion 1: yaml.Unmarshal unmarshals valid YAML bytes into a struct with yaml struct tags
	validYAML := []byte("host: localhost\nport: 8080\nenabled: true\n")
	var srv ServerConfig
	if err := yaml.Unmarshal(validYAML, &srv); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: yaml.Unmarshal failed: %v\n", err)
		os.Exit(1)
	}
	if srv.Host != "localhost" || srv.Port != 8080 || !srv.Enabled {
		fmt.Fprintf(os.Stderr, "FAIL: struct fields mismatch: %+v\n", srv)
		os.Exit(1)
	}

	// Assertion 2: yaml.Unmarshal unmarshals nested maps and slices into typed struct fields
	nestedYAML := []byte("host: localhost\nport: 8080\nenabled: true\ntags:\n  - api\n  - v1\nsettings:\n  region: us-east\n")
	var nestedSrv ServerConfig
	if err := yaml.Unmarshal(nestedYAML, &nestedSrv); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: yaml.Unmarshal nested failed: %v\n", err)
		os.Exit(1)
	}
	if len(nestedSrv.Tags) != 2 || nestedSrv.Tags[0] != "api" || nestedSrv.Tags[1] != "v1" {
		fmt.Fprintf(os.Stderr, "FAIL: tags slice mismatch: %v\n", nestedSrv.Tags)
		os.Exit(1)
	}
	if nestedSrv.Settings["region"] != "us-east" {
		fmt.Fprintf(os.Stderr, "FAIL: settings map mismatch: %v\n", nestedSrv.Settings)
		os.Exit(1)
	}

	// Assertion 3: yaml.Unmarshal returns an error when parsing malformed YAML syntax
	malformedYAML := []byte("host: [unclosed sequence\nport: 8080\n")
	var malformedSrv ServerConfig
	if err := yaml.Unmarshal(malformedYAML, &malformedSrv); err == nil {
		fmt.Fprintf(os.Stderr, "FAIL: expected error for malformed YAML, got nil\n")
		os.Exit(1)
	}

	// Assertion 4: yaml.Unmarshal unmarshals custom type implementing yaml.Unmarshaler interface
	customYAML := []byte("name: proxy\nport: 8443\n")
	var customSvc CustomService
	if err := yaml.Unmarshal(customYAML, &customSvc); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: custom unmarshaler failed: %v\n", err)
		os.Exit(1)
	}
	if customSvc.Name != "proxy" || customSvc.Port != 8443 {
		fmt.Fprintf(os.Stderr, "FAIL: custom unmarshaler values mismatch: %+v\n", customSvc)
		os.Exit(1)
	}

	// Assertion 5: yaml.Unmarshal returns TypeError when unmarshaling incompatible types
	incompatibleYAML := []byte("host: localhost\nport: not-a-number\n")
	var incompatibleSrv ServerConfig
	incompatibleErr := yaml.Unmarshal(incompatibleYAML, &incompatibleSrv)
	if incompatibleErr == nil {
		fmt.Fprintf(os.Stderr, "FAIL: expected error for incompatible type, got nil\n")
		os.Exit(1)
	}
	var typeErr *yaml.TypeError
	if !errors.As(incompatibleErr, &typeErr) {
		fmt.Fprintf(os.Stderr, "FAIL: expected *yaml.TypeError, got %T (%v)\n", incompatibleErr, incompatibleErr)
		os.Exit(1)
	}

	fmt.Println("PASS: pkg:golang/gopkg.in/yaml.v2@v2.3.0 contract passed")
}

Ursprungs-Seeder

anonym