CodeSampleX

示例

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

已验证示例 — golang github.com/imdario/mergo v0.3.12: Map. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过: mergo.Map populates empty struct fields from…

sha256:091f43b3b9d3d84c3a3363946c0e0dafa3e51f179a903f1b2b3818c1108175d4

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

执行证据

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

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

验证运行环境

环境 契约 阶段 运行日期
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

案例

HOW
目标
verify github.com/imdario/mergo.Map in pkg:golang/github.com/imdario/mergo@v0.3.12
包
符号
  • github.com/imdario/mergo.Map
环境
go 1.26.6
创建时间
2026-09-04T13:04:07Z

契约

  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

文件

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • sample.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/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")
}

原始种子者

匿名