CodeSampleX

Exemple

github.com/google/uuid v1.6.0: text serialization with github.com/google/uuid MarshalText and UnmarshalText

Échantillon vérifié pour golang github.com/google/uuid v1.6.0: text serialization with github.com/google/uuid MarshalText and UnmarshalText. Le contrat s'est…

sha256:00a1e6998c589f038cd7634013b7c187f0d4afcdb9af4d4194ad18a26b672642

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é
1
Environnement déclaré windows 11 x64 go

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 alpine/x64 · docker ed25519:c1973797be207ac4 SKIPPED compile:SKIPPED · contract:SKIPPED · load:SKIPPED · resolve:FAIL
CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f…
2026-09-07

Cas

HOW
Objectif
text serialization with github.com/google/uuid MarshalText and UnmarshalText
Paquets
Symboles
  • MarshalText
  • UnmarshalText
Créé
2026-08-19T04:25:58Z

Contrat

  1. UUID.MarshalText encodes UUID into standard 36-byte UTF-8 string representation without error
  2. UUID.UnmarshalText decodes standard 36-byte UUID text into matching UUID value
  3. UUID.UnmarshalText decodes 32-character hex and URN formatted byte slices
  4. UUID.UnmarshalText returns error for invalid format or invalid length byte slices
  5. UUID.MarshalText and UnmarshalText support uuid.Nil and round trip lossless conversion

Fichiers

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • sample.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: text serialization with github.com/google/uuid MarshalText and UnmarshalText
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/google/uuid@v1.6.0
Demonstrate these symbols/APIs:
  - MarshalText
  - UnmarshalText
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@11.8.0
  - runtime: node@24.13.1

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:1dd0a6293a3784295d77eab9a0ceb30637d8d33ffd4bcf30821b40d6fe1cbed4","contract":["UUID.MarshalText encodes UUID into standard 36-byte UTF-8 string representation without error","UUID.UnmarshalText decodes standard 36-byte UUID text into matching UUID value","UUID.UnmarshalText decodes 32-character hex and URN formatted byte slices","UUID.UnmarshalText returns error for invalid format or invalid length byte slices","UUID.MarshalText and UnmarshalText support uuid.Nil and round trip lossless conversion"],"goal":"text serialization with github.com/google/uuid MarshalText and UnmarshalText","kind":"HOW","packages":["pkg:golang/github.com/google/uuid@v1.6.0"],"schemaVersion":1,"symbols":["MarshalText","UnmarshalText"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","ecosystem":"golang","os":"windows","osVersionBucket":"11","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/google/uuid@v1.6.0"],"schemaVersion":1,"symbols":["MarshalText","UnmarshalText"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.22.0

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=
sample.go
package sample

import (
	"github.com/google/uuid"
)

// MarshalUUID serializes a UUID into its standard text representation.
func MarshalUUID(u uuid.UUID) ([]byte, error) {
	return u.MarshalText()
}

// UnmarshalUUID deserializes text data into a UUID.
func UnmarshalUUID(data []byte) (uuid.UUID, error) {
	var u uuid.UUID
	err := u.UnmarshalText(data)
	if err != nil {
		return uuid.Nil, err
	}
	return u, nil
}
spec.json
{
  "schemaVersion": 1,
  "goal": "text serialization with github.com/google/uuid MarshalText and UnmarshalText",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/google/uuid@v1.6.0"
  ],
  "symbols": [
    "MarshalText",
    "UnmarshalText"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@11.8.0",
    "runtime": "node@24.13.1"
  }
}
test/contract.go
package main

import (
	"fmt"
	"os"

	"github.com/google/uuid"
)

func main() {
	// 1. UUID.MarshalText encodes UUID into standard 36-byte UTF-8 string representation without error
	original := uuid.MustParse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
	textBytes, err := original.MarshalText()
	if err != nil {
		fmt.Fprintf(os.Stderr, "unexpected error in MarshalText: %v\n", err)
		os.Exit(1)
	}
	expectedStr := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
	if string(textBytes) != expectedStr {
		fmt.Fprintf(os.Stderr, "expected text %q, got %q\n", expectedStr, string(textBytes))
		os.Exit(1)
	}
	if len(textBytes) != 36 {
		fmt.Fprintf(os.Stderr, "expected length 36, got %d\n", len(textBytes))
		os.Exit(1)
	}

	// 2. UUID.UnmarshalText decodes standard 36-byte UUID text into matching UUID value
	var unmarshaled uuid.UUID
	if err := unmarshaled.UnmarshalText(textBytes); err != nil {
		fmt.Fprintf(os.Stderr, "unexpected error in UnmarshalText: %v\n", err)
		os.Exit(1)
	}
	if unmarshaled != original {
		fmt.Fprintf(os.Stderr, "expected unmarshaled UUID %v, got %v\n", original, unmarshaled)
		os.Exit(1)
	}

	// 3. UUID.UnmarshalText decodes 32-character hex and URN formatted byte slices
	hexBytes := []byte("6ba7b8109dad11d180b400c04fd430c8")
	var hexUUID uuid.UUID
	if err := hexUUID.UnmarshalText(hexBytes); err != nil {
		fmt.Fprintf(os.Stderr, "unexpected error in UnmarshalText with hex: %v\n", err)
		os.Exit(1)
	}
	if hexUUID != original {
		fmt.Fprintf(os.Stderr, "hex decode mismatch: got %v, want %v\n", hexUUID, original)
		os.Exit(1)
	}

	urnBytes := []byte("urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8")
	var urnUUID uuid.UUID
	if err := urnUUID.UnmarshalText(urnBytes); err != nil {
		fmt.Fprintf(os.Stderr, "unexpected error in UnmarshalText with URN: %v\n", err)
		os.Exit(1)
	}
	if urnUUID != original {
		fmt.Fprintf(os.Stderr, "URN decode mismatch: got %v, want %v\n", urnUUID, original)
		os.Exit(1)
	}

	// 4. UUID.UnmarshalText returns error for invalid format or invalid length byte slices
	invalidInputs := [][]byte{
		[]byte(""),
		[]byte("not-a-uuid"),
		[]byte("6ba7b810-9dad-11d1-80b4-00c04fd430cg"), // invalid hex 'g'
		[]byte("6ba7b810-9dad-11d1-80b4-00c04fd430c"),   // 35 chars
		[]byte("6ba7b810-9dad-11d1-80b4-00c04fd430c89"),  // 37 chars
	}
	for _, input := range invalidInputs {
		var u uuid.UUID
		if err := u.UnmarshalText(input); err == nil {
			fmt.Fprintf(os.Stderr, "expected error for invalid input %q, got nil\n", string(input))
			os.Exit(1)
		}
	}

	// 5. UUID.MarshalText and UnmarshalText support uuid.Nil and round trip lossless conversion
	nilBytes, err := uuid.Nil.MarshalText()
	if err != nil {
		fmt.Fprintf(os.Stderr, "unexpected error in uuid.Nil.MarshalText: %v\n", err)
		os.Exit(1)
	}
	if string(nilBytes) != "00000000-0000-0000-0000-000000000000" {
		fmt.Fprintf(os.Stderr, "unexpected nil text: %q\n", string(nilBytes))
		os.Exit(1)
	}
	var unmarshaledNil uuid.UUID
	if err := unmarshaledNil.UnmarshalText(nilBytes); err != nil {
		fmt.Fprintf(os.Stderr, "unexpected error unmarshaling nil UUID text: %v\n", err)
		os.Exit(1)
	}
	if unmarshaledNil != uuid.Nil {
		fmt.Fprintf(os.Stderr, "expected uuid.Nil, got %v\n", unmarshaledNil)
		os.Exit(1)
	}

	// Random UUID round trip
	randomUUID := uuid.New()
	randomText, err := randomUUID.MarshalText()
	if err != nil {
		fmt.Fprintf(os.Stderr, "unexpected error marshaling random UUID: %v\n", err)
		os.Exit(1)
	}
	var roundTripUUID uuid.UUID
	if err := roundTripUUID.UnmarshalText(randomText); err != nil {
		fmt.Fprintf(os.Stderr, "unexpected error unmarshaling random UUID: %v\n", err)
		os.Exit(1)
	}
	if roundTripUUID != randomUUID {
		fmt.Fprintf(os.Stderr, "round trip mismatch: got %v, want %v\n", roundTripUUID, randomUUID)
		os.Exit(1)
	}

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

Seeder d'origine

anonyme