CodeSampleX

サンプル

google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9: errdetails.LocalizedMessage

検証済みサンプル — golang google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9: errdetails.LocalizedMessage. go 1.26 · linux alpine/x64 …

sha256:cee7a56eb92c5e47b8182ae7a77f35233a4bff500d7c896902d4de03cc037f43

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

実行証拠

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

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

検証実行環境

環境 コントラクト ステージ 実行日
go 1.26 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f…
2026-08-27

ケース

HOW
ゴール
verify google.golang.org/genproto/googleapis/rpc/errdetails.LocalizedMessage in pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20241007155032-5fefd90f89a9
パッケージ
シンボル
  • google.golang.org/genproto/googleapis/rpc/errdetails.LocalizedMessage
作成日
2026-08-27T11:41:02Z

コントラクト

  1. LocalizedMessage sets and gets locale and message fields
  2. LocalizedMessage marshals to and unmarshals from binary protobuf format
  3. LocalizedMessage packs into and unpacks from anypb.Any protobuf container
  4. LocalizedMessage attaches to status.Status details for RPC error communication
  5. LocalizedMessage supports multiple distinct locales in error status details
  6. LocalizedMessage zero value and reset behaviors conform to protobuf message specification

ファイル

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.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 google.golang.org/genproto/googleapis/rpc/errdetails.LocalizedMessage in pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20241007155032-5fefd90f89a9
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20241007155032-5fefd90f89a9
Demonstrate these symbols/APIs:
  - google.golang.org/genproto/googleapis/rpc/errdetails.LocalizedMessage
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: esm
  - 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.
csx.json
{"case":{"caseId":"case:sha256:766d99b6df994bdb15ed34b76c095694edc82090959b356ac01c9ea85ad90774","contract":["LocalizedMessage sets and gets locale and message fields","LocalizedMessage marshals to and unmarshals from binary protobuf format","LocalizedMessage packs into and unpacks from anypb.Any protobuf container","LocalizedMessage attaches to status.Status details for RPC error communication","LocalizedMessage supports multiple distinct locales in error status details","LocalizedMessage zero value and reset behaviors conform to protobuf message specification"],"goal":"verify google.golang.org/genproto/googleapis/rpc/errdetails.LocalizedMessage in pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20241007155032-5fefd90f89a9","kind":"HOW","packages":["pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20241007155032-5fefd90f89a9"],"schemaVersion":1,"symbols":["google.golang.org/genproto/googleapis/rpc/errdetails.LocalizedMessage"]},"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/genproto/googleapis/rpc@v0.0.0-20241007155032-5fefd90f89a9"],"schemaVersion":1,"subject":"pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20241007155032-5fefd90f89a9","symbols":["google.golang.org/genproto/googleapis/rpc/errdetails.LocalizedMessage"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.22

require (
	google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9
	google.golang.org/protobuf v1.35.1
)
go.sum
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9 h1:QCqS/PdaHTSWGvupk2F/ehwHtGc0/GYkT+3GAcR1CCc=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9/go.mod h1:GX3210XPVPUjJbTUbvwI8f2IpZDMZuPJWDzDuebbviI=
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
main.go
package main

import (
	"fmt"
	"log"

	"google.golang.org/genproto/googleapis/rpc/code"
	"google.golang.org/genproto/googleapis/rpc/errdetails"
	"google.golang.org/genproto/googleapis/rpc/status"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/known/anypb"
)

func main() {
	// Create localized messages for different user locales
	enMsg := &errdetails.LocalizedMessage{
		Locale:  "en-US",
		Message: "The requested item is out of stock.",
	}
	koMsg := &errdetails.LocalizedMessage{
		Locale:  "ko-KR",
		Message: "요청하신 상품의 재고가 소진되었습니다.",
	}

	// Pack localized messages into anypb.Any containers
	anyEn, err := anypb.New(enMsg)
	if err != nil {
		log.Fatalf("failed to pack English localized message: %v", err)
	}

	anyKo, err := anypb.New(koMsg)
	if err != nil {
		log.Fatalf("failed to pack Korean localized message: %v", err)
	}

	// Attach localized messages to RPC status details
	st := &status.Status{
		Code:    int32(code.Code_FAILED_PRECONDITION),
		Message: "Item out of stock",
		Details: []*anypb.Any{anyEn, anyKo},
	}

	// Serialize status message to binary protobuf
	payload, err := proto.Marshal(st)
	if err != nil {
		log.Fatalf("failed to marshal status: %v", err)
	}

	// Deserialize status message
	var decoded status.Status
	if err := proto.Unmarshal(payload, &decoded); err != nil {
		log.Fatalf("failed to unmarshal status: %v", err)
	}

	fmt.Printf("Status Code: %d\n", decoded.GetCode())
	fmt.Printf("Status Message: %s\n", decoded.GetMessage())
	fmt.Printf("Details Count: %d\n", len(decoded.GetDetails()))

	// Unpack and display each LocalizedMessage detail
	for idx, anyDetail := range decoded.GetDetails() {
		var locMsg errdetails.LocalizedMessage
		if err := anypb.UnmarshalTo(anyDetail, &locMsg, proto.UnmarshalOptions{}); err != nil {
			log.Fatalf("failed to unpack detail #%d: %v", idx, err)
		}
		fmt.Printf("Detail [%d] Locale: %s, Message: %s\n", idx, locMsg.GetLocale(), locMsg.GetMessage())
	}
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify google.golang.org/genproto/googleapis/rpc/errdetails.LocalizedMessage in pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20241007155032-5fefd90f89a9",
  "kind": "HOW",
  "packages": [
    "pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20241007155032-5fefd90f89a9"
  ],
  "symbols": [
    "google.golang.org/genproto/googleapis/rpc/errdetails.LocalizedMessage"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "esm",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
test/contract.go
package main

import (
	"fmt"
	"os"

	"google.golang.org/genproto/googleapis/rpc/code"
	"google.golang.org/genproto/googleapis/rpc/errdetails"
	"google.golang.org/genproto/googleapis/rpc/status"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/known/anypb"
)

func assert(condition bool, message string) {
	if !condition {
		fmt.Fprintf(os.Stderr, "Assertion failed: %s\n", message)
		os.Exit(1)
	}
}

func main() {
	// 1. LocalizedMessage sets and gets locale and message fields
	msg := &errdetails.LocalizedMessage{
		Locale:  "en-US",
		Message: "The requested entity was not found.",
	}
	assert(msg.GetLocale() == "en-US", "Locale must match en-US")
	assert(msg.GetMessage() == "The requested entity was not found.", "Message must match")

	// 2. LocalizedMessage marshals to and unmarshals from binary protobuf format
	data, err := proto.Marshal(msg)
	assert(err == nil, "proto.Marshal(msg) should succeed")
	assert(len(data) > 0, "marshaled bytes should not be empty")

	var unmarshaledMsg errdetails.LocalizedMessage
	err = proto.Unmarshal(data, &unmarshaledMsg)
	assert(err == nil, "proto.Unmarshal should succeed")
	assert(unmarshaledMsg.GetLocale() == "en-US", "unmarshaled Locale must match en-US")
	assert(unmarshaledMsg.GetMessage() == "The requested entity was not found.", "unmarshaled Message must match")

	// 3. LocalizedMessage packs into and unpacks from anypb.Any protobuf container
	anyMsg, err := anypb.New(msg)
	assert(err == nil, "anypb.New(msg) should succeed")
	assert(anyMsg.MessageIs(&errdetails.LocalizedMessage{}), "anyMsg should match LocalizedMessage type")

	var unpackedMsg errdetails.LocalizedMessage
	err = anypb.UnmarshalTo(anyMsg, &unpackedMsg, proto.UnmarshalOptions{})
	assert(err == nil, "anypb.UnmarshalTo should succeed")
	assert(unpackedMsg.GetLocale() == "en-US", "unpacked Locale must match en-US")
	assert(unpackedMsg.GetMessage() == "The requested entity was not found.", "unpacked Message must match")

	// 4. LocalizedMessage attaches to status.Status details for RPC error communication
	st := &status.Status{
		Code:    int32(code.Code_NOT_FOUND),
		Message: "Resource not found",
		Details: []*anypb.Any{anyMsg},
	}
	assert(st.GetCode() == int32(code.Code_NOT_FOUND), "status code should match NOT_FOUND")
	assert(len(st.GetDetails()) == 1, "details count should be 1")

	stData, err := proto.Marshal(st)
	assert(err == nil, "proto.Marshal(st) should succeed")
	var decodedSt status.Status
	err = proto.Unmarshal(stData, &decodedSt)
	assert(err == nil, "proto.Unmarshal(stData) should succeed")
	assert(len(decodedSt.GetDetails()) == 1, "decoded details count should be 1")

	var extractedMsg errdetails.LocalizedMessage
	err = anypb.UnmarshalTo(decodedSt.GetDetails()[0], &extractedMsg, proto.UnmarshalOptions{})
	assert(err == nil, "unmarshaling detail to LocalizedMessage should succeed")
	assert(extractedMsg.GetLocale() == "en-US", "extracted Locale must match en-US")
	assert(extractedMsg.GetMessage() == "The requested entity was not found.", "extracted Message must match")

	// 5. LocalizedMessage supports multiple distinct locales in error status details
	locales := []struct {
		locale  string
		message string
	}{
		{"en-US", "Item out of stock."},
		{"ko-KR", "상품 재고가 없습니다."},
		{"ja-JP", "商品の在庫がありません。"},
		{"fr-FR", "L'article est en rupture de stock."},
	}

	var detailAnys []*anypb.Any
	for _, l := range locales {
		lm := &errdetails.LocalizedMessage{
			Locale:  l.locale,
			Message: l.message,
		}
		a, err := anypb.New(lm)
		assert(err == nil, fmt.Sprintf("anypb.New for locale %s should succeed", l.locale))
		detailAnys = append(detailAnys, a)
	}

	multiStatus := &status.Status{
		Code:    int32(code.Code_FAILED_PRECONDITION),
		Message: "Inventory precondition failed",
		Details: detailAnys,
	}
	assert(len(multiStatus.GetDetails()) == len(locales), "multiStatus details count must match locales count")

	for idx, anyDetail := range multiStatus.GetDetails() {
		var lm errdetails.LocalizedMessage
		err = anypb.UnmarshalTo(anyDetail, &lm, proto.UnmarshalOptions{})
		assert(err == nil, "unmarshal multi detail should succeed")
		assert(lm.GetLocale() == locales[idx].locale, fmt.Sprintf("locale at %d must match %s", idx, locales[idx].locale))
		assert(lm.GetMessage() == locales[idx].message, fmt.Sprintf("message at %d must match %s", idx, locales[idx].message))
	}

	// 6. LocalizedMessage zero value and reset behaviors conform to protobuf message specification
	zeroMsg := &errdetails.LocalizedMessage{}
	assert(zeroMsg.GetLocale() == "", "zero value locale must be empty string")
	assert(zeroMsg.GetMessage() == "", "zero value message must be empty string")

	msgToReset := &errdetails.LocalizedMessage{
		Locale:  "es-ES",
		Message: "Recurso no encontrado",
	}
	msgToReset.Reset()
	assert(msgToReset.GetLocale() == "", "reset message locale must be empty")
	assert(msgToReset.GetMessage() == "", "reset message text must be empty")

	fmt.Println("All LocalizedMessage contract assertions passed successfully.")
}

オリジンシーダー

匿名