CodeSampleX

Exemplo

github.com/go-logfmt/logfmt v0.5.0: Encoder.EncodeKeyval

Amostra verificada para golang github.com/go-logfmt/logfmt v0.5.0: Encoder.EncodeKeyval. O contrato rodou em go 1.26 · linux debian/x64 · docker e passou.

sha256:c7be55960872d3f3417f8558b68b7a063b809b8e00f118bd1d3fa7dd888475d8

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

Caso

HOW
Objetivo
verify pkg:golang/github.com/go-logfmt/logfmt@v0.5.0
Pacotes
Símbolos
  • github.com/go-logfmt/logfmt.Encoder.EncodeKeyval
Criado
2026-09-03T18:59:18Z

Contrato

  1. logfmt.Encoder.EncodeKeyval writes the logfmt encoding of a single key and value to the output stream
  2. logfmt.Encoder.EncodeKeyval writes a single space separator before second and subsequent key-value pairs on the same record
  3. logfmt.Encoder.EncodeKeyval formats nil values as null
  4. logfmt.Encoder.EncodeKeyval returns ErrNilKey on nil key or nil pointer and writes nothing to the stream
  5. logfmt.Encoder.EncodeKeyval returns ErrInvalidKey when key contains only invalid runes and writes nothing to the stream
  6. logfmt.Encoder.EncodeKeyval returns ErrUnsupportedKeyType for array chan func map slice or struct key and writes nothing to the stream
  7. logfmt.Encoder.EncodeKeyval returns ErrUnsupportedValueType for unsupported complex types and writes nothing to the stream

Arquivos

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • sample.go
  • 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/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.Encoder.EncodeKeyval

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:abbe8b8081550de2c538216831908649ccb2947490e0f1de52694c076fbd09bc","contract":["logfmt.Encoder.EncodeKeyval writes the logfmt encoding of a single key and value to the output stream","logfmt.Encoder.EncodeKeyval writes a single space separator before second and subsequent key-value pairs on the same record","logfmt.Encoder.EncodeKeyval formats nil values as null","logfmt.Encoder.EncodeKeyval returns ErrNilKey on nil key or nil pointer and writes nothing to the stream","logfmt.Encoder.EncodeKeyval returns ErrInvalidKey when key contains only invalid runes and writes nothing to the stream","logfmt.Encoder.EncodeKeyval returns ErrUnsupportedKeyType for array chan func map slice or struct key and writes nothing to the stream","logfmt.Encoder.EncodeKeyval returns ErrUnsupportedValueType for unsupported complex types and writes nothing to the stream"],"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.Encoder.EncodeKeyval"]},"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.Encoder.EncodeKeyval"],"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 (
	"bytes"
	"io"

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

// LogEntry writes key-value pairs one by one using EncodeKeyval and ends the record with a newline.
func LogEntry(w io.Writer, kvs [][2]interface{}) error {
	enc := logfmt.NewEncoder(w)
	for _, kv := range kvs {
		if err := enc.EncodeKeyval(kv[0], kv[1]); err != nil {
			return err
		}
	}
	return enc.EndRecord()
}

// FormatPair encodes a single key-value pair to a string using EncodeKeyval.
func FormatPair(key, value interface{}) (string, error) {
	var buf bytes.Buffer
	enc := logfmt.NewEncoder(&buf)
	if err := enc.EncodeKeyval(key, value); err != nil {
		return "", err
	}
	return buf.String(), 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.Encoder.EncodeKeyval"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"errors"
	"fmt"
	"os"

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

func main() {
	// Assertion 1: logfmt.Encoder.EncodeKeyval writes the logfmt encoding of a single key and value to the output stream
	var buf bytes.Buffer
	enc := logfmt.NewEncoder(&buf)
	if err := enc.EncodeKeyval("msg", "hello world"); err != nil {
		fmt.Fprintf(os.Stderr, "Assertion 1 failed: %v\n", err)
		os.Exit(1)
	}
	if buf.String() != "msg=\"hello world\"" {
		fmt.Fprintf(os.Stderr, "Assertion 1 output mismatch: %q\n", buf.String())
		os.Exit(1)
	}

	// Assertion 2: logfmt.Encoder.EncodeKeyval writes a single space separator before second and subsequent key-value pairs on the same record
	if err := enc.EncodeKeyval("count", 42); err != nil {
		fmt.Fprintf(os.Stderr, "Assertion 2 failed: %v\n", err)
		os.Exit(1)
	}
	if buf.String() != "msg=\"hello world\" count=42" {
		fmt.Fprintf(os.Stderr, "Assertion 2 output mismatch: %q\n", buf.String())
		os.Exit(1)
	}
	if err := enc.EndRecord(); err != nil {
		fmt.Fprintf(os.Stderr, "EndRecord failed: %v\n", err)
		os.Exit(1)
	}
	if buf.String() != "msg=\"hello world\" count=42\n" {
		fmt.Fprintf(os.Stderr, "EndRecord output mismatch: %q\n", buf.String())
		os.Exit(1)
	}

	// Assertion 3: logfmt.Encoder.EncodeKeyval formats nil values as null
	buf.Reset()
	if err := enc.EncodeKeyval("data", nil); err != nil {
		fmt.Fprintf(os.Stderr, "Assertion 3 failed: %v\n", err)
		os.Exit(1)
	}
	if buf.String() != "data=null" {
		fmt.Fprintf(os.Stderr, "Assertion 3 output mismatch: %q\n", buf.String())
		os.Exit(1)
	}

	// Assertion 4: logfmt.Encoder.EncodeKeyval returns ErrNilKey on nil key or nil pointer and writes nothing to the stream
	buf.Reset()
	if err := enc.EncodeKeyval(nil, "val"); !errors.Is(err, logfmt.ErrNilKey) {
		fmt.Fprintf(os.Stderr, "Assertion 4 nil interface failed: expected ErrNilKey, got %v\n", err)
		os.Exit(1)
	}
	var nilPtr *string
	if err := enc.EncodeKeyval(nilPtr, "val"); !errors.Is(err, logfmt.ErrNilKey) {
		fmt.Fprintf(os.Stderr, "Assertion 4 nil pointer failed: expected ErrNilKey, got %v\n", err)
		os.Exit(1)
	}
	if buf.Len() != 0 {
		fmt.Fprintf(os.Stderr, "Assertion 4 stream mismatch: expected empty buffer, got %q\n", buf.String())
		os.Exit(1)
	}

	// Assertion 5: logfmt.Encoder.EncodeKeyval returns ErrInvalidKey when key contains only invalid runes and writes nothing to the stream
	buf.Reset()
	if err := enc.EncodeKeyval("   ===", "val"); !errors.Is(err, logfmt.ErrInvalidKey) {
		fmt.Fprintf(os.Stderr, "Assertion 5 failed: expected ErrInvalidKey, got %v\n", err)
		os.Exit(1)
	}
	if buf.Len() != 0 {
		fmt.Fprintf(os.Stderr, "Assertion 5 stream mismatch: expected empty buffer, got %q\n", buf.String())
		os.Exit(1)
	}

	// Assertion 6: logfmt.Encoder.EncodeKeyval returns ErrUnsupportedKeyType for array chan func map slice or struct key and writes nothing to the stream
	buf.Reset()
	type dummyKey struct{}
	if err := enc.EncodeKeyval(dummyKey{}, "val"); !errors.Is(err, logfmt.ErrUnsupportedKeyType) {
		fmt.Fprintf(os.Stderr, "Assertion 6 failed: expected ErrUnsupportedKeyType, got %v\n", err)
		os.Exit(1)
	}
	if buf.Len() != 0 {
		fmt.Fprintf(os.Stderr, "Assertion 6 stream mismatch: expected empty buffer, got %q\n", buf.String())
		os.Exit(1)
	}

	// Assertion 7: logfmt.Encoder.EncodeKeyval returns ErrUnsupportedValueType for unsupported complex types and writes nothing to the stream
	buf.Reset()
	type dummyVal struct{}
	if err := enc.EncodeKeyval("meta", dummyVal{}); !errors.Is(err, logfmt.ErrUnsupportedValueType) {
		fmt.Fprintf(os.Stderr, "Assertion 7 failed: expected ErrUnsupportedValueType, got %v\n", err)
		os.Exit(1)
	}
	if buf.Len() != 0 {
		fmt.Fprintf(os.Stderr, "Assertion 7 stream mismatch: expected empty buffer, got %q\n", buf.String())
		os.Exit(1)
	}

	// Helper verification: LogEntry and FormatPair
	pairStr, err := sample.FormatPair("status", "ok")
	if err != nil || pairStr != "status=ok" {
		fmt.Fprintf(os.Stderr, "FormatPair helper failed: %v, got %q\n", err, pairStr)
		os.Exit(1)
	}

	buf.Reset()
	err = sample.LogEntry(&buf, [][2]interface{}{
		{"module", "auth"},
		{"action", "login"},
	})
	if err != nil || buf.String() != "module=auth action=login\n" {
		fmt.Fprintf(os.Stderr, "LogEntry helper failed: %v, got %q\n", err, buf.String())
		os.Exit(1)
	}

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

Seeder de origem

anônimo