CodeSampleX

Exemplo

github.com/google/uuid v1.6.0: binary serialization with github.com/google/uuid MarshalBinary and UnmarshalBinary

Amostra verificada para golang github.com/google/uuid v1.6.0: binary serialization with github.com/google/uuid MarshalBinary and UnmarshalBinary. O contrato…

sha256:8525be64586c339bd0d5b7d875cc90b3509b1e13e99976deb399027730db87ce

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
2
Chaves de assinatura que o compilaram
2
Ambiente declarado windows 11 x64 go

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
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 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f…
2026-09-07

Caso

HOW
Objetivo
binary serialization with github.com/google/uuid MarshalBinary and UnmarshalBinary
Pacotes
Símbolos
  • github.com/google/uuid.UUID.MarshalBinary
Criado
2026-08-19T04:21:00Z

Contrato

  1. UUID.MarshalBinary encodes UUID into a 16-byte binary representation without error
  2. UUID.UnmarshalBinary reconstructs exact original UUID from valid 16-byte slice
  3. UUID.UnmarshalBinary returns error for byte slices of invalid length
  4. UUID.MarshalBinary and UnmarshalBinary support uuid.Nil and arbitrary UUIDs in lossless round trip

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: binary serialization with github.com/google/uuid MarshalBinary and UnmarshalBinary
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/google/uuid@v1.6.0
Demonstrate these symbols/APIs:
  - github.com/google/uuid.UUID.MarshalBinary
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:e1d764c3879c227351c0c439d0824016c32ef3c68db4782796e11ab2868c014e","contract":["UUID.MarshalBinary encodes UUID into a 16-byte binary representation without error","UUID.UnmarshalBinary reconstructs exact original UUID from valid 16-byte slice","UUID.UnmarshalBinary returns error for byte slices of invalid length","UUID.MarshalBinary and UnmarshalBinary support uuid.Nil and arbitrary UUIDs in lossless round trip"],"goal":"binary serialization with github.com/google/uuid MarshalBinary and UnmarshalBinary","kind":"HOW","packages":["pkg:golang/github.com/google/uuid@v1.6.0"],"schemaVersion":1,"symbols":["github.com/google/uuid.UUID.MarshalBinary"]},"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":["github.com/google/uuid.UUID.MarshalBinary"],"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"
)

// EncodeUUID serializes a UUID into its 16-byte binary representation.
func EncodeUUID(u uuid.UUID) ([]byte, error) {
	return u.MarshalBinary()
}

// DecodeUUID deserializes a 16-byte binary slice into a UUID.
func DecodeUUID(data []byte) (uuid.UUID, error) {
	var u uuid.UUID
	err := u.UnmarshalBinary(data)
	if err != nil {
		return uuid.Nil, err
	}
	return u, nil
}
spec.json
{
  "schemaVersion": 1,
  "goal": "binary serialization with github.com/google/uuid MarshalBinary and UnmarshalBinary",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/google/uuid@v1.6.0"
  ],
  "symbols": [
    "github.com/google/uuid.UUID.MarshalBinary"
  ],
  "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 (
	"bytes"
	"fmt"
	"os"

	"github.com/google/uuid"
)

func main() {
	// 1. MarshalBinary encodes a UUID into a 16-byte slice without error
	original := uuid.MustParse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
	data, err := original.MarshalBinary()
	if err != nil {
		fmt.Fprintf(os.Stderr, "unexpected error in MarshalBinary: %v\n", err)
		os.Exit(1)
	}
	if len(data) != 16 {
		fmt.Fprintf(os.Stderr, "expected binary length 16, got %d\n", len(data))
		os.Exit(1)
	}

	// Verify byte content matches raw UUID bytes
	if !bytes.Equal(data, original[:]) {
		fmt.Fprintf(os.Stderr, "MarshalBinary output does not match raw bytes\n")
		os.Exit(1)
	}

	// 2. UnmarshalBinary reconstructs the exact UUID from valid 16-byte slice
	var decoded uuid.UUID
	if err := decoded.UnmarshalBinary(data); err != nil {
		fmt.Fprintf(os.Stderr, "unexpected error in UnmarshalBinary: %v\n", err)
		os.Exit(1)
	}
	if decoded != original {
		fmt.Fprintf(os.Stderr, "expected decoded UUID %v, got %v\n", original, decoded)
		os.Exit(1)
	}

	// 3. UnmarshalBinary returns error for byte slices not exactly 16 bytes
	invalidSlices := [][]byte{
		{},
		make([]byte, 15),
		make([]byte, 17),
		make([]byte, 32),
	}
	for _, invalid := range invalidSlices {
		var u uuid.UUID
		if err := u.UnmarshalBinary(invalid); err == nil {
			fmt.Fprintf(os.Stderr, "expected error for invalid byte slice length %d, got nil\n", len(invalid))
			os.Exit(1)
		}
	}

	// 4. MarshalBinary and UnmarshalBinary work correctly for uuid.Nil
	nilData, err := uuid.Nil.MarshalBinary()
	if err != nil {
		fmt.Fprintf(os.Stderr, "unexpected error in uuid.Nil.MarshalBinary: %v\n", err)
		os.Exit(1)
	}
	if !bytes.Equal(nilData, make([]byte, 16)) {
		fmt.Fprintf(os.Stderr, "expected 16 zero bytes for nil UUID, got %v\n", nilData)
		os.Exit(1)
	}

	var unmarshaledNil uuid.UUID
	if err := unmarshaledNil.UnmarshalBinary(nilData); err != nil {
		fmt.Fprintf(os.Stderr, "unexpected error unmarshaling nil UUID: %v\n", err)
		os.Exit(1)
	}
	if unmarshaledNil != uuid.Nil {
		fmt.Fprintf(os.Stderr, "expected uuid.Nil, got %v\n", unmarshaledNil)
		os.Exit(1)
	}

	// 5. Random UUID round-trip test
	randomUUID := uuid.New()
	randomBytes, err := randomUUID.MarshalBinary()
	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.UnmarshalBinary(randomBytes); 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 de origem

anônimo