CodeSampleX

示例

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

已验证示例 — golang github.com/go-logfmt/logfmt v0.5.0: MarshalKeyvals. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过: logfmt.MarshalKeyvals serializes…

sha256:6fb4245f53e54918d051b0dea910fe3bed11182fcdba921769a854db176c03cb

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 go

验证运行环境

环境 契约 阶段 运行日期
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

案例

HOW
目标
verify pkg:golang/github.com/go-logfmt/logfmt@v0.5.0
包
符号
  • github.com/go-logfmt/logfmt.MarshalKeyvals
创建时间
2026-09-03T18:52:25Z

契约

  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

文件

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

下载源代码构件 (tar.gz)

源代码

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.")
}

原始种子者

匿名