CodeSampleX

샘플

google.golang.org/genproto/googleapis/rpc v0.0.0-20260825221802-da73d73af1c5: attribute_context.AttributeContext_Peer

검증된 샘플 — golang google.golang.org/genproto/googleapis/rpc v0.0.0-20260825221802-da73d73af1c5: attribute_context.AttributeContext_Peer. go 1.26 · linux…

sha256:3a2b94238e4f9a7a001116ba7d7c7c2ba9c57e72b03929bee251683516ab2f7e

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

케이스

HOW
목표
verify google.golang.org/genproto/googleapis/rpc/context/attribute_context.AttributeContext_Peer in pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20260825221802-da73d73af1c5
패키지
심벌
  • google.golang.org/genproto/googleapis/rpc/context/attribute_context.AttributeContext_Peer
생성일
2026-09-03T04:33:25Z

컨트랙트

  1. AttributeContext_Peer fields are initialized correctly
  2. FormatEndpoint formats ip and port as endpoint string
  3. GetLabel retrieves label value or returns false if missing
  4. nil AttributeContext_Peer getters return zero values safely
  5. Reset clears all fields of AttributeContext_Peer
  6. ProtoReflect returns non-nil valid protoreflect.Message
  7. AttributeContext embeds AttributeContext_Peer in Source and Destination

파일

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • peer.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/context/attribute_context.AttributeContext_Peer in pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20260825221802-da73d73af1c5
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20260825221802-da73d73af1c5
Demonstrate these symbols/APIs:
  - google.golang.org/genproto/googleapis/rpc/context/attribute_context.AttributeContext_Peer

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:3642933a5d957bb2ad4d424cabdf174423c1d798c3aa1bbc45c5893c2a5b19f8","contract":["AttributeContext_Peer fields are initialized correctly","FormatEndpoint formats ip and port as endpoint string","GetLabel retrieves label value or returns false if missing","nil AttributeContext_Peer getters return zero values safely","Reset clears all fields of AttributeContext_Peer","ProtoReflect returns non-nil valid protoreflect.Message","AttributeContext embeds AttributeContext_Peer in Source and Destination"],"goal":"verify google.golang.org/genproto/googleapis/rpc/context/attribute_context.AttributeContext_Peer in pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20260825221802-da73d73af1c5","kind":"HOW","packages":["pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20260825221802-da73d73af1c5"],"schemaVersion":1,"symbols":["google.golang.org/genproto/googleapis/rpc/context/attribute_context.AttributeContext_Peer"]},"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-20260825221802-da73d73af1c5"],"schemaVersion":1,"subject":"pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20260825221802-da73d73af1c5","symbols":["google.golang.org/genproto/googleapis/rpc/context/attribute_context.AttributeContext_Peer"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require google.golang.org/genproto/googleapis/rpc v0.0.0-20260825221802-da73d73af1c5

require google.golang.org/protobuf v1.36.12 // indirect
go.sum
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
google.golang.org/genproto/googleapis/rpc v0.0.0-20260825221802-da73d73af1c5 h1:1VUiZAXyC+zmiFYi+WLtBzr68Cj8wOofHjjrA/kkizc=
google.golang.org/genproto/googleapis/rpc v0.0.0-20260825221802-da73d73af1c5/go.mod h1:DjtHYE8FKJLivXcBEjGwndXfIC23G0VpXiXKqG179uA=
google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc=
google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
peer.go
package sample

import (
	"fmt"

	"google.golang.org/genproto/googleapis/rpc/context/attribute_context"
)

// NewPeer constructs an AttributeContext_Peer with the provided fields.
func NewPeer(ip string, port int64, principal string, regionCode string, labels map[string]string) *attribute_context.AttributeContext_Peer {
	return &attribute_context.AttributeContext_Peer{
		Ip:         ip,
		Port:       port,
		Principal:  principal,
		RegionCode: regionCode,
		Labels:     labels,
	}
}

// FormatEndpoint returns "ip:port" or empty string if IP is not set or peer is nil.
func FormatEndpoint(peer *attribute_context.AttributeContext_Peer) string {
	if peer == nil || peer.GetIp() == "" {
		return ""
	}
	return fmt.Sprintf("%s:%d", peer.GetIp(), peer.GetPort())
}

// GetLabel returns the value of the label key, or ("", false) if not found or peer is nil.
func GetLabel(peer *attribute_context.AttributeContext_Peer, key string) (string, bool) {
	if peer == nil {
		return "", false
	}
	labels := peer.GetLabels()
	if labels == nil {
		return "", false
	}
	val, ok := labels[key]
	return val, ok
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify google.golang.org/genproto/googleapis/rpc/context/attribute_context.AttributeContext_Peer in pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20260825221802-da73d73af1c5",
  "kind": "HOW",
  "packages": [
    "pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20260825221802-da73d73af1c5"
  ],
  "symbols": [
    "google.golang.org/genproto/googleapis/rpc/context/attribute_context.AttributeContext_Peer"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"

	"google.golang.org/genproto/googleapis/rpc/context/attribute_context"
	"sample"
)

func main() {
	labels := map[string]string{
		"env":  "prod",
		"tier": "frontend",
	}
	peer := sample.NewPeer("192.0.2.1", 8080, "spiffe://example.com/sa/client", "US", labels)

	// Assertion 1: AttributeContext_Peer fields are initialized correctly
	if peer.GetIp() != "192.0.2.1" {
		fmt.Fprintf(os.Stderr, "expected GetIp() to equal 192.0.2.1, got %q\n", peer.GetIp())
		os.Exit(1)
	}
	if peer.GetPort() != 8080 {
		fmt.Fprintf(os.Stderr, "expected GetPort() to equal 8080, got %d\n", peer.GetPort())
		os.Exit(1)
	}
	if peer.GetPrincipal() != "spiffe://example.com/sa/client" {
		fmt.Fprintf(os.Stderr, "expected GetPrincipal() to equal spiffe://example.com/sa/client, got %q\n", peer.GetPrincipal())
		os.Exit(1)
	}
	if peer.GetRegionCode() != "US" {
		fmt.Fprintf(os.Stderr, "expected GetRegionCode() to equal US, got %q\n", peer.GetRegionCode())
		os.Exit(1)
	}
	if peer.GetLabels()["env"] != "prod" || peer.GetLabels()["tier"] != "frontend" {
		fmt.Fprintf(os.Stderr, "labels mismatch in peer: %v\n", peer.GetLabels())
		os.Exit(1)
	}

	// Assertion 2: FormatEndpoint formats ip and port as endpoint string
	if endpoint := sample.FormatEndpoint(peer); endpoint != "192.0.2.1:8080" {
		fmt.Fprintf(os.Stderr, "expected endpoint 192.0.2.1:8080, got %q\n", endpoint)
		os.Exit(1)
	}

	// Assertion 3: GetLabel retrieves label value or returns false if missing
	if val, ok := sample.GetLabel(peer, "env"); !ok || val != "prod" {
		fmt.Fprintf(os.Stderr, "expected GetLabel('env') to be (prod, true), got (%q, %t)\n", val, ok)
		os.Exit(1)
	}
	if _, ok := sample.GetLabel(peer, "nonexistent"); ok {
		fmt.Fprintf(os.Stderr, "expected GetLabel('nonexistent') to return false\n")
		os.Exit(1)
	}

	// Assertion 4: nil AttributeContext_Peer getters return zero values safely
	var nilPeer *attribute_context.AttributeContext_Peer
	if nilPeer.GetIp() != "" || nilPeer.GetPort() != 0 || nilPeer.GetPrincipal() != "" || nilPeer.GetRegionCode() != "" || nilPeer.GetLabels() != nil {
		fmt.Fprintf(os.Stderr, "expected zero values from nil peer getters\n")
		os.Exit(1)
	}
	if sample.FormatEndpoint(nilPeer) != "" {
		fmt.Fprintf(os.Stderr, "expected FormatEndpoint(nil) to return empty string\n")
		os.Exit(1)
	}
	if _, ok := sample.GetLabel(nilPeer, "env"); ok {
		fmt.Fprintf(os.Stderr, "expected GetLabel(nil, 'env') to return false\n")
		os.Exit(1)
	}

	// Assertion 5: Reset clears all fields of AttributeContext_Peer
	resetPeer := sample.NewPeer("198.51.100.1", 443, "user", "CA", map[string]string{"k": "v"})
	resetPeer.Reset()
	if resetPeer.GetIp() != "" || resetPeer.GetPort() != 0 || resetPeer.GetPrincipal() != "" || resetPeer.GetRegionCode() != "" || resetPeer.GetLabels() != nil {
		fmt.Fprintf(os.Stderr, "expected Reset() to clear all fields\n")
		os.Exit(1)
	}

	// Assertion 6: ProtoReflect returns non-nil valid protoreflect.Message
	msg := peer.ProtoReflect()
	if msg == nil || !msg.IsValid() {
		fmt.Fprintf(os.Stderr, "expected valid protoreflect.Message from ProtoReflect()\n")
		os.Exit(1)
	}

	// Assertion 7: AttributeContext embeds AttributeContext_Peer in Source and Destination
	attrCtx := &attribute_context.AttributeContext{
		Source:      peer,
		Destination: sample.NewPeer("198.51.100.2", 9090, "service-b", "US", nil),
	}
	if attrCtx.GetSource().GetIp() != "192.0.2.1" {
		fmt.Fprintf(os.Stderr, "expected GetSource().GetIp() to be 192.0.2.1, got %q\n", attrCtx.GetSource().GetIp())
		os.Exit(1)
	}
	if attrCtx.GetDestination().GetPort() != 9090 {
		fmt.Fprintf(os.Stderr, "expected GetDestination().GetPort() to be 9090, got %d\n", attrCtx.GetDestination().GetPort())
		os.Exit(1)
	}

	fmt.Println("All google.golang.org/genproto/googleapis/rpc/context/attribute_context.AttributeContext_Peer contract assertions passed.")
}

오리진 시더

익명