CodeSampleX

Exemple

github.com/google/go-cmp v0.5.7: cmpopts.IgnoreFields

Échantillon vérifié pour golang github.com/google/go-cmp v0.5.7: cmpopts.IgnoreFields. Le contrat s'est exécuté sur go 1.26 · linux debian/x64 · docker et a…

sha256:8f2cbd97ad9da7073017c8328620f39674fbaf5a531de61bd9b1ad5d1e5977f8

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
1
Clés de signature qui l’ont compilé
1
Environnement déclaré linux 24 · ubuntu · glibc 2.39 x64 go

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
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-11-05

Cas

HOW
Objectif
verify github.com/google/go-cmp/cmp/cmpopts.IgnoreFields in pkg:golang/github.com/google/go-cmp@v0.5.7
Paquets
Symboles
  • github.com/google/go-cmp/cmp/cmpopts.IgnoreFields
Créé
2026-11-05T21:43:28Z

Contrat

  1. cmpopts.IgnoreFields ignores a single specified struct field during comparison
  2. cmpopts.IgnoreFields ignores multiple specified struct fields during comparison
  3. cmpopts.IgnoreFields detects differences when non-ignored fields differ
  4. cmpopts.IgnoreFields applies field ignoring when comparing slices of structs
  5. cmpopts.IgnoreFields ignores fields in nested structs when target struct type is specified

Fichiers

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.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: verify github.com/google/go-cmp/cmp/cmpopts.IgnoreFields in pkg:golang/github.com/google/go-cmp@v0.5.7
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/google/go-cmp@v0.5.7
Demonstrate these symbols/APIs:
  - github.com/google/go-cmp/cmp/cmpopts.IgnoreFields

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:0f229431e23bcbdcbb1fe533f59294c73c9505f8c740eaeaf0f16720dabb97fc","contract":["cmpopts.IgnoreFields ignores a single specified struct field during comparison","cmpopts.IgnoreFields ignores multiple specified struct fields during comparison","cmpopts.IgnoreFields detects differences when non-ignored fields differ","cmpopts.IgnoreFields applies field ignoring when comparing slices of structs","cmpopts.IgnoreFields ignores fields in nested structs when target struct type is specified"],"goal":"verify github.com/google/go-cmp/cmp/cmpopts.IgnoreFields in pkg:golang/github.com/google/go-cmp@v0.5.7","kind":"HOW","packages":["pkg:golang/github.com/google/go-cmp@v0.5.7"],"schemaVersion":1,"symbols":["github.com/google/go-cmp/cmp/cmpopts.IgnoreFields"]},"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/go-cmp@v0.5.7"],"schemaVersion":1,"subject":"pkg:golang/github.com/google/go-cmp@v0.5.7","symbols":["github.com/google/go-cmp/cmp/cmpopts.IgnoreFields"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.22.0

require github.com/google/go-cmp v0.5.7

require golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
go.sum
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
main.go
package main

import (
	"fmt"

	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
)

// AuditRecord represents a structured record with metadata fields.
type AuditRecord struct {
	ID        int
	Action    string
	Actor     string
	Timestamp string
	Metadata  RecordMetadata
}

// RecordMetadata holds nested details for an audit record.
type RecordMetadata struct {
	TraceID string
	Detail  string
}

// CompareRecords compares two AuditRecord values while ignoring the specified fields.
func CompareRecords(a, b AuditRecord, ignored ...string) bool {
	return cmp.Equal(a, b, cmpopts.IgnoreFields(AuditRecord{}, ignored...))
}

func main() {
	r1 := AuditRecord{
		ID:        1,
		Action:    "update",
		Actor:     "operator",
		Timestamp: "2026-01-01T00:00:00Z",
	}
	r2 := AuditRecord{
		ID:        1,
		Action:    "update",
		Actor:     "operator",
		Timestamp: "2026-01-01T01:00:00Z",
	}

	fmt.Printf("Default equal: %v\n", cmp.Equal(r1, r2))
	fmt.Printf("Ignoring Timestamp equal: %v\n", CompareRecords(r1, r2, "Timestamp"))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/google/go-cmp/cmp/cmpopts.IgnoreFields in pkg:golang/github.com/google/go-cmp@v0.5.7",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/google/go-cmp@v0.5.7"
  ],
  "symbols": [
    "github.com/google/go-cmp/cmp/cmpopts.IgnoreFields"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"
	"strings"

	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
)

type auditEvent struct {
	ID        int
	Action    string
	Actor     string
	Timestamp string
	Payload   eventPayload
}

type eventPayload struct {
	CorrelationID string
	Message       string
}

func main() {
	// 1. cmpopts.IgnoreFields ignores a single specified struct field during comparison
	{
		a := auditEvent{
			ID:        1,
			Action:    "login",
			Actor:     "user-1",
			Timestamp: "2026-09-01T00:00:00Z",
		}
		b := auditEvent{
			ID:        1,
			Action:    "login",
			Actor:     "user-1",
			Timestamp: "2026-09-01T12:00:00Z",
		}

		if cmp.Equal(a, b) {
			fmt.Fprintln(os.Stderr, "FAIL: assertion 1 - expected inequality when Timestamp differs without IgnoreFields")
			os.Exit(1)
		}
		if !cmp.Equal(a, b, cmpopts.IgnoreFields(auditEvent{}, "Timestamp")) {
			fmt.Fprintln(os.Stderr, "FAIL: assertion 1 - expected equality when ignoring Timestamp")
			os.Exit(1)
		}
		if diff := cmp.Diff(a, b, cmpopts.IgnoreFields(auditEvent{}, "Timestamp")); diff != "" {
			fmt.Fprintf(os.Stderr, "FAIL: assertion 1 - expected empty diff when ignoring Timestamp, got:\n%s\n", diff)
			os.Exit(1)
		}
	}

	// 2. cmpopts.IgnoreFields ignores multiple specified struct fields during comparison
	{
		a := auditEvent{
			ID:        100,
			Action:    "read",
			Actor:     "reader-a",
			Timestamp: "2026-09-02T01:00:00Z",
		}
		b := auditEvent{
			ID:        200,
			Action:    "read",
			Actor:     "reader-a",
			Timestamp: "2026-09-02T02:00:00Z",
		}

		if !cmp.Equal(a, b, cmpopts.IgnoreFields(auditEvent{}, "ID", "Timestamp")) {
			fmt.Fprintln(os.Stderr, "FAIL: assertion 2 - expected equality when ignoring ID and Timestamp")
			os.Exit(1)
		}
		if diff := cmp.Diff(a, b, cmpopts.IgnoreFields(auditEvent{}, "ID", "Timestamp")); diff != "" {
			fmt.Fprintf(os.Stderr, "FAIL: assertion 2 - expected empty diff when ignoring ID and Timestamp, got:\n%s\n", diff)
			os.Exit(1)
		}
	}

	// 3. cmpopts.IgnoreFields detects differences when non-ignored fields differ
	{
		a := auditEvent{
			ID:        10,
			Action:    "create",
			Actor:     "admin",
			Timestamp: "2026-09-03T00:00:00Z",
		}
		b := auditEvent{
			ID:        10,
			Action:    "delete",
			Actor:     "admin",
			Timestamp: "2026-09-03T10:00:00Z",
		}

		if cmp.Equal(a, b, cmpopts.IgnoreFields(auditEvent{}, "Timestamp")) {
			fmt.Fprintln(os.Stderr, "FAIL: assertion 3 - expected inequality when non-ignored Action differs")
			os.Exit(1)
		}
		diff := cmp.Diff(a, b, cmpopts.IgnoreFields(auditEvent{}, "Timestamp"))
		if !strings.Contains(diff, "create") || !strings.Contains(diff, "delete") {
			fmt.Fprintf(os.Stderr, "FAIL: assertion 3 - expected diff to contain Action values, got:\n%s\n", diff)
			os.Exit(1)
		}
	}

	// 4. cmpopts.IgnoreFields applies field ignoring when comparing slices of structs
	{
		listA := []auditEvent{
			{ID: 1, Action: "sync", Timestamp: "10:00"},
			{ID: 2, Action: "flush", Timestamp: "10:01"},
		}
		listB := []auditEvent{
			{ID: 1, Action: "sync", Timestamp: "11:00"},
			{ID: 2, Action: "flush", Timestamp: "11:01"},
		}

		if cmp.Equal(listA, listB) {
			fmt.Fprintln(os.Stderr, "FAIL: assertion 4 - expected inequality for slices without IgnoreFields")
			os.Exit(1)
		}
		if !cmp.Equal(listA, listB, cmpopts.IgnoreFields(auditEvent{}, "Timestamp")) {
			fmt.Fprintln(os.Stderr, "FAIL: assertion 4 - expected equality for slices when ignoring Timestamp")
			os.Exit(1)
		}
	}

	// 5. cmpopts.IgnoreFields ignores fields in nested structs when target struct type is specified
	{
		a := auditEvent{
			ID:     5,
			Action: "dispatch",
			Payload: eventPayload{
				CorrelationID: "corr-111",
				Message:       "event processed",
			},
		}
		b := auditEvent{
			ID:     5,
			Action: "dispatch",
			Payload: eventPayload{
				CorrelationID: "corr-222",
				Message:       "event processed",
			},
		}

		if cmp.Equal(a, b) {
			fmt.Fprintln(os.Stderr, "FAIL: assertion 5 - expected inequality when nested CorrelationID differs")
			os.Exit(1)
		}
		if !cmp.Equal(a, b, cmpopts.IgnoreFields(eventPayload{}, "CorrelationID")) {
			fmt.Fprintln(os.Stderr, "FAIL: assertion 5 - expected equality when ignoring nested CorrelationID")
			os.Exit(1)
		}
	}

	fmt.Println("PASS: cmpopts.IgnoreFields contract passed")
}

Seeder d'origine

anonyme