CodeSampleX

サンプル

google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc: proto.Marshal, proto.Unmarshal, proto.Equal

検証済みサンプル — golang google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc: proto.Marshal, proto.Unmarshal, proto.Equal. go 1.26 · linux debian/x64 …

sha256:31c23a6a1550850c9aa337d675848e0235f96144a238efa883d984e552e63c10

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

実行証拠

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

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 linux 24 · ubuntu · glibc 2.39 x64 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-10-21

ケース

HOW
ゴール
verify pkg:golang/google.golang.org/protobuf@v1.23.1-0.20200526195155-81db48ad09cc
パッケージ
シンボル
  • proto.Marshal
  • proto.Unmarshal
  • proto.Equal
  • proto.Clone
作成日
2026-10-21T20:44:44Z

コントラクト

  1. proto.Marshal and proto.Unmarshal round-trip protobuf messages preserving field values and proto.Size matches wire length
  2. proto.Equal compares message equality across identical, differing, and nil messages
  3. proto.Clone creates an independent deep copy that does not mutate the original message when modified
  4. protojson.Marshal and protojson.Unmarshal serialize and deserialize well-known types (timestamppb.Timestamp and durationpb.Duration)

ファイル

  • 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 pkg:golang/google.golang.org/protobuf@v1.23.1-0.20200526195155-81db48ad09cc
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/google.golang.org/protobuf@v1.23.1-0.20200526195155-81db48ad09cc

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:b836dae985d0a55524160654dd884b635dc6d1e89f2782e85ff8a091740e8f6f","contract":["proto.Marshal and proto.Unmarshal round-trip protobuf messages preserving field values and proto.Size matches wire length","proto.Equal compares message equality across identical, differing, and nil messages","proto.Clone creates an independent deep copy that does not mutate the original message when modified","protojson.Marshal and protojson.Unmarshal serialize and deserialize well-known types (timestamppb.Timestamp and durationpb.Duration)"],"goal":"verify pkg:golang/google.golang.org/protobuf@v1.23.1-0.20200526195155-81db48ad09cc","kind":"HOW","packages":["pkg:golang/google.golang.org/protobuf@v1.23.1-0.20200526195155-81db48ad09cc"],"schemaVersion":1,"symbols":["proto.Marshal","proto.Unmarshal","proto.Equal","proto.Clone"]},"contractCommand":["go","run","./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/google.golang.org/protobuf@v1.23.1-0.20200526195155-81db48ad09cc"],"schemaVersion":1,"subject":"pkg:golang/google.golang.org/protobuf@v1.23.1-0.20200526195155-81db48ad09cc","symbols":["proto.Marshal","proto.Unmarshal","proto.Equal","proto.Clone"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.23

require google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc
go.sum
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/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 v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc h1:TnonUr8u3himcMY0vSh23jFOXA+cnucl1gB6EQTReBI=
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
sample.go
package sample

import (
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/known/wrapperspb"
)

// RoundTripString marshals a StringValue message to wire bytes and unmarshals it into a new message.
func RoundTripString(val string) (*wrapperspb.StringValue, []byte, error) {
	orig := &wrapperspb.StringValue{Value: val}
	wire, err := proto.Marshal(orig)
	if err != nil {
		return nil, nil, err
	}
	decoded := &wrapperspb.StringValue{}
	if err := proto.Unmarshal(wire, decoded); err != nil {
		return nil, nil, err
	}
	return decoded, wire, nil
}

// CloneMessage returns a deep copy of the given protobuf message using proto.Clone.
func CloneMessage(m proto.Message) proto.Message {
	return proto.Clone(m)
}

// EqualMessages reports whether two protobuf messages are equal using proto.Equal.
func EqualMessages(a, b proto.Message) bool {
	return proto.Equal(a, b)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/google.golang.org/protobuf@v1.23.1-0.20200526195155-81db48ad09cc",
  "kind": "HOW",
  "packages": [
    "pkg:golang/google.golang.org/protobuf@v1.23.1-0.20200526195155-81db48ad09cc"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"

	"sample"

	"google.golang.org/protobuf/encoding/protojson"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/known/durationpb"
	"google.golang.org/protobuf/types/known/timestamppb"
	"google.golang.org/protobuf/types/known/wrapperspb"
)

func main() {
	// 1. proto.Marshal and proto.Unmarshal round-trip protobuf messages preserving field values and proto.Size matches wire length
	orig := &wrapperspb.StringValue{Value: "hello-protobuf"}
	decoded, wire, err := sample.RoundTripString("hello-protobuf")
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: RoundTripString returned error: %v\n", err)
		os.Exit(1)
	}
	if len(wire) == 0 || len(wire) != proto.Size(orig) {
		fmt.Fprintf(os.Stderr, "FAIL: wire length %d does not match proto.Size %d\n", len(wire), proto.Size(orig))
		os.Exit(1)
	}
	if decoded.GetValue() != "hello-protobuf" {
		fmt.Fprintf(os.Stderr, "FAIL: expected 'hello-protobuf', got %q\n", decoded.GetValue())
		os.Exit(1)
	}

	// 2. proto.Equal compares message equality across identical, differing, and nil messages
	if !sample.EqualMessages(orig, decoded) {
		fmt.Fprintf(os.Stderr, "FAIL: expected orig and decoded to be equal\n")
		os.Exit(1)
	}
	diff := &wrapperspb.StringValue{Value: "different"}
	if sample.EqualMessages(orig, diff) {
		fmt.Fprintf(os.Stderr, "FAIL: expected orig and diff to not be equal\n")
		os.Exit(1)
	}
	var nilMsg *wrapperspb.StringValue
	if sample.EqualMessages(orig, nilMsg) {
		fmt.Fprintf(os.Stderr, "FAIL: expected orig and nilMsg to not be equal\n")
		os.Exit(1)
	}

	// 3. proto.Clone creates an independent deep copy that does not mutate the original message when modified
	cloned, ok := sample.CloneMessage(orig).(*wrapperspb.StringValue)
	if !ok || !proto.Equal(orig, cloned) {
		fmt.Fprintf(os.Stderr, "FAIL: cloned message does not equal original\n")
		os.Exit(1)
	}
	cloned.Value = "mutated"
	if orig.GetValue() != "hello-protobuf" || proto.Equal(orig, cloned) {
		fmt.Fprintf(os.Stderr, "FAIL: mutating cloned message affected original\n")
		os.Exit(1)
	}

	// 4. protojson.Marshal and protojson.Unmarshal serialize and deserialize well-known types (timestamppb.Timestamp and durationpb.Duration)
	ts := &timestamppb.Timestamp{Seconds: 1736944200, Nanos: 500}
	jsonBytes, err := protojson.Marshal(ts)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: protojson.Marshal failed: %v\n", err)
		os.Exit(1)
	}
	tsOut := &timestamppb.Timestamp{}
	if err := protojson.Unmarshal(jsonBytes, tsOut); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: protojson.Unmarshal failed: %v\n", err)
		os.Exit(1)
	}
	if !proto.Equal(ts, tsOut) || tsOut.GetSeconds() != 1736944200 || tsOut.GetNanos() != 500 {
		fmt.Fprintf(os.Stderr, "FAIL: timestamp round-trip mismatch\n")
		os.Exit(1)
	}

	dur := &durationpb.Duration{Seconds: 90, Nanos: 0}
	durJSON, err := protojson.Marshal(dur)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: protojson.Marshal duration failed: %v\n", err)
		os.Exit(1)
	}
	durOut := &durationpb.Duration{}
	if err := protojson.Unmarshal(durJSON, durOut); err != nil || !proto.Equal(dur, durOut) || durOut.GetSeconds() != 90 {
		fmt.Fprintf(os.Stderr, "FAIL: duration round-trip mismatch\n")
		os.Exit(1)
	}
}

オリジンシーダー

匿名