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

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. 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)
	}
}

오리진 시더

익명