CodeSampleX

Beispiel

go.yaml.in/yaml/v2 v2.4.4: Marshal

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

sha256:e060b232d5c642a6ff7c49e93028c235f1ec0f5adeeb667d3d78b7c350689091

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

Fall

HOW
Ziel
verify go.yaml.in/yaml/v2.Marshal in pkg:golang/go.yaml.in/yaml/v2@v2.4.4
Pakete
Symbole
  • go.yaml.in/yaml/v2.Marshal
Erstellt
2026-09-06T13:02:29Z

Contract

  1. yaml.Marshal serializes primitive types, structs, slices, and maps into standard YAML
  2. yaml.Marshal respects struct field tags for custom key names, omitempty, and ignored fields
  3. yaml.Marshal supports inlining embedded structs using the inline tag
  4. yaml.Marshal applies flow style to collections when flow tag is present
  5. yaml.Marshal invokes MarshalYAML on types implementing the Marshaler interface
  6. yaml.Marshal propagates errors returned by types implementing the Marshaler interface

Dateien

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • 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 go.yaml.in/yaml/v2.Marshal in pkg:golang/go.yaml.in/yaml/v2@v2.4.4
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/go.yaml.in/yaml/v2@v2.4.4
Demonstrate these symbols/APIs:
  - go.yaml.in/yaml/v2.Marshal

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:ca4122add92a100006b1ba89a9dfdf0b3cb55a2a70bab285ddcbf7026b42a2f2","contract":["yaml.Marshal serializes primitive types, structs, slices, and maps into standard YAML","yaml.Marshal respects struct field tags for custom key names, omitempty, and ignored fields","yaml.Marshal supports inlining embedded structs using the inline tag","yaml.Marshal applies flow style to collections when flow tag is present","yaml.Marshal invokes MarshalYAML on types implementing the Marshaler interface","yaml.Marshal propagates errors returned by types implementing the Marshaler interface"],"goal":"verify go.yaml.in/yaml/v2.Marshal in pkg:golang/go.yaml.in/yaml/v2@v2.4.4","kind":"HOW","packages":["pkg:golang/go.yaml.in/yaml/v2@v2.4.4"],"schemaVersion":1,"symbols":["go.yaml.in/yaml/v2.Marshal"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","language":"go","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/go.yaml.in/yaml/v2@v2.4.4"],"schemaVersion":1,"subject":"pkg:golang/go.yaml.in/yaml/v2@v2.4.4","symbols":["go.yaml.in/yaml/v2.Marshal"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require go.yaml.in/yaml/v2 v2.4.4
go.sum
go.yaml.in/yaml/v2 v2.4.4 h1:tuyd0P+2Ont/d6e2rl3be67goVK4R6deVxCUX5vyPaQ=
go.yaml.in/yaml/v2 v2.4.4/go.mod h1:gMZqIpDtDqOfM0uNfy0SkpRhvUryYH0Z6wdMYcacYXQ=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify go.yaml.in/yaml/v2.Marshal in pkg:golang/go.yaml.in/yaml/v2@v2.4.4",
  "kind": "HOW",
  "packages": [
    "pkg:golang/go.yaml.in/yaml/v2@v2.4.4"
  ],
  "symbols": [
    "go.yaml.in/yaml/v2.Marshal"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"
	"strings"

	yaml "go.yaml.in/yaml/v2"
)

// 1. Types for testing basic serialization and struct tags
type ServerConfig struct {
	Host       string            `yaml:"host"`
	Port       int               `yaml:"port"`
	Active     bool              `yaml:"active"`
	Labels     []string          `yaml:"labels,omitempty"`
	Settings   map[string]string `yaml:"settings,omitempty"`
	SecretKey  string            `yaml:"-"`
	CustomName string            `yaml:"service_name"`
}

// 2. Types for inline tag testing
type BaseMetadata struct {
	Version string `yaml:"version"`
}

type ServiceDefinition struct {
	Name         string `yaml:"name"`
	BaseMetadata `yaml:",inline"`
}

// 3. Types for flow style testing
type FlowConfig struct {
	Endpoints []string `yaml:"endpoints,flow"`
}

// 4. Types for custom Marshaler interface testing
type StatusValue struct {
	code int
}

func (s StatusValue) MarshalYAML() (interface{}, error) {
	switch s.code {
	case 200:
		return "OK", nil
	case 404:
		return "NOT_FOUND", nil
	default:
		return nil, fmt.Errorf("invalid status code: %d", s.code)
	}
}

type StatusReport struct {
	Status StatusValue `yaml:"status"`
}

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

func runContractTests() error {
	// 1. yaml.Marshal serializes primitive types, structs, slices, and maps into standard YAML
	cfg := ServerConfig{
		Host:       "127.0.0.1",
		Port:       8080,
		Active:     true,
		Labels:     []string{"web", "prod"},
		Settings:   map[string]string{"timeout": "30s", "mode": "fast"},
		SecretKey:  "supersecret",
		CustomName: "api-service",
	}

	out, err := yaml.Marshal(&cfg)
	if err != nil {
		return fmt.Errorf("yaml.Marshal basic failed: %w", err)
	}

	yamlStr := string(out)
	if !strings.Contains(yamlStr, "host: 127.0.0.1") ||
		!strings.Contains(yamlStr, "port: 8080") ||
		!strings.Contains(yamlStr, "active: true") {
		return fmt.Errorf("yaml.Marshal output missing scalar fields: %s", yamlStr)
	}

	// 2. yaml.Marshal respects struct field tags for custom key names, omitempty, and ignored fields
	if !strings.Contains(yamlStr, "service_name: api-service") {
		return fmt.Errorf("custom key tag failed: %s", yamlStr)
	}
	if strings.Contains(yamlStr, "supersecret") || strings.Contains(yamlStr, "SecretKey") {
		return fmt.Errorf("ignored field '-' was included: %s", yamlStr)
	}

	// Verify omitempty
	emptyCfg := ServerConfig{
		Host:       "localhost",
		Port:       9000,
		Active:     false,
		CustomName: "test-service",
	}
	emptyOut, err := yaml.Marshal(&emptyCfg)
	if err != nil {
		return fmt.Errorf("yaml.Marshal empty failed: %w", err)
	}
	emptyStr := string(emptyOut)
	if strings.Contains(emptyStr, "labels:") || strings.Contains(emptyStr, "settings:") {
		return fmt.Errorf("omitempty failed, empty fields present: %s", emptyStr)
	}

	// 3. yaml.Marshal supports inlining embedded structs using the inline tag
	svc := ServiceDefinition{
		Name: "auth",
		BaseMetadata: BaseMetadata{
			Version: "v1.2.3",
		},
	}
	inlineOut, err := yaml.Marshal(&svc)
	if err != nil {
		return fmt.Errorf("yaml.Marshal inline failed: %w", err)
	}
	inlineStr := string(inlineOut)
	if !strings.Contains(inlineStr, "name: auth") || !strings.Contains(inlineStr, "version: v1.2.3") {
		return fmt.Errorf("inline tag failed: %s", inlineStr)
	}
	if strings.Contains(inlineStr, "basemetadata:") {
		return fmt.Errorf("inline field was nested: %s", inlineStr)
	}

	// 4. yaml.Marshal applies flow style to collections when flow tag is present
	flowCfg := FlowConfig{
		Endpoints: []string{"api1", "api2"},
	}
	flowOut, err := yaml.Marshal(&flowCfg)
	if err != nil {
		return fmt.Errorf("yaml.Marshal flow failed: %w", err)
	}
	flowStr := string(flowOut)
	if !strings.Contains(flowStr, "endpoints: [api1, api2]") {
		return fmt.Errorf("flow tag failed: %s", flowStr)
	}

	// 5. yaml.Marshal invokes MarshalYAML on types implementing the Marshaler interface
	rep := StatusReport{
		Status: StatusValue{code: 200},
	}
	repOut, err := yaml.Marshal(&rep)
	if err != nil {
		return fmt.Errorf("yaml.Marshal custom Marshaler failed: %w", err)
	}
	if !strings.Contains(string(repOut), "status: OK") {
		return fmt.Errorf("custom Marshaler failed: %s", string(repOut))
	}

	// 6. yaml.Marshal propagates errors returned by types implementing the Marshaler interface
	repErr := StatusReport{
		Status: StatusValue{code: 500},
	}
	if _, err := yaml.Marshal(&repErr); err == nil {
		return fmt.Errorf("expected error from custom Marshaler, got nil")
	}

	// Verify roundtrip fidelity with a map to ensure valid YAML produced
	var parsed map[string]interface{}
	if err := yaml.Unmarshal(out, &parsed); err != nil {
		return fmt.Errorf("unmarshaling generated YAML failed: %w", err)
	}
	if parsed["service_name"] != "api-service" {
		return fmt.Errorf("roundtrip key check failed: %v", parsed)
	}

	return nil
}

Ursprungs-Seeder

anonym