CodeSampleX

Exemple

github.com/google/uuid v1.6.0: Convert raw byte slices, parse formatted byte sequences, and marshal binary/text representations with uuid.FromBytes, uuid.Must, uuid.ParseBytes, and uuid.MarshalBinary in google/uuid

Échantillon vérifié pour golang github.com/google/uuid v1.6.0: Convert raw byte slices, parse formatted byte sequences, and marshal binary/text…

sha256:263ff5e099274144065a3770758e5789c1847324fc904738d3dc8ab4a5d53582

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
2
Clés de signature qui l’ont compilé
2
Environnement déclaré linux 24 · ubuntu · glibc 2.39 x64 go wsl

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
go 1.26 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1
2026-08-19
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-07

Cas

HOW
Objectif
Convert raw byte slices, parse formatted byte sequences, and marshal binary/text representations with uuid.FromBytes, uuid.Must, uuid.ParseBytes, and uuid.MarshalBinary in google/uuid
Paquets
Symboles
  • uuid.FromBytes
  • uuid.Must
  • uuid.ParseBytes
  • uuid.UUID.MarshalBinary
  • uuid.UUID.MarshalText
  • uuid.UUID.UnmarshalBinary
  • uuid.UUID.UnmarshalText
Créé
2026-08-18T19:22:15Z

Contrat

  1. assert FromBytes creates valid UUID from 16-byte slice and returns error on invalid length
  2. assert Must returns UUID when err is nil and panics when err is non-nil
  3. assert ParseBytes parses formatted byte slices and rejects invalid byte sequences
  4. assert MarshalBinary produces 16-byte raw slice and UnmarshalBinary restores original UUID or returns error on invalid length
  5. assert MarshalText produces canonical 36-byte ASCII representation and UnmarshalText parses text into UUID

Fichiers

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

Télécharger l’artefact source (tar.gz)

Code source

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: Convert raw byte slices, parse formatted byte sequences, and marshal binary/text representations with uuid.FromBytes, uuid.Must, uuid.ParseBytes, and uuid.MarshalBinary in google/uuid
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/google/uuid@v1.6.0
Demonstrate these symbols/APIs:
  - uuid.FromBytes
  - uuid.Must
  - uuid.ParseBytes
  - uuid.UUID.MarshalBinary
  - uuid.UUID.MarshalText
  - uuid.UUID.UnmarshalBinary
  - uuid.UUID.UnmarshalText

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:5d973088b8bbf4168d5bb9fd51b0f73091c43511bdf86dde380a6234f2d14025","contract":["assert FromBytes creates valid UUID from 16-byte slice and returns error on invalid length","assert Must returns UUID when err is nil and panics when err is non-nil","assert ParseBytes parses formatted byte slices and rejects invalid byte sequences","assert MarshalBinary produces 16-byte raw slice and UnmarshalBinary restores original UUID or returns error on invalid length","assert MarshalText produces canonical 36-byte ASCII representation and UnmarshalText parses text into UUID"],"goal":"Convert raw byte slices, parse formatted byte sequences, and marshal binary/text representations with uuid.FromBytes, uuid.Must, uuid.ParseBytes, and uuid.MarshalBinary in google/uuid","kind":"HOW","packages":["pkg:golang/github.com/google/uuid@v1.6.0"],"schemaVersion":1,"symbols":["uuid.FromBytes","uuid.Must","uuid.ParseBytes","uuid.UUID.MarshalBinary","uuid.UUID.MarshalText","uuid.UUID.UnmarshalBinary","uuid.UUID.UnmarshalText"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1,"virtualization":"wsl"},"license":"MIT-0","packages":["pkg:golang/github.com/google/uuid@v1.6.0"],"schemaVersion":1,"symbols":["uuid.FromBytes","uuid.Must","uuid.ParseBytes","uuid.UUID.MarshalBinary","uuid.UUID.MarshalText","uuid.UUID.UnmarshalBinary","uuid.UUID.UnmarshalText"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.22

require github.com/google/uuid v1.6.0
go.sum
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
main.go
package main

import (
	"fmt"
	"os"

	"github.com/google/uuid"
)

func main() {
	// Sample 16-byte raw UUID slice
	rawBytes := []byte{
		0x6b, 0xa7, 0xb8, 0x10,
		0x9d, 0xad,
		0x11, 0xd1,
		0x80, 0xb4,
		0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8,
	}

	// 1. Convert raw 16-byte slice with FromBytes
	id, err := uuid.FromBytes(rawBytes)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FromBytes error: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("FromBytes UUID: %s\n", id.String())

	// 2. Wrap parser calls with Must helper
	guaranteedID := uuid.Must(uuid.FromBytes(rawBytes))
	fmt.Printf("Must UUID:      %s\n", guaranteedID.String())

	// 3. Parse formatted byte slice with ParseBytes
	formattedBytes := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
	parsedID, err := uuid.ParseBytes(formattedBytes)
	if err != nil {
		fmt.Fprintf(os.Stderr, "ParseBytes error: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("ParseBytes UUID: %s\n", parsedID.String())

	// 4. Binary marshaling and unmarshaling
	binData, err := id.MarshalBinary()
	if err != nil {
		fmt.Fprintf(os.Stderr, "MarshalBinary error: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("MarshalBinary:  %x (len: %d)\n", binData, len(binData))

	var unmarshaledBin uuid.UUID
	if err := unmarshaledBin.UnmarshalBinary(binData); err != nil {
		fmt.Fprintf(os.Stderr, "UnmarshalBinary error: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("UnmarshalBinary: %s (matches: %t)\n", unmarshaledBin.String(), unmarshaledBin == id)

	// 5. Text marshaling and unmarshaling
	textData, err := id.MarshalText()
	if err != nil {
		fmt.Fprintf(os.Stderr, "MarshalText error: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("MarshalText:    %s (len: %d)\n", string(textData), len(textData))

	var unmarshaledText uuid.UUID
	if err := unmarshaledText.UnmarshalText(textData); err != nil {
		fmt.Fprintf(os.Stderr, "UnmarshalText error: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("UnmarshalText:  %s (matches: %t)\n", unmarshaledText.String(), unmarshaledText == id)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "Convert raw byte slices, parse formatted byte sequences, and marshal binary/text representations with uuid.FromBytes, uuid.Must, uuid.ParseBytes, and uuid.MarshalBinary in google/uuid",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/google/uuid@v1.6.0"
  ],
  "symbols": [
    "uuid.FromBytes",
    "uuid.Must",
    "uuid.ParseBytes",
    "uuid.UUID.MarshalBinary",
    "uuid.UUID.MarshalText",
    "uuid.UUID.UnmarshalBinary",
    "uuid.UUID.UnmarshalText"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"encoding"
	"fmt"
	"os"

	"github.com/google/uuid"
)

func main() {
	sampleBytes := []byte{
		0x6b, 0xa7, 0xb8, 0x10,
		0x9d, 0xad,
		0x11, 0xd1,
		0x80, 0xb4,
		0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8,
	}
	expectedString := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"

	// 1. assert FromBytes creates valid UUID from 16-byte slice and returns error on invalid length
	id, err := uuid.FromBytes(sampleBytes)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: FromBytes failed on valid 16-byte slice: %v\n", err)
		os.Exit(1)
	}
	if id.String() != expectedString {
		fmt.Fprintf(os.Stderr, "FAIL: FromBytes mismatch: got %s, want %s\n", id.String(), expectedString)
		os.Exit(1)
	}

	for _, invalidLen := range []int{0, 1, 15, 17, 32} {
		badSlice := make([]byte, invalidLen)
		badID, badErr := uuid.FromBytes(badSlice)
		if badErr == nil {
			fmt.Fprintf(os.Stderr, "FAIL: FromBytes expected error for length %d, got nil\n", invalidLen)
			os.Exit(1)
		}
		if badID != uuid.Nil {
			fmt.Fprintf(os.Stderr, "FAIL: FromBytes expected uuid.Nil on error, got %s\n", badID.String())
			os.Exit(1)
		}
	}

	// 2. assert Must returns UUID when err is nil and panics when err is non-nil
	mustID := uuid.Must(uuid.FromBytes(sampleBytes))
	if mustID != id {
		fmt.Fprintf(os.Stderr, "FAIL: Must mismatch: got %s, want %s\n", mustID.String(), id.String())
		os.Exit(1)
	}

	panicked := false
	func() {
		defer func() {
			if r := recover(); r != nil {
				panicked = true
			}
		}()
		_ = uuid.Must(uuid.FromBytes([]byte{1, 2, 3}))
	}()
	if !panicked {
		fmt.Fprintf(os.Stderr, "FAIL: Must did not panic on error\n")
		os.Exit(1)
	}

	// 3. assert ParseBytes parses formatted byte slices and rejects invalid byte sequences
	parsedFromBytes, err := uuid.ParseBytes([]byte(expectedString))
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: ParseBytes failed on canonical text bytes: %v\n", err)
		os.Exit(1)
	}
	if parsedFromBytes != id {
		fmt.Fprintf(os.Stderr, "FAIL: ParseBytes mismatch: got %s, want %s\n", parsedFromBytes.String(), id.String())
		os.Exit(1)
	}

	if _, err := uuid.ParseBytes([]byte("not-a-valid-uuid-bytes")); err == nil {
		fmt.Fprintf(os.Stderr, "FAIL: ParseBytes expected error on invalid input, got nil\n")
		os.Exit(1)
	}

	// 4. assert MarshalBinary produces 16-byte raw slice and UnmarshalBinary restores original UUID or returns error on invalid length
	binData, err := id.MarshalBinary()
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: MarshalBinary returned error: %v\n", err)
		os.Exit(1)
	}
	if len(binData) != 16 || !bytes.Equal(binData, sampleBytes) {
		fmt.Fprintf(os.Stderr, "FAIL: MarshalBinary payload mismatch: got %x, want %x\n", binData, sampleBytes)
		os.Exit(1)
	}

	var restoredBin uuid.UUID
	if err := restoredBin.UnmarshalBinary(binData); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: UnmarshalBinary failed on valid 16-byte slice: %v\n", err)
		os.Exit(1)
	}
	if restoredBin != id {
		fmt.Fprintf(os.Stderr, "FAIL: UnmarshalBinary restored value mismatch: got %s, want %s\n", restoredBin.String(), id.String())
		os.Exit(1)
	}

	var dummyBin uuid.UUID
	if err := dummyBin.UnmarshalBinary([]byte{1, 2, 3}); err == nil {
		fmt.Fprintf(os.Stderr, "FAIL: UnmarshalBinary expected error for invalid length, got nil\n")
		os.Exit(1)
	}

	// 5. assert MarshalText produces canonical 36-byte ASCII representation and UnmarshalText parses text into UUID
	textData, err := id.MarshalText()
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: MarshalText returned error: %v\n", err)
		os.Exit(1)
	}
	if string(textData) != expectedString {
		fmt.Fprintf(os.Stderr, "FAIL: MarshalText payload mismatch: got %s, want %s\n", string(textData), expectedString)
		os.Exit(1)
	}

	var restoredText uuid.UUID
	if err := restoredText.UnmarshalText(textData); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: UnmarshalText failed on valid text: %v\n", err)
		os.Exit(1)
	}
	if restoredText != id {
		fmt.Fprintf(os.Stderr, "FAIL: UnmarshalText restored value mismatch: got %s, want %s\n", restoredText.String(), id.String())
		os.Exit(1)
	}

	var dummyText uuid.UUID
	if err := dummyText.UnmarshalText([]byte("invalid-uuid-string")); err == nil {
		fmt.Fprintf(os.Stderr, "FAIL: UnmarshalText expected error on invalid text, got nil\n")
		os.Exit(1)
	}

	// Verify standard library interface implementations
	var _ encoding.BinaryMarshaler = id
	var _ encoding.BinaryUnmarshaler = &id
	var _ encoding.TextMarshaler = id
	var _ encoding.TextUnmarshaler = &id

	fmt.Println("PASS: all contract assertions satisfied")
}

Seeder d'origine

anonyme