CodeSampleX

示例

github.com/mitchellh/reflectwalk v1.0.2: SkipEntry

已验证示例 — golang github.com/mitchellh/reflectwalk v1.0.2: SkipEntry. contract 在 go 1.26 · linux alpine/x64 · docker 上运行并通过: reflectwalk.SkipEntry returned from…

sha256:305c7c25133ab70ccb27a1e83c8d9e9c91f2cb7644d8046f8249e22ee122a2bf

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

执行证据

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

证据依据
签名契约通过
验证回执
2
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 go

验证运行环境

环境 契约 阶段 运行日期
go 1.26 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1
2026-08-21
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

案例

HOW
目标
verify github.com/mitchellh/reflectwalk.SkipEntry in pkg:golang/github.com/mitchellh/reflectwalk@v1.0.2
包
符号
  • github.com/mitchellh/reflectwalk.SkipEntry
创建时间
2026-08-21T00:38:41Z

契约

  1. reflectwalk.SkipEntry returned from StructField skips walking the specified struct field
  2. reflectwalk.SkipEntry returned from Struct skips walking all fields within that struct
  3. reflectwalk.SkipEntry returned from Pointer skips dereferencing and walking into the pointer target
  4. reflectwalk.SkipEntry is a distinct sentinel error that does not abort the overall Walk operation

文件

  • 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 github.com/mitchellh/reflectwalk.SkipEntry in pkg:golang/github.com/mitchellh/reflectwalk@v1.0.2
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/mitchellh/reflectwalk@v1.0.2
Demonstrate these symbols/APIs:
  - github.com/mitchellh/reflectwalk.SkipEntry

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:7509dc85c01b5e98aadbeab293bab56c870392ff3311ec6dcaf8780db2458618","contract":["reflectwalk.SkipEntry returned from StructField skips walking the specified struct field","reflectwalk.SkipEntry returned from Struct skips walking all fields within that struct","reflectwalk.SkipEntry returned from Pointer skips dereferencing and walking into the pointer target","reflectwalk.SkipEntry is a distinct sentinel error that does not abort the overall Walk operation"],"goal":"verify github.com/mitchellh/reflectwalk.SkipEntry in pkg:golang/github.com/mitchellh/reflectwalk@v1.0.2","kind":"HOW","packages":["pkg:golang/github.com/mitchellh/reflectwalk@v1.0.2"],"schemaVersion":1,"symbols":["github.com/mitchellh/reflectwalk.SkipEntry"]},"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/mitchellh/reflectwalk@v1.0.2"],"schemaVersion":1,"subject":"pkg:golang/github.com/mitchellh/reflectwalk@v1.0.2","symbols":["github.com/mitchellh/reflectwalk.SkipEntry"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require github.com/mitchellh/reflectwalk v1.0.2
go.sum
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
main.go
package main

import (
	"fmt"
	"reflect"
	"strings"

	"github.com/mitchellh/reflectwalk"
)

type filterWalker struct {
	visited []string
}

func (w *filterWalker) Struct(v reflect.Value) error {
	return nil
}

func (w *filterWalker) StructField(sf reflect.StructField, v reflect.Value) error {
	if strings.HasPrefix(sf.Name, "Hidden") {
		return reflectwalk.SkipEntry
	}
	w.visited = append(w.visited, sf.Name)
	return nil
}

type Config struct {
	PublicField string
	HiddenField string
	ExtraField  int
}

func main() {
	cfg := Config{
		PublicField: "visible",
		HiddenField: "secret",
		ExtraField:  42,
	}

	walker := &filterWalker{}
	if err := reflectwalk.Walk(cfg, walker); err != nil {
		panic(err)
	}

	fmt.Printf("Visited fields: %v\n", walker.visited)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/mitchellh/reflectwalk.SkipEntry in pkg:golang/github.com/mitchellh/reflectwalk@v1.0.2",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/mitchellh/reflectwalk@v1.0.2"
  ],
  "symbols": [
    "github.com/mitchellh/reflectwalk.SkipEntry"
  ]
}
test/contract.go
package main

import (
	"errors"
	"fmt"
	"os"
	"reflect"
	"strings"

	"github.com/mitchellh/reflectwalk"
)

func main() {
	if err := runContract(); err != nil {
		fmt.Fprintf(os.Stderr, "Contract verification failed: %v\n", err)
		os.Exit(1)
	}
	fmt.Println("All reflectwalk.SkipEntry contract assertions passed successfully.")
}

type fieldFilterWalker struct {
	visitedFields []string
}

func (w *fieldFilterWalker) Struct(v reflect.Value) error {
	return nil
}

func (w *fieldFilterWalker) StructField(sf reflect.StructField, v reflect.Value) error {
	if strings.HasPrefix(sf.Name, "Skip") {
		return reflectwalk.SkipEntry
	}
	w.visitedFields = append(w.visitedFields, sf.Name)
	return nil
}

type structFilterWalker struct {
	structCount int
	fieldCount  int
	skipStruct  bool
}

func (w *structFilterWalker) Struct(v reflect.Value) error {
	w.structCount++
	if w.skipStruct {
		return reflectwalk.SkipEntry
	}
	return nil
}

func (w *structFilterWalker) StructField(sf reflect.StructField, v reflect.Value) error {
	w.fieldCount++
	return nil
}

type pointerFilterWalker struct {
	pointerVisited []string
	primitiveCount int
	skipPointer    bool
}

func (w *pointerFilterWalker) Pointer(v reflect.Value) error {
	w.pointerVisited = append(w.pointerVisited, v.Type().String())
	if w.skipPointer {
		return reflectwalk.SkipEntry
	}
	return nil
}

func (w *pointerFilterWalker) Primitive(v reflect.Value) error {
	w.primitiveCount++
	return nil
}

func runContract() error {
	// Assertion 1: reflectwalk.SkipEntry returned from StructField skips walking the specified struct field
	type SampleData struct {
		KeepFirst  string
		SkipMiddle int
		KeepLast   bool
	}
	data1 := SampleData{
		KeepFirst:  "alpha",
		SkipMiddle: 100,
		KeepLast:   true,
	}
	w1 := &fieldFilterWalker{}
	if err := reflectwalk.Walk(data1, w1); err != nil {
		return fmt.Errorf("unexpected error walking data1: %w", err)
	}
	if len(w1.visitedFields) != 2 || w1.visitedFields[0] != "KeepFirst" || w1.visitedFields[1] != "KeepLast" {
		return fmt.Errorf("expected [KeepFirst KeepLast], got %v", w1.visitedFields)
	}

	// Assertion 2: reflectwalk.SkipEntry returned from Struct skips walking all fields within that struct
	data2 := SampleData{
		KeepFirst:  "beta",
		SkipMiddle: 200,
		KeepLast:   false,
	}
	w2 := &structFilterWalker{skipStruct: true}
	if err := reflectwalk.Walk(data2, w2); err != nil {
		return fmt.Errorf("unexpected error walking data2 with skipStruct: %w", err)
	}
	if w2.structCount != 1 || w2.fieldCount != 0 {
		return fmt.Errorf("expected structCount 1 and fieldCount 0, got structCount=%d, fieldCount=%d", w2.structCount, w2.fieldCount)
	}

	// Assertion 3: reflectwalk.SkipEntry returned from Pointer skips dereferencing and walking into the pointer target
	val := 42
	ptrData := &val
	w3 := &pointerFilterWalker{skipPointer: true}
	if err := reflectwalk.Walk(ptrData, w3); err != nil {
		return fmt.Errorf("unexpected error walking pointer: %w", err)
	}
	if len(w3.pointerVisited) != 1 || w3.primitiveCount != 0 {
		return fmt.Errorf("expected pointer visited once and 0 primitives walked, got pointerVisited=%v, primitiveCount=%d", w3.pointerVisited, w3.primitiveCount)
	}

	// Assertion 4: reflectwalk.SkipEntry is a distinct sentinel error that does not abort the overall Walk operation
	if reflectwalk.SkipEntry == nil {
		return errors.New("reflectwalk.SkipEntry must not be nil")
	}
	if reflectwalk.SkipEntry.Error() != "skip this entry" {
		return fmt.Errorf("expected SkipEntry error message %q, got %q", "skip this entry", reflectwalk.SkipEntry.Error())
	}

	return nil
}

原始种子者

匿名