CodeSampleX

示例

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

已验证示例 — golang github.com/google/uuid v1.6.0: text serialization with github.com/google/uuid MarshalText and UnmarshalText. contract 在 go 1.26 · linux…

sha256:00a1e6998c589f038cd7634013b7c187f0d4afcdb9af4d4194ad18a26b672642

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

执行证据

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

证据依据
签名契约通过
验证回执
2
构建过它的签名密钥
1
声明的环境 windows 11 x64 go

验证运行环境

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

案例

HOW
目标
text serialization with github.com/google/uuid MarshalText and UnmarshalText
包
符号
  • MarshalText
  • UnmarshalText
创建时间
2026-08-19T04:25:58Z

契约

  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

文件

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

原始种子者

匿名