CodeSampleX

示例

google.golang.org/genproto/googleapis/rpc v0.0.0-20260427160629-7cedc36a6bc4: attribute_context.AttributeContext

已验证示例 — golang google.golang.org/genproto/googleapis/rpc v0.0.0-20260427160629-7cedc36a6bc4: attribute_context.AttributeContext. contract 在 go 1.26 · linux…

sha256:80c3847c259f5cdf075a620ec042a4788fe276edcbdfa13fd83742e316b52a71

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 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/genproto/googleapis/rpc/context/attribute_context.AttributeContext in pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20260427160629-7cedc36a6bc4
包
符号
  • google.golang.org/genproto/googleapis/rpc/context/attribute_context.AttributeContext
环境
go
创建时间
2026-09-01T10:34:34Z

契约

  1. AttributeContext fields Request, Response, Origin, Source, Destination, and Resource store structured RPC metadata accessible via getters.
  2. AttributeContext and its nested fields return safe nil or empty values when called on nil receivers.
  3. AttributeContext serializes to and deserializes from protocol buffer binary format preserving nested Request, Response, and Peer details.
  4. AttributeContext.Reset clears all fields and resets the message back to zero state.
  5. AttributeContext_Request stores Method, Path, Host, Headers, and Auth metadata accurately.

文件

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract_test.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 in pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20260427160629-7cedc36a6bc4
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20260427160629-7cedc36a6bc4
Demonstrate these symbols/APIs:
  - google.golang.org/genproto/googleapis/rpc/context/attribute_context.AttributeContext

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:4ddaed5377c7895fd95329d32c7c309f3cfcf97fb7b45ed294097a4f00f820fa","contract":["AttributeContext fields Request, Response, Origin, Source, Destination, and Resource store structured RPC metadata accessible via getters.","AttributeContext and its nested fields return safe nil or empty values when called on nil receivers.","AttributeContext serializes to and deserializes from protocol buffer binary format preserving nested Request, Response, and Peer details.","AttributeContext.Reset clears all fields and resets the message back to zero state.","AttributeContext_Request stores Method, Path, Host, Headers, and Auth metadata accurately."],"goal":"verify google.golang.org/genproto/googleapis/rpc/context/attribute_context.AttributeContext in pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20260427160629-7cedc36a6bc4","kind":"HOW","packages":["pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20260427160629-7cedc36a6bc4"],"schemaVersion":1,"symbols":["google.golang.org/genproto/googleapis/rpc/context/attribute_context.AttributeContext"]},"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/genproto/googleapis/rpc@v0.0.0-20260427160629-7cedc36a6bc4"],"schemaVersion":1,"subject":"pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20260427160629-7cedc36a6bc4","symbols":["google.golang.org/genproto/googleapis/rpc/context/attribute_context.AttributeContext"],"verifierAdapter":"golang@1"}
go.mod
module example.com/attributecontext

go 1.25.0

require (
	google.golang.org/genproto/googleapis/rpc v0.0.0-20260427160629-7cedc36a6bc4
	google.golang.org/protobuf v1.36.12
)
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-20260427160629-7cedc36a6bc4 h1:tEkOQcXgF6dH1G+MVKZrfpYvozGrzb91k6ha7jireSM=
google.golang.org/genproto/googleapis/rpc v0.0.0-20260427160629-7cedc36a6bc4/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8=
google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc=
google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
main.go
package main

import (
	"fmt"

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

func main() {
	attr := &attribute_context.AttributeContext{
		Origin: &attribute_context.AttributeContext_Peer{
			Ip:   "192.0.2.1",
			Port: 8080,
		},
		Request: &attribute_context.AttributeContext_Request{
			Id:     "req-12345",
			Method: "POST",
			Path:   "/v1/resources",
			Host:   "example.com",
			Scheme: "https",
			Headers: map[string]string{
				"content-type": "application/json",
			},
			Auth: &attribute_context.AttributeContext_Auth{
				Principal: "user-service@example.com",
			},
		},
		Response: &attribute_context.AttributeContext_Response{
			Code: 200,
			Size: 512,
		},
		Resource: &attribute_context.AttributeContext_Resource{
			Service: "resource-service",
			Name:    "items",
			Type:    "type.googleapis.com/example.Item",
		},
	}

	data, err := proto.Marshal(attr)
	if err != nil {
		panic(err)
	}

	var decoded attribute_context.AttributeContext
	if err := proto.Unmarshal(data, &decoded); err != nil {
		panic(err)
	}

	fmt.Printf("Request Method: %s\n", decoded.GetRequest().GetMethod())
	fmt.Printf("Request Path: %s\n", decoded.GetRequest().GetPath())
	fmt.Printf("Origin IP: %s\n", decoded.GetOrigin().GetIp())
	fmt.Printf("Response Code: %d\n", decoded.GetResponse().GetCode())
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify google.golang.org/genproto/googleapis/rpc/context/attribute_context.AttributeContext in pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20260427160629-7cedc36a6bc4",
  "kind": "HOW",
  "packages": [
    "pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20260427160629-7cedc36a6bc4"
  ],
  "symbols": [
    "google.golang.org/genproto/googleapis/rpc/context/attribute_context.AttributeContext"
  ]
}
test/contract_test.go
package test

import (
	"testing"

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

func TestAttributeContextFieldsAndGetters(t *testing.T) {
	req := &attribute_context.AttributeContext_Request{
		Id:     "req-101",
		Method: "GET",
		Path:   "/api/items",
		Host:   "example.com",
		Scheme: "https",
	}
	resp := &attribute_context.AttributeContext_Response{
		Code: 200,
		Size: 1024,
	}
	origin := &attribute_context.AttributeContext_Peer{
		Ip:   "192.0.2.1",
		Port: 8080,
	}
	source := &attribute_context.AttributeContext_Peer{
		Ip:   "10.0.0.1",
		Port: 443,
	}
	dest := &attribute_context.AttributeContext_Peer{
		Ip:   "10.0.0.2",
		Port: 8443,
	}
	resource := &attribute_context.AttributeContext_Resource{
		Service: "catalog-service",
		Name:    "catalog-item-1",
		Type:    "type.googleapis.com/example.CatalogItem",
	}

	ctx := &attribute_context.AttributeContext{
		Origin:      origin,
		Source:      source,
		Destination: dest,
		Request:     req,
		Response:    resp,
		Resource:    resource,
	}

	if got := ctx.GetOrigin().GetIp(); got != "192.0.2.1" {
		t.Errorf("GetOrigin().GetIp() = %q, want %q", got, "192.0.2.1")
	}
	if got := ctx.GetSource().GetIp(); got != "10.0.0.1" {
		t.Errorf("GetSource().GetIp() = %q, want %q", got, "10.0.0.1")
	}
	if got := ctx.GetDestination().GetPort(); got != 8443 {
		t.Errorf("GetDestination().GetPort() = %d, want 8443", got)
	}
	if got := ctx.GetRequest().GetMethod(); got != "GET" {
		t.Errorf("GetRequest().GetMethod() = %q, want %q", got, "GET")
	}
	if got := ctx.GetResponse().GetCode(); got != 200 {
		t.Errorf("GetResponse().GetCode() = %d, want 200", got)
	}
	if got := ctx.GetResource().GetService(); got != "catalog-service" {
		t.Errorf("GetResource().GetService() = %q, want %q", got, "catalog-service")
	}
}

func TestAttributeContextNilSafeGetters(t *testing.T) {
	var nilCtx *attribute_context.AttributeContext

	if nilCtx.GetOrigin() != nil {
		t.Errorf("nilCtx.GetOrigin() expected nil, got %v", nilCtx.GetOrigin())
	}
	if nilCtx.GetSource() != nil {
		t.Errorf("nilCtx.GetSource() expected nil, got %v", nilCtx.GetSource())
	}
	if nilCtx.GetDestination() != nil {
		t.Errorf("nilCtx.GetDestination() expected nil, got %v", nilCtx.GetDestination())
	}
	if nilCtx.GetRequest() != nil {
		t.Errorf("nilCtx.GetRequest() expected nil, got %v", nilCtx.GetRequest())
	}
	if nilCtx.GetResponse() != nil {
		t.Errorf("nilCtx.GetResponse() expected nil, got %v", nilCtx.GetResponse())
	}
	if nilCtx.GetResource() != nil {
		t.Errorf("nilCtx.GetResource() expected nil, got %v", nilCtx.GetResource())
	}
	if nilCtx.GetApi() != nil {
		t.Errorf("nilCtx.GetApi() expected nil, got %v", nilCtx.GetApi())
	}

	var nilReq *attribute_context.AttributeContext_Request
	if got := nilReq.GetId(); got != "" {
		t.Errorf("nilReq.GetId() = %q, want empty string", got)
	}
	if got := nilReq.GetMethod(); got != "" {
		t.Errorf("nilReq.GetMethod() = %q, want empty string", got)
	}
	if nilReq.GetHeaders() != nil {
		t.Errorf("nilReq.GetHeaders() expected nil, got %v", nilReq.GetHeaders())
	}
	if nilReq.GetAuth() != nil {
		t.Errorf("nilReq.GetAuth() expected nil, got %v", nilReq.GetAuth())
	}
}

func TestAttributeContextProtoSerialization(t *testing.T) {
	original := &attribute_context.AttributeContext{
		Origin: &attribute_context.AttributeContext_Peer{
			Ip:   "198.51.100.1",
			Port: 9000,
		},
		Request: &attribute_context.AttributeContext_Request{
			Id:     "req-roundtrip",
			Method: "PUT",
			Path:   "/v1/records/42",
			Host:   "example.com",
			Scheme: "https",
		},
		Response: &attribute_context.AttributeContext_Response{
			Code: 204,
		},
	}

	data, err := proto.Marshal(original)
	if err != nil {
		t.Fatalf("proto.Marshal failed: %v", err)
	}

	var decoded attribute_context.AttributeContext
	if err := proto.Unmarshal(data, &decoded); err != nil {
		t.Fatalf("proto.Unmarshal failed: %v", err)
	}

	if decoded.GetRequest().GetId() != original.GetRequest().GetId() {
		t.Errorf("decoded Request.Id = %q, want %q", decoded.GetRequest().GetId(), original.GetRequest().GetId())
	}
	if decoded.GetRequest().GetMethod() != original.GetRequest().GetMethod() {
		t.Errorf("decoded Request.Method = %q, want %q", decoded.GetRequest().GetMethod(), original.GetRequest().GetMethod())
	}
	if decoded.GetOrigin().GetIp() != original.GetOrigin().GetIp() {
		t.Errorf("decoded Origin.Ip = %q, want %q", decoded.GetOrigin().GetIp(), original.GetOrigin().GetIp())
	}
	if decoded.GetResponse().GetCode() != original.GetResponse().GetCode() {
		t.Errorf("decoded Response.Code = %d, want %d", decoded.GetResponse().GetCode(), original.GetResponse().GetCode())
	}
}

func TestAttributeContextReset(t *testing.T) {
	ctx := &attribute_context.AttributeContext{
		Request: &attribute_context.AttributeContext_Request{
			Id:     "req-to-reset",
			Method: "DELETE",
		},
		Response: &attribute_context.AttributeContext_Response{
			Code: 200,
		},
	}

	ctx.Reset()

	if ctx.GetRequest() != nil {
		t.Errorf("Reset() did not clear Request: got %v", ctx.GetRequest())
	}
	if ctx.GetResponse() != nil {
		t.Errorf("Reset() did not clear Response: got %v", ctx.GetResponse())
	}
}

func TestAttributeContextRequestMetadata(t *testing.T) {
	req := &attribute_context.AttributeContext_Request{
		Id:     "req-meta",
		Method: "POST",
		Path:   "/api/v2/orders",
		Host:   "order.example.com",
		Headers: map[string]string{
			"content-type": "application/json",
			"x-api-key":    "test-key",
		},
		Auth: &attribute_context.AttributeContext_Auth{
			Principal: "service-account@example.com",
			Audiences: []string{"order.example.com"},
		},
	}

	if got := req.GetMethod(); got != "POST" {
		t.Errorf("GetMethod() = %q, want %q", got, "POST")
	}
	if got := req.GetPath(); got != "/api/v2/orders" {
		t.Errorf("GetPath() = %q, want %q", got, "/api/v2/orders")
	}
	if got := req.GetHost(); got != "order.example.com" {
		t.Errorf("GetHost() = %q, want %q", got, "order.example.com")
	}
	if got := req.GetHeaders()["content-type"]; got != "application/json" {
		t.Errorf("Headers[content-type] = %q, want application/json", got)
	}
	if got := req.GetAuth().GetPrincipal(); got != "service-account@example.com" {
		t.Errorf("GetAuth().GetPrincipal() = %q, want service-account@example.com", got)
	}
}

原始种子者

匿名