CodeSampleX

Beispiel

github.com/go-logfmt/logfmt v0.5.0: MarshalKeyvals

Verifiziertes Beispiel für golang github.com/go-logfmt/logfmt v0.5.0: MarshalKeyvals. Der Vertrag lief auf go 1.26 · linux debian/x64 · docker und bestand.

sha256:6fb4245f53e54918d051b0dea910fe3bed11182fcdba921769a854db176c03cb

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

Fall

HOW
Ziel
verify pkg:golang/github.com/go-logfmt/logfmt@v0.5.0
Pakete
Symbole
  • github.com/go-logfmt/logfmt.MarshalKeyvals
Erstellt
2026-09-03T18:52:25Z

Contract

  1. logfmt.MarshalKeyvals serializes variadic key-value pairs into logfmt format without trailing newline
  2. logfmt.MarshalKeyvals correctly formats and quotes values containing spaces, quotes, and newlines
  3. logfmt.MarshalKeyvals formats types including booleans, errors, time.Time, and fmt.Stringer
  4. logfmt.MarshalKeyvals returns ErrNilKey when a nil key is provided
  5. logfmt.MarshalKeyvals returns ErrInvalidKey when a key contains only invalid runes like spaces or equals
  6. logfmt.MarshalKeyvals handles odd number of arguments by appending null value
  7. Helper functions in sample package format key-value pairs accurately

Dateien

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • sample.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/github.com/go-logfmt/logfmt@v0.5.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/go-logfmt/logfmt@v0.5.0
Demonstrate these symbols/APIs:
  - github.com/go-logfmt/logfmt.MarshalKeyvals

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:45042ec20eeef6b1c3c9006f3d38002eec0d0565b0a64e73728fe92aeba3fcf1","contract":["logfmt.MarshalKeyvals serializes variadic key-value pairs into logfmt format without trailing newline","logfmt.MarshalKeyvals correctly formats and quotes values containing spaces, quotes, and newlines","logfmt.MarshalKeyvals formats types including booleans, errors, time.Time, and fmt.Stringer","logfmt.MarshalKeyvals returns ErrNilKey when a nil key is provided","logfmt.MarshalKeyvals returns ErrInvalidKey when a key contains only invalid runes like spaces or equals","logfmt.MarshalKeyvals handles odd number of arguments by appending null value","Helper functions in sample package format key-value pairs accurately"],"goal":"verify pkg:golang/github.com/go-logfmt/logfmt@v0.5.0","kind":"HOW","packages":["pkg:golang/github.com/go-logfmt/logfmt@v0.5.0"],"schemaVersion":1,"symbols":["github.com/go-logfmt/logfmt.MarshalKeyvals"]},"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/github.com/go-logfmt/logfmt@v0.5.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/go-logfmt/logfmt@v0.5.0","symbols":["github.com/go-logfmt/logfmt.MarshalKeyvals"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require github.com/go-logfmt/logfmt v0.5.0
go.sum
github.com/go-logfmt/logfmt v0.5.0 h1:TrB8swr/68K7m9CcGut2g3UOihhbcbiMAYiuTXdEih4=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
sample.go
package sample

import (
	"github.com/go-logfmt/logfmt"
)

// FormatLog formats alternating key-value pairs into a logfmt byte slice.
func FormatLog(keyvals ...interface{}) ([]byte, error) {
	return logfmt.MarshalKeyvals(keyvals...)
}

// FormatLogString formats alternating key-value pairs into a logfmt string.
func FormatLogString(keyvals ...interface{}) (string, error) {
	b, err := logfmt.MarshalKeyvals(keyvals...)
	if err != nil {
		return "", err
	}
	return string(b), nil
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/github.com/go-logfmt/logfmt@v0.5.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/go-logfmt/logfmt@v0.5.0"
  ],
  "symbols": [
    "github.com/go-logfmt/logfmt.MarshalKeyvals"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"errors"
	"fmt"
	"os"
	"strings"
	"time"

	"github.com/go-logfmt/logfmt"
	"sample"
)

type customStringer struct {
	val string
}

func (c customStringer) String() string {
	return c.val
}

func main() {
	// Assertion 1: logfmt.MarshalKeyvals serializes variadic key-value pairs into logfmt format without trailing newline
	b1, err := logfmt.MarshalKeyvals("level", "info", "app", "demo", "count", 42)
	if err != nil {
		fmt.Fprintf(os.Stderr, "assertion 1 error: %v\n", err)
		os.Exit(1)
	}
	expected1 := "level=info app=demo count=42"
	if string(b1) != expected1 {
		fmt.Fprintf(os.Stderr, "assertion 1 mismatch: got %q, expected %q\n", string(b1), expected1)
		os.Exit(1)
	}
	if bytes.HasSuffix(b1, []byte("\n")) {
		fmt.Fprintf(os.Stderr, "assertion 1 failed: MarshalKeyvals should not append a trailing newline\n")
		os.Exit(1)
	}

	// Assertion 2: logfmt.MarshalKeyvals correctly formats and quotes values containing spaces, quotes, and newlines
	b2, err := logfmt.MarshalKeyvals("msg", "hello world", "quote", `quoted "value"`, "newline", "line1\nline2")
	if err != nil {
		fmt.Fprintf(os.Stderr, "assertion 2 error: %v\n", err)
		os.Exit(1)
	}
	got2 := string(b2)
	if !strings.Contains(got2, `msg="hello world"`) {
		fmt.Fprintf(os.Stderr, "assertion 2 failed: expected quoted spaces in msg, got %q\n", got2)
		os.Exit(1)
	}
	if !strings.Contains(got2, `quote="quoted \"value\""`) {
		fmt.Fprintf(os.Stderr, "assertion 2 failed: expected escaped quotes, got %q\n", got2)
		os.Exit(1)
	}

	// Assertion 3: logfmt.MarshalKeyvals formats types including booleans, errors, time.Time, and fmt.Stringer
	ts := time.Date(2026, 8, 27, 12, 0, 0, 0, time.UTC)
	testErr := errors.New("connection reset")
	stringer := customStringer{val: "custom-val"}
	b3, err := logfmt.MarshalKeyvals("time", ts, "active", true, "err", testErr, "custom", stringer)
	if err != nil {
		fmt.Fprintf(os.Stderr, "assertion 3 error: %v\n", err)
		os.Exit(1)
	}
	got3 := string(b3)
	expected3 := "time=2026-08-27T12:00:00Z active=true err=\"connection reset\" custom=custom-val"
	if got3 != expected3 {
		fmt.Fprintf(os.Stderr, "assertion 3 mismatch: got %q, expected %q\n", got3, expected3)
		os.Exit(1)
	}

	// Assertion 4: logfmt.MarshalKeyvals returns ErrNilKey when a nil key is provided
	_, errNil := logfmt.MarshalKeyvals(nil, "value")
	if !errors.Is(errNil, logfmt.ErrNilKey) {
		fmt.Fprintf(os.Stderr, "assertion 4 failed: expected ErrNilKey, got %v\n", errNil)
		os.Exit(1)
	}

	// Assertion 5: logfmt.MarshalKeyvals returns ErrInvalidKey when a key contains only invalid runes like spaces or equals
	_, errInvalid := logfmt.MarshalKeyvals("   ", "value")
	if !errors.Is(errInvalid, logfmt.ErrInvalidKey) {
		fmt.Fprintf(os.Stderr, "assertion 5 failed: expected ErrInvalidKey for whitespace key, got %v\n", errInvalid)
		os.Exit(1)
	}
	_, errInvalidEq := logfmt.MarshalKeyvals("=", "value")
	if !errors.Is(errInvalidEq, logfmt.ErrInvalidKey) {
		fmt.Fprintf(os.Stderr, "assertion 5 failed: expected ErrInvalidKey for equals key, got %v\n", errInvalidEq)
		os.Exit(1)
	}

	// Assertion 6: logfmt.MarshalKeyvals handles odd number of arguments by appending null value
	bOdd, errOdd := logfmt.MarshalKeyvals("key1", "val1", "key2")
	if errOdd != nil {
		fmt.Fprintf(os.Stderr, "assertion 6 failed with error: %v\n", errOdd)
		os.Exit(1)
	}
	if string(bOdd) != "key1=val1 key2=null" {
		fmt.Fprintf(os.Stderr, "assertion 6 mismatch: got %q, expected %q\n", string(bOdd), "key1=val1 key2=null")
		os.Exit(1)
	}

	// Assertion 7: Helper functions in sample package format key-value pairs accurately
	bHelper, err := sample.FormatLog("env", "production", "status", 200)
	if err != nil {
		fmt.Fprintf(os.Stderr, "assertion 7 FormatLog error: %v\n", err)
		os.Exit(1)
	}
	if string(bHelper) != "env=production status=200" {
		fmt.Fprintf(os.Stderr, "assertion 7 FormatLog mismatch: got %q\n", string(bHelper))
		os.Exit(1)
	}

	sHelper, err := sample.FormatLogString("env", "production", "status", 200)
	if err != nil {
		fmt.Fprintf(os.Stderr, "assertion 7 FormatLogString error: %v\n", err)
		os.Exit(1)
	}
	if sHelper != "env=production status=200" {
		fmt.Fprintf(os.Stderr, "assertion 7 FormatLogString mismatch: got %q\n", sHelper)
		os.Exit(1)
	}

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

Ursprungs-Seeder

anonym