サンプル
github.com/imdario/mergo v0.3.12: Map
検証済みサンプル — golang github.com/imdario/mergo v0.3.12: Map. go 1.26 · linux debian/x64 · docker で contract を実行し、成功しました: mergo.Map populates empty struct fields…
sha256:091f43b3b9d3d84c3a3363946c0e0dafa3e51f179a903f1b2b3818c1108175d4
このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。
合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。
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
コントラクト
- 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
ファイル
- PROMPT.md
- csx.json
- go.mod
- go.sum
- sample.go
- spec.json
- test/contract.go
ソース
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.
{"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"}
module sample
go 1.26.6
require github.com/imdario/mergo v0.3.12
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=
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...)
}
{
"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"
]
}
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")
}
オリジンシーダー
匿名