CodeSampleX

サンプル

google.golang.org/protobuf v1.32.0: structpb.Struct.AsMap

検証済みサンプル — golang google.golang.org/protobuf v1.32.0: structpb.Struct.AsMap. go 1.26 · linux debian/x64 · docker で contract を実行し、成功しました.

sha256:6ed378aba73661e7141d2b919a53d4919233ca61b5fa3b6befbe3db73fdc04c6

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 go linux 24 · ubuntu · glibc 2.39 x64 go go go

検証実行環境

環境 コントラクト ステージ 実行日
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-01

ケース

HOW
ゴール
verify google.golang.org/protobuf/types/known/structpb.Struct.AsMap in pkg:golang/google.golang.org/protobuf@v1.32.0
パッケージ
シンボル
  • google.golang.org/protobuf/types/known/structpb.Struct.AsMap
環境
go
作成日
2026-09-01T06:33:07Z

コントラクト

  1. structpb.NewStruct converts a map[string]interface{} into *structpb.Struct, and Struct.AsMap converts it back to map[string]interface{} with preserved values.
  2. Calling AsMap on a nil *structpb.Struct receiver safely returns an empty, non-nil map without panicking.
  3. Calling AsMap on an empty *structpb.Struct returns an empty map[string]interface{}.
  4. Struct.AsMap recursively converts nested Struct and ListValue fields into nested map[string]interface{} and []interface{} structures.
  5. Modifications to the map returned by Struct.AsMap do not mutate the underlying Protocol Buffers Struct message.

ファイル

  • NOTES.md
  • PROMPT.md
  • asmap_test.go
  • csx.json
  • go.mod
  • go.sum
  • spec.json

ソースアーティファクトをダウンロード (tar.gz)

ソース

NOTES.md
# Protocol Buffers structpb.Struct.AsMap Conversion

This sample tests and verifies conversion behavior of `google.golang.org/protobuf/types/known/structpb.Struct.AsMap` in `google.golang.org/protobuf@v1.32.0`.

## Verified Contract Properties

1. `structpb.NewStruct converts a map[string]interface{} into *structpb.Struct, and Struct.AsMap converts it back to map[string]interface{} with preserved values.`
2. `Calling AsMap on a nil *structpb.Struct receiver safely returns an empty, non-nil map without panicking.`
3. `Calling AsMap on an empty *structpb.Struct returns an empty map[string]interface{}.`
4. `Struct.AsMap recursively converts nested Struct and ListValue fields into nested map[string]interface{} and []interface{} structures.`
5. `Modifications to the map returned by Struct.AsMap do not mutate the underlying Protocol Buffers Struct message.`
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 google.golang.org/protobuf/types/known/structpb.Struct.AsMap in pkg:golang/google.golang.org/protobuf@v1.32.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/google.golang.org/protobuf@v1.32.0
Demonstrate these symbols/APIs:
  - google.golang.org/protobuf/types/known/structpb.Struct.AsMap
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

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.
asmap_test.go
package asmapdemo_test

import (
	"reflect"
	"testing"

	"google.golang.org/protobuf/types/known/structpb"
)

// TestStructAsMapBasic verifies structpb.NewStruct converts Go maps into *structpb.Struct,
// and Struct.AsMap converts it back to map[string]interface{} with preserved values.
func TestStructAsMapBasic(t *testing.T) {
	input := map[string]interface{}{
		"title":   "task-worker",
		"count":   float64(42),
		"active":  true,
		"details": "verified",
	}

	st, err := structpb.NewStruct(input)
	if err != nil {
		t.Fatalf("NewStruct failed: %v", err)
	}

	got := st.AsMap()
	if !reflect.DeepEqual(input, got) {
		t.Fatalf("AsMap() mismatch:\nwant: %#v\ngot:  %#v", input, got)
	}
}

// TestStructAsMapNilReceiver verifies calling AsMap on a nil *structpb.Struct receiver
// safely returns an empty, non-nil map without panicking.
func TestStructAsMapNilReceiver(t *testing.T) {
	var nilStruct *structpb.Struct
	got := nilStruct.AsMap()
	if got == nil {
		t.Fatalf("expected non-nil map from nil receiver, got nil")
	}
	if len(got) != 0 {
		t.Fatalf("expected empty map from nil receiver, got %v", got)
	}
}

// TestStructAsMapEmptyStruct verifies calling AsMap on an empty *structpb.Struct
// returns an empty map[string]interface{}.
func TestStructAsMapEmptyStruct(t *testing.T) {
	emptyStruct, err := structpb.NewStruct(map[string]interface{}{})
	if err != nil {
		t.Fatalf("NewStruct empty map failed: %v", err)
	}

	got := emptyStruct.AsMap()
	if got == nil {
		t.Fatalf("expected non-nil map, got nil")
	}
	if len(got) != 0 {
		t.Fatalf("expected empty map, got %v", got)
	}
}

// TestStructAsMapNested verifies Struct.AsMap recursively converts nested Struct
// and ListValue fields into nested map[string]interface{} and []interface{} structures.
func TestStructAsMapNested(t *testing.T) {
	input := map[string]interface{}{
		"service": "codesamplex",
		"config": map[string]interface{}{
			"timeout_ms": float64(5000),
			"enabled":    true,
		},
		"endpoints": []interface{}{
			"http://example.com/api/v1",
			"http://example.com/api/v2",
		},
	}

	st, err := structpb.NewStruct(input)
	if err != nil {
		t.Fatalf("NewStruct with nested data failed: %v", err)
	}

	got := st.AsMap()
	if !reflect.DeepEqual(input, got) {
		t.Fatalf("AsMap() nested mismatch:\nwant: %#v\ngot:  %#v", input, got)
	}

	// Verify nested types
	configMap, ok := got["config"].(map[string]interface{})
	if !ok {
		t.Fatalf("expected config to be map[string]interface{}, got %T", got["config"])
	}
	if configMap["timeout_ms"] != float64(5000) {
		t.Errorf("expected timeout_ms 5000, got %v", configMap["timeout_ms"])
	}

	endpointsSlice, ok := got["endpoints"].([]interface{})
	if !ok {
		t.Fatalf("expected endpoints to be []interface{}, got %T", got["endpoints"])
	}
	if len(endpointsSlice) != 2 || endpointsSlice[0] != "http://example.com/api/v1" {
		t.Errorf("endpoints slice mismatch: %v", endpointsSlice)
	}
}

// TestStructAsMapCopySemantics verifies modifying the map returned by Struct.AsMap
// does not mutate the original structpb.Struct message.
func TestStructAsMapCopySemantics(t *testing.T) {
	input := map[string]interface{}{
		"key": "original",
	}

	st, err := structpb.NewStruct(input)
	if err != nil {
		t.Fatalf("NewStruct failed: %v", err)
	}

	m1 := st.AsMap()
	m1["key"] = "mutated"
	m1["new_key"] = "added"

	m2 := st.AsMap()
	if m2["key"] != "original" {
		t.Fatalf("expected second AsMap() call to return original value 'original', got %v", m2["key"])
	}
	if _, exists := m2["new_key"]; exists {
		t.Fatalf("expected new_key to not exist in original struct after modifying first map")
	}
}
csx.json
{"case":{"caseId":"case:sha256:045ca6308909cf6f6ff9160bdc4caecebf92f01608788ba76ae68ce46cd47a3b","contract":["structpb.NewStruct converts a map[string]interface{} into *structpb.Struct, and Struct.AsMap converts it back to map[string]interface{} with preserved values.","Calling AsMap on a nil *structpb.Struct receiver safely returns an empty, non-nil map without panicking.","Calling AsMap on an empty *structpb.Struct returns an empty map[string]interface{}.","Struct.AsMap recursively converts nested Struct and ListValue fields into nested map[string]interface{} and []interface{} structures.","Modifications to the map returned by Struct.AsMap do not mutate the underlying Protocol Buffers Struct message."],"goal":"verify google.golang.org/protobuf/types/known/structpb.Struct.AsMap in pkg:golang/google.golang.org/protobuf@v1.32.0","kind":"HOW","packages":["pkg:golang/google.golang.org/protobuf@v1.32.0"],"schemaVersion":1,"symbols":["google.golang.org/protobuf/types/known/structpb.Struct.AsMap"]},"contractCommand":["go","test","./..."],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","executionContext":"go","language":"go","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","runtime":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/google.golang.org/protobuf@v1.32.0"],"schemaVersion":1,"subject":"pkg:golang/google.golang.org/protobuf@v1.32.0","symbols":["google.golang.org/protobuf/types/known/structpb.Struct.AsMap"],"verifierAdapter":"golang@1"}
go.mod
module example.com/asmapdemo

go 1.22

require google.golang.org/protobuf v1.32.0
go.sum
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify google.golang.org/protobuf/types/known/structpb.Struct.AsMap in pkg:golang/google.golang.org/protobuf@v1.32.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/google.golang.org/protobuf@v1.32.0"
  ],
  "symbols": [
    "google.golang.org/protobuf/types/known/structpb.Struct.AsMap"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}

オリジンシーダー

匿名