CodeSampleX

Exemple

github.com/imdario/mergo v0.3.8: Merge

Échantillon vérifié pour golang github.com/imdario/mergo v0.3.8: Merge. Le contrat s'est exécuté sur go 1.26 · linux alpine/x64 · docker et a réussi.

sha256:138ab318ae6127bb59d13da0cc6884906c5e01bc9334574e400c294803b34e19

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 alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f…
2026-08-28

Cas

HOW
Objectif
verify pkg:golang/github.com/imdario/mergo@v0.3.8
Paquets
Symboles
  • mergo.Merge
Créé
2026-08-28T00:48:03Z

Contrat

  1. mergo.Merge merges non-zero fields from a source struct into zero-value fields of a destination struct without overwriting non-zero destination fields.
  2. mergo.Merge with mergo.WithOverride overwrites existing non-zero destination fields with source values.
  3. mergo.Merge with mergo.WithAppendSlice appends source slice elements to destination slice elements.
  4. mergo.Merge returns an error when destination or source argument is nil.

Fichiers

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract_test.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 pkg:golang/github.com/imdario/mergo@v0.3.8
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/imdario/mergo@v0.3.8
Demonstrate these symbols/APIs:
  - mergo.Merge

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:cf0fffe452267b3fbff232836f9cdcf060d84b0b07a3df66339306001f91e2ec","contract":["mergo.Merge merges non-zero fields from a source struct into zero-value fields of a destination struct without overwriting non-zero destination fields.","mergo.Merge with mergo.WithOverride overwrites existing non-zero destination fields with source values.","mergo.Merge with mergo.WithAppendSlice appends source slice elements to destination slice elements.","mergo.Merge returns an error when destination or source argument is nil."],"goal":"verify pkg:golang/github.com/imdario/mergo@v0.3.8","kind":"HOW","packages":["pkg:golang/github.com/imdario/mergo@v0.3.8"],"schemaVersion":1,"symbols":["mergo.Merge"]},"contractCommand":["go","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/imdario/mergo@v0.3.8"],"schemaVersion":1,"subject":"pkg:golang/github.com/imdario/mergo@v0.3.8","symbols":["mergo.Merge"],"verifierAdapter":"golang@1"}
go.mod
module example.com/mergomerge

go 1.22

require github.com/imdario/mergo v0.3.8

require (
	github.com/stretchr/testify v1.12.1 // indirect
	gopkg.in/yaml.v2 v2.4.0 // indirect
)
go.sum
github.com/imdario/mergo v0.3.8 h1:CGgOkSJeqMRmt0D9XLWExdT4m4F1vd3FV3VPt+0VxkQ=
github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE=
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
main.go
package main

import (
	"fmt"

	"github.com/imdario/mergo"
)

type Config struct {
	Host    string
	Port    int
	Timeout int
	Tags    []string
}

func main() {
	defaultCfg := Config{
		Host:    "localhost",
		Port:    8080,
		Timeout: 30,
		Tags:    []string{"default"},
	}

	userCfg := Config{
		Port: 9000,
	}

	if err := mergo.Merge(&userCfg, defaultCfg); err != nil {
		panic(err)
	}

	fmt.Printf("Host: %s, Port: %d, Timeout: %d, Tags: %v\n", userCfg.Host, userCfg.Port, userCfg.Timeout, userCfg.Tags)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/github.com/imdario/mergo@v0.3.8",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/imdario/mergo@v0.3.8"
  ],
  "symbols": [
    "mergo.Merge"
  ]
}
test/contract_test.go
package test

import (
	"reflect"
	"testing"

	"github.com/imdario/mergo"
)

type ServerConfig struct {
	Host    string
	Port    int
	Timeout int
	Tags    []string
}

func TestMergeDefaultWithoutOverwrite(t *testing.T) {
	dst := ServerConfig{
		Port: 9000,
	}
	src := ServerConfig{
		Host:    "localhost",
		Port:    8080,
		Timeout: 30,
		Tags:    []string{"web", "api"},
	}

	if err := mergo.Merge(&dst, src); err != nil {
		t.Fatalf("mergo.Merge failed: %v", err)
	}

	if dst.Host != "localhost" {
		t.Errorf("expected Host 'localhost', got %q", dst.Host)
	}
	if dst.Port != 9000 {
		t.Errorf("expected Port 9000 (preserved), got %d", dst.Port)
	}
	if dst.Timeout != 30 {
		t.Errorf("expected Timeout 30, got %d", dst.Timeout)
	}
	if !reflect.DeepEqual(dst.Tags, []string{"web", "api"}) {
		t.Errorf("expected Tags [web api], got %v", dst.Tags)
	}
}

func TestMergeWithOverride(t *testing.T) {
	dst := ServerConfig{
		Host: "example.com",
		Port: 8080,
	}
	src := ServerConfig{
		Host: "localhost",
		Port: 9090,
	}

	if err := mergo.Merge(&dst, src, mergo.WithOverride); err != nil {
		t.Fatalf("mergo.Merge with WithOverride failed: %v", err)
	}

	if dst.Host != "localhost" {
		t.Errorf("expected Host 'localhost', got %q", dst.Host)
	}
	if dst.Port != 9090 {
		t.Errorf("expected Port 9090, got %d", dst.Port)
	}
}

func TestMergeWithAppendSlice(t *testing.T) {
	dst := ServerConfig{
		Tags: []string{"primary"},
	}
	src := ServerConfig{
		Tags: []string{"secondary", "backup"},
	}

	if err := mergo.Merge(&dst, src, mergo.WithAppendSlice); err != nil {
		t.Fatalf("mergo.Merge with WithAppendSlice failed: %v", err)
	}

	expectedTags := []string{"primary", "secondary", "backup"}
	if !reflect.DeepEqual(dst.Tags, expectedTags) {
		t.Errorf("expected Tags %v, got %v", expectedTags, dst.Tags)
	}
}

func TestMergeNilArguments(t *testing.T) {
	var nilDst *ServerConfig
	src := ServerConfig{Host: "localhost"}

	err := mergo.Merge(nilDst, src)
	if err == nil {
		t.Errorf("expected error when merging into nil dst, got nil")
	}
}

Seeder d'origine

anonyme