CodeSampleX

Exemple

github.com/imdario/mergo v0.3.12: Map

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

sha256:091f43b3b9d3d84c3a3363946c0e0dafa3e51f179a903f1b2b3818c1108175d4

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é go 1.26 linux 24 · ubuntu · glibc 2.39 x64 go 1.26 go go 1

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-09-04

Cas

HOW
Objectif
verify github.com/imdario/mergo.Map in pkg:golang/github.com/imdario/mergo@v0.3.12
Paquets
Symboles
  • github.com/imdario/mergo.Map
Environnement
go 1.26.6
Créé
2026-09-04T13:04:07Z

Contrat

  1. mergo.Map populates empty struct fields from matching map keys
  2. mergo.Map preserves existing non-zero struct field values by default
  3. mergo.Map with mergo.WithOverride overwrites existing struct fields with map values
  4. mergo.Map merges source map entries into destination map without overwriting existing keys by default
  5. mergo.Map returns an error when destination is not a pointer

Fichiers

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • sample.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/imdario/mergo.Map in pkg:golang/github.com/imdario/mergo@v0.3.12
Kind: HOW

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

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:dd95d172f1cb4f1cdaee3bc13d078440c35d0fa86ce0441d20f86898ee5aa3f2","contract":["mergo.Map populates empty struct fields from matching map keys","mergo.Map preserves existing non-zero struct field values by default","mergo.Map with mergo.WithOverride overwrites existing struct fields with map values","mergo.Map merges source map entries into destination map without overwriting existing keys by default","mergo.Map returns an error when destination is not a pointer"],"goal":"verify github.com/imdario/mergo.Map in pkg:golang/github.com/imdario/mergo@v0.3.12","kind":"HOW","packages":["pkg:golang/github.com/imdario/mergo@v0.3.12"],"schemaVersion":1,"symbols":["github.com/imdario/mergo.Map"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","language":"go","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","packageManagerVersion":"1.26.6","runtime":"go","runtimeVersion":"1.26.6","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/imdario/mergo@v0.3.12"],"schemaVersion":1,"subject":"pkg:golang/github.com/imdario/mergo@v0.3.12","symbols":["github.com/imdario/mergo.Map"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require github.com/imdario/mergo v0.3.12
go.sum
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
sample.go
package sample

import (
	"errors"
	"fmt"

	"github.com/imdario/mergo"
)

// Config represents a sample configuration struct to demonstrate map-to-struct merging.
type Config struct {
	Host    string
	Port    int
	Debug   bool
	Timeout int
}

// MergeMapToConfig merges a map into a Config struct pointer.
// If override is true, existing non-zero fields in dst are overwritten by non-empty src values.
func MergeMapToConfig(dst *Config, src map[string]interface{}, override bool) error {
	if dst == nil {
		return errors.New("dst cannot be nil")
	}
	var opts []func(*mergo.Config)
	if override {
		opts = append(opts, mergo.WithOverride)
	}
	if err := mergo.Map(dst, src, opts...); err != nil {
		return fmt.Errorf("mergo.Map failed: %w", err)
	}
	return nil
}

// MergeStringMap merges source map entries into destination map.
// If override is true, existing keys in dst are overwritten by src values.
func MergeStringMap(dst *map[string]interface{}, src map[string]interface{}, override bool) error {
	if dst == nil {
		return errors.New("dst pointer cannot be nil")
	}
	var opts []func(*mergo.Config)
	if override {
		opts = append(opts, mergo.WithOverride)
	}
	if err := mergo.Map(dst, src, opts...); err != nil {
		return fmt.Errorf("mergo.Map failed: %w", err)
	}
	return nil
}

// DirectMap wraps mergo.Map directly.
func DirectMap(dst, src interface{}, opts ...func(*mergo.Config)) error {
	return mergo.Map(dst, src, opts...)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/imdario/mergo.Map in pkg:golang/github.com/imdario/mergo@v0.3.12",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/imdario/mergo@v0.3.12"
  ],
  "symbols": [
    "github.com/imdario/mergo.Map"
  ]
}
test/contract.go
package main

import (
	"errors"
	"fmt"
	"os"

	"github.com/imdario/mergo"
	"sample"
)

func main() {
	// Assertion 1: mergo.Map populates empty struct fields from matching map keys
	cfg1 := sample.Config{}
	src1 := map[string]interface{}{
		"Host":  "localhost",
		"Port":  8080,
		"Debug": true,
	}
	if err := sample.MergeMapToConfig(&cfg1, src1, false); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 1 failed: %v\n", err)
		os.Exit(1)
	}
	if cfg1.Host != "localhost" || cfg1.Port != 8080 || !cfg1.Debug {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 1 failed: unexpected fields: %+v\n", cfg1)
		os.Exit(1)
	}

	// Assertion 2: mergo.Map preserves existing non-zero struct field values by default
	cfg2 := sample.Config{
		Host:    "original.host",
		Port:    9000,
		Timeout: 30,
	}
	src2 := map[string]interface{}{
		"Host":  "new.host",
		"Port":  8080,
		"Debug": true,
	}
	if err := sample.MergeMapToConfig(&cfg2, src2, false); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 2 failed: %v\n", err)
		os.Exit(1)
	}
	if cfg2.Host != "original.host" || cfg2.Port != 9000 || cfg2.Timeout != 30 || !cfg2.Debug {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 2 failed: default merge altered existing fields: %+v\n", cfg2)
		os.Exit(1)
	}

	// Assertion 3: mergo.Map with mergo.WithOverride overwrites existing struct fields with map values
	cfg3 := sample.Config{
		Host: "original.host",
		Port: 9000,
	}
	src3 := map[string]interface{}{
		"Host": "new.host",
		"Port": 8080,
	}
	if err := sample.MergeMapToConfig(&cfg3, src3, true); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 3 failed: %v\n", err)
		os.Exit(1)
	}
	if cfg3.Host != "new.host" || cfg3.Port != 8080 {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 3 failed: override did not update fields: %+v\n", cfg3)
		os.Exit(1)
	}

	// Assertion 4: mergo.Map merges source map entries into destination map without overwriting existing keys by default
	dstMap := map[string]interface{}{
		"a": 1,
		"b": "keep",
	}
	srcMap := map[string]interface{}{
		"b": "overwrite_attempt",
		"c": true,
	}
	if err := sample.MergeStringMap(&dstMap, srcMap, false); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 4 failed: %v\n", err)
		os.Exit(1)
	}
	if dstMap["a"] != 1 || dstMap["b"] != "keep" || dstMap["c"] != true {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 4 failed: unexpected map contents: %+v\n", dstMap)
		os.Exit(1)
	}

	// Assertion 5: mergo.Map returns an error when destination is not a pointer
	nonPtrCfg := sample.Config{Host: "base"}
	errNonPtr := sample.DirectMap(nonPtrCfg, src1)
	if errNonPtr == nil {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 5 failed: expected error when passing non-pointer destination, got nil\n")
		os.Exit(1)
	}
	if !errors.Is(errNonPtr, mergo.ErrNonPointerAgument) {
		// mergo typically returns ErrNonPointerAgument
		// Verify an error is returned
	}

	fmt.Println("PASS: all mergo.Map contract assertions succeeded")
}

Seeder d'origine

anonyme