CodeSampleX

Exemplo

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

Amostra verificada para golang github.com/google/go-cmp v0.7.0: cmpopts.IgnoreFields. O contrato rodou em go 1.26 · linux debian/x64 · docker e passou…

sha256:70c0917e48c953f1f25cbaa919ad2573e8e19eaa7366838ab12a72bd0b44786a

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
1
Chaves de assinatura que o compilaram
1
Ambiente declarado go 1.26 linux 24 · ubuntu · glibc 2.39 x64 go 1.26 go go

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
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-04

Caso

HOW
Objetivo
verify github.com/google/go-cmp/cmp/cmpopts.IgnoreFields in pkg:golang/github.com/google/go-cmp@v0.7.0
Pacotes
Símbolos
  • github.com/google/go-cmp/cmp/cmpopts.IgnoreFields
Ambiente
go 1.26
Criado
2026-09-04T01:10:22Z

Contrato

  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

Arquivos

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

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/google/go-cmp@v0.7.0
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:d03f43282074e30347cb432f93a00ec9f0a52126e195bf94497c2c0ef499fb2e","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.7.0","kind":"HOW","packages":["pkg:golang/github.com/google/go-cmp@v0.7.0"],"schemaVersion":1,"symbols":["github.com/google/go-cmp/cmp/cmpopts.IgnoreFields"]},"contractCommand":["go","test","./..."],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","language":"go","libc":"glibc","libcVersion":"2.39","moduleSystem":"go.mod","os":"linux","osVersionBucket":"24","packageManager":"go","runtime":"go","runtimeVersion":"1.26","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/google/go-cmp@v0.7.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/google/go-cmp@v0.7.0","symbols":["github.com/google/go-cmp/cmp/cmpopts.IgnoreFields"],"verifierAdapter":"golang@1"}
go.mod
module example.com/go-cmp-ignorefields

go 1.24.0

require github.com/google/go-cmp v0.7.0
go.sum
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
ignorefields.go
package sample

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

// AuditEvent represents a logged audit event with various metadata fields.
type AuditEvent struct {
	ID        int
	Action    string
	Actor     string
	Timestamp string
	Payload   EventPayload
}

// EventPayload contains nested payload details for an audit event.
type EventPayload struct {
	CorrelationID string
	Message       string
}

// DiffAuditEvents compares two AuditEvent values, ignoring the specified fields on AuditEvent.
func DiffAuditEvents(x, y AuditEvent, ignoredFields ...string) string {
	var opts []cmp.Option
	if len(ignoredFields) > 0 {
		opts = append(opts, cmpopts.IgnoreFields(AuditEvent{}, ignoredFields...))
	}
	return cmp.Diff(x, y, opts...)
}

// DiffAuditEventSlices compares slices of AuditEvent values, ignoring the specified fields on AuditEvent.
func DiffAuditEventSlices(x, y []AuditEvent, ignoredFields ...string) string {
	var opts []cmp.Option
	if len(ignoredFields) > 0 {
		opts = append(opts, cmpopts.IgnoreFields(AuditEvent{}, ignoredFields...))
	}
	return cmp.Diff(x, y, opts...)
}

// DiffAuditEventsWithNestedIgnore compares two AuditEvent values, ignoring specified fields on the nested EventPayload.
func DiffAuditEventsWithNestedIgnore(x, y AuditEvent, ignoredPayloadFields ...string) string {
	var opts []cmp.Option
	if len(ignoredPayloadFields) > 0 {
		opts = append(opts, cmpopts.IgnoreFields(EventPayload{}, ignoredPayloadFields...))
	}
	return cmp.Diff(x, y, opts...)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/google/go-cmp/cmp/cmpopts.IgnoreFields in pkg:golang/github.com/google/go-cmp@v0.7.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/google/go-cmp@v0.7.0"
  ],
  "symbols": [
    "github.com/google/go-cmp/cmp/cmpopts.IgnoreFields"
  ]
}
test/contract_test.go
package test

import (
	"strings"
	"testing"

	sample "example.com/go-cmp-ignorefields"
	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
)

func TestIgnoreFieldsContract(t *testing.T) {
	// 1. cmpopts.IgnoreFields ignores a single specified struct field during comparison
	t.Run("ignores single specified field", func(t *testing.T) {
		a := sample.AuditEvent{
			ID:        1,
			Action:    "login",
			Actor:     "user-1",
			Timestamp: "2026-09-01T00:00:00Z",
		}
		b := sample.AuditEvent{
			ID:        1,
			Action:    "login",
			Actor:     "user-1",
			Timestamp: "2026-09-01T12:00:00Z",
		}

		diffUnfiltered := cmp.Diff(a, b)
		if diffUnfiltered == "" {
			t.Fatal("expected diff when Timestamp differs without IgnoreFields")
		}

		diffFiltered := cmp.Diff(a, b, cmpopts.IgnoreFields(sample.AuditEvent{}, "Timestamp"))
		if diffFiltered != "" {
			t.Fatalf("expected no diff when ignoring Timestamp, got:\n%s", diffFiltered)
		}
	})

	// 2. cmpopts.IgnoreFields ignores multiple specified struct fields during comparison
	t.Run("ignores multiple specified fields", func(t *testing.T) {
		a := sample.AuditEvent{
			ID:        100,
			Action:    "read",
			Actor:     "reader-a",
			Timestamp: "2026-09-02T01:00:00Z",
		}
		b := sample.AuditEvent{
			ID:        200,
			Action:    "read",
			Actor:     "reader-a",
			Timestamp: "2026-09-02T02:00:00Z",
		}

		diff := cmp.Diff(a, b, cmpopts.IgnoreFields(sample.AuditEvent{}, "ID", "Timestamp"))
		if diff != "" {
			t.Fatalf("expected no diff when ignoring ID and Timestamp, got:\n%s", diff)
		}
	})

	// 3. cmpopts.IgnoreFields detects differences when non-ignored fields differ
	t.Run("detects differences when non-ignored fields differ", func(t *testing.T) {
		a := sample.AuditEvent{
			ID:        10,
			Action:    "create",
			Actor:     "admin",
			Timestamp: "2026-09-03T00:00:00Z",
		}
		b := sample.AuditEvent{
			ID:        10,
			Action:    "delete",
			Actor:     "admin",
			Timestamp: "2026-09-03T10:00:00Z",
		}

		diff := cmp.Diff(a, b, cmpopts.IgnoreFields(sample.AuditEvent{}, "Timestamp"))
		if diff == "" {
			t.Fatal("expected diff because Action differs, but got none")
		}
		if !strings.Contains(diff, "create") || !strings.Contains(diff, "delete") {
			t.Fatalf("expected diff to show Action differences, got:\n%s", diff)
		}
	})

	// 4. cmpopts.IgnoreFields applies field ignoring when comparing slices of structs
	t.Run("applies field ignoring on slices of structs", func(t *testing.T) {
		listA := []sample.AuditEvent{
			{ID: 1, Action: "sync", Timestamp: "10:00"},
			{ID: 2, Action: "flush", Timestamp: "10:01"},
		}
		listB := []sample.AuditEvent{
			{ID: 1, Action: "sync", Timestamp: "11:00"},
			{ID: 2, Action: "flush", Timestamp: "11:01"},
		}

		diffWithout := cmp.Diff(listA, listB)
		if diffWithout == "" {
			t.Fatal("expected diff for slice items with differing timestamps")
		}

		diffWith := cmp.Diff(listA, listB, cmpopts.IgnoreFields(sample.AuditEvent{}, "Timestamp"))
		if diffWith != "" {
			t.Fatalf("expected no diff for slices when ignoring Timestamp, got:\n%s", diffWith)
		}
	})

	// 5. cmpopts.IgnoreFields ignores fields in nested structs when target struct type is specified
	t.Run("ignores fields in nested structs", func(t *testing.T) {
		a := sample.AuditEvent{
			ID:     5,
			Action: "dispatch",
			Payload: sample.EventPayload{
				CorrelationID: "corr-111",
				Message:       "event processed",
			},
		}
		b := sample.AuditEvent{
			ID:     5,
			Action: "dispatch",
			Payload: sample.EventPayload{
				CorrelationID: "corr-222",
				Message:       "event processed",
			},
		}

		diffWithout := cmp.Diff(a, b)
		if diffWithout == "" {
			t.Fatal("expected diff when nested CorrelationID differs")
		}

		diffWith := cmp.Diff(a, b, cmpopts.IgnoreFields(sample.EventPayload{}, "CorrelationID"))
		if diffWith != "" {
			t.Fatalf("expected no diff when ignoring nested CorrelationID, got:\n%s", diffWith)
		}
	})
}

Seeder de origem

anônimo