CodeSampleX

示例

github.com/google/uuid v1.3.0

已验证示例 — golang github.com/google/uuid v1.3.0. contract 在 go 1.26 · linux alpine/x64 · docker 上运行并通过: uuid.NewUUID generates RFC 4122 version 1 UUID with…

sha256:022998b8a6636b54f335c89661d63455a014845dc81677ff5cd4665ef762af7f

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

执行证据

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

证据依据
签名契约通过
验证回执
2
构建过它的签名密钥
2
声明的环境 linux 24 · ubuntu · glibc 2.39 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-20
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-08

案例

HOW
目标
verify pkg:golang/github.com/google/uuid@v1.3.0
包
创建时间
2026-08-20T00:29:26Z

契约

  1. uuid.NewUUID generates RFC 4122 version 1 UUID with valid timestamp and node components
  2. uuid.FromBytes validates 16-byte slice length and converts to UUID without errors
  3. uuid.ParseBytes parses byte representations and validates format
  4. uuid.MustParse returns parsed UUID on valid input and panics on invalid input
  5. uuid.NullUUID handles database nullability with sql.Scanner and driver.Valuer implementations

文件

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.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: verify pkg:golang/github.com/google/uuid@v1.3.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/google/uuid@v1.3.0
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

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:a0450847a3f2de7e5649f04061b9d5f5c448b856e880b20f171609b80c9f8e9f","contract":["uuid.NewUUID generates RFC 4122 version 1 UUID with valid timestamp and node components","uuid.FromBytes validates 16-byte slice length and converts to UUID without errors","uuid.ParseBytes parses byte representations and validates format","uuid.MustParse returns parsed UUID on valid input and panics on invalid input","uuid.NullUUID handles database nullability with sql.Scanner and driver.Valuer implementations"],"goal":"verify pkg:golang/github.com/google/uuid@v1.3.0","kind":"HOW","packages":["pkg:golang/github.com/google/uuid@v1.3.0"],"schemaVersion":1},"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/google/uuid@v1.3.0"],"schemaVersion":1,"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require github.com/google/uuid v1.3.0
go.sum
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
main.go
package main

import (
	"bytes"
	"database/sql/driver"
	"fmt"
	"os"

	"github.com/google/uuid"
)

// Record represents a database entity with unique identifiers and optional parent reference.
type Record struct {
	ID       uuid.UUID
	ParentID uuid.NullUUID
}

// NewRecord creates a new Record with a generated Version 1 time-based UUID.
func NewRecord(parentID *uuid.UUID) (*Record, error) {
	id, err := uuid.NewUUID()
	if err != nil {
		return nil, fmt.Errorf("failed to generate v1 uuid: %w", err)
	}

	rec := &Record{
		ID: id,
	}

	if parentID != nil {
		rec.ParentID = uuid.NullUUID{
			UUID:  *parentID,
			Valid: true,
		}
	} else {
		rec.ParentID = uuid.NullUUID{
			Valid: false,
		}
	}

	return rec, nil
}

// ParseIdentifier safely converts raw binary or ASCII byte slice into a UUID.
func ParseIdentifier(raw []byte) (uuid.UUID, error) {
	if len(raw) == 16 {
		return uuid.FromBytes(raw)
	}
	return uuid.ParseBytes(raw)
}

// FormatDatabaseValue serializes nullable UUID for database driver compatibility.
func FormatDatabaseValue(nullUUID uuid.NullUUID) (driver.Value, error) {
	return nullUUID.Value()
}

func main() {
	// 1. Create a root record with Version 1 UUID
	root, err := NewRecord(nil)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error creating root record: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("Root Record ID (v%d): %s, URN: %s\n", root.ID.Version(), root.ID.String(), root.ID.URN())

	// 2. Create a child record referencing root
	child, err := NewRecord(&root.ID)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error creating child record: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("Child Record ID: %s, Parent ID: %s (valid: %v)\n",
		child.ID.String(), child.ParentID.UUID.String(), child.ParentID.Valid)

	// 3. Round-trip binary representation
	bin, err := root.ID.MarshalBinary()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Binary marshal error: %v\n", err)
		os.Exit(1)
	}

	restored, err := ParseIdentifier(bin)
	if err != nil || !bytes.Equal(root.ID[:], restored[:]) {
		fmt.Fprintf(os.Stderr, "Binary restore error: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("Restored ID from 16-byte binary: %s\n", restored.String())

	// 4. MustParse demonstration for well-known constants
	const namespaceStr = "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
	parsedConst := uuid.MustParse(namespaceStr)
	fmt.Printf("Constant parsed via MustParse: %s\n", parsedConst.String())
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/github.com/google/uuid@v1.3.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/google/uuid@v1.3.0"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
test/contract.go
package main

import (
	"bytes"
	"fmt"
	"os"

	"github.com/google/uuid"
)

func main() {
	// Assertion 1: uuid.NewUUID generates RFC 4122 version 1 UUID with valid timestamp and node components
	{
		u, err := uuid.NewUUID()
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: uuid.NewUUID returned error: %v\n", err)
			os.Exit(1)
		}

		if u.Version() != 1 {
			fmt.Fprintf(os.Stderr, "FAIL: expected version 1, got %v\n", u.Version())
			os.Exit(1)
		}

		if u.Variant() != uuid.RFC4122 {
			fmt.Fprintf(os.Stderr, "FAIL: expected RFC4122 variant, got %v\n", u.Variant())
			os.Exit(1)
		}

		node := u.NodeID()
		if len(node) != 6 {
			fmt.Fprintf(os.Stderr, "FAIL: expected 6-byte node ID, got %d bytes\n", len(node))
			os.Exit(1)
		}

		t := u.Time()
		if t == 0 {
			fmt.Fprintf(os.Stderr, "FAIL: expected non-zero timestamp from v1 UUID\n")
			os.Exit(1)
		}
	}

	// Assertion 2: uuid.FromBytes validates 16-byte slice length and converts to UUID without errors
	{
		validBytes := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10}
		u, err := uuid.FromBytes(validBytes)
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: uuid.FromBytes failed on valid 16-byte slice: %v\n", err)
			os.Exit(1)
		}

		if !bytes.Equal(u[:], validBytes) {
			fmt.Fprintf(os.Stderr, "FAIL: uuid.FromBytes produced bytes %x, expected %x\n", u[:], validBytes)
			os.Exit(1)
		}

		shortBytes := []byte{0x01, 0x02, 0x03}
		if _, err := uuid.FromBytes(shortBytes); err == nil {
			fmt.Fprintf(os.Stderr, "FAIL: uuid.FromBytes expected error on short slice, got nil\n")
			os.Exit(1)
		}

		longBytes := make([]byte, 17)
		if _, err := uuid.FromBytes(longBytes); err == nil {
			fmt.Fprintf(os.Stderr, "FAIL: uuid.FromBytes expected error on 17-byte slice, got nil\n")
			os.Exit(1)
		}
	}

	// Assertion 3: uuid.ParseBytes parses byte representations and validates format
	{
		inputStr := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
		u, err := uuid.ParseBytes([]byte(inputStr))
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: uuid.ParseBytes failed on valid UUID bytes: %v\n", err)
			os.Exit(1)
		}

		if u.String() != inputStr {
			fmt.Fprintf(os.Stderr, "FAIL: uuid.ParseBytes result %s != expected %s\n", u.String(), inputStr)
			os.Exit(1)
		}

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

	// Assertion 4: uuid.MustParse returns parsed UUID on valid input and panics on invalid input
	{
		validStr := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
		u := uuid.MustParse(validStr)
		if u.String() != validStr {
			fmt.Fprintf(os.Stderr, "FAIL: uuid.MustParse returned %s, expected %s\n", u.String(), validStr)
			os.Exit(1)
		}

		panicked := false
		func() {
			defer func() {
				if r := recover(); r != nil {
					panicked = true
				}
			}()
			_ = uuid.MustParse("invalid-uuid-string")
		}()

		if !panicked {
			fmt.Fprintf(os.Stderr, "FAIL: uuid.MustParse did not panic on invalid input\n")
			os.Exit(1)
		}
	}

	// Assertion 5: uuid.NullUUID handles database nullability with sql.Scanner and driver.Valuer implementations
	{
		var nullID uuid.NullUUID
		val, err := nullID.Value()
		if err != nil || val != nil {
			fmt.Fprintf(os.Stderr, "FAIL: NullUUID.Value() on invalid struct expected (nil, nil), got (%v, %v)\n", val, err)
			os.Exit(1)
		}

		if err := nullID.Scan(nil); err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: NullUUID.Scan(nil) error: %v\n", err)
			os.Exit(1)
		}
		if nullID.Valid {
			fmt.Fprintf(os.Stderr, "FAIL: NullUUID.Scan(nil) should result in Valid=false\n")
			os.Exit(1)
		}

		targetStr := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
		if err := nullID.Scan(targetStr); err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: NullUUID.Scan(string) error: %v\n", err)
			os.Exit(1)
		}
		if !nullID.Valid || nullID.UUID.String() != targetStr {
			fmt.Fprintf(os.Stderr, "FAIL: NullUUID.Scan(string) failed: Valid=%v, UUID=%s\n", nullID.Valid, nullID.UUID.String())
			os.Exit(1)
		}

		val, err = nullID.Value()
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: NullUUID.Value() error: %v\n", err)
			os.Exit(1)
		}
		strVal, ok := val.(string)
		if !ok || strVal != targetStr {
			fmt.Fprintf(os.Stderr, "FAIL: NullUUID.Value() = %v (type %T), expected %q\n", val, val, targetStr)
			os.Exit(1)
		}
	}

	fmt.Println("PASS: github.com/google/uuid contract passed")
}

原始种子者

匿名