CodeSampleX

示例

google.golang.org/genproto/googleapis/api v0.0.0-20241007155032-5fefd90f89a9: annotations.E_Http

已验证示例 — golang google.golang.org/genproto/googleapis/api v0.0.0-20241007155032-5fefd90f89a9: annotations.E_Http. contract 在 go 1.26 · linux alpine/x64 …

sha256:7d1b51d2caea12002e4aa10880e9c12d495020818b67d930abab912d5a10dab7

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
2
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 go

验证运行环境

环境 契约 阶段 运行日期
go 1.26 · linux alpine/x64 · docker ed25519:c1973797be207ac4 FAIL compile:SKIPPED · contract:FAIL · load:SKIPPED · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f…
2026-08-30
go 1.26 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f…
2026-08-30

案例

HOW
目标
verify google.golang.org/genproto/googleapis/api/annotations.E_Http in pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20241007155032-5fefd90f89a9
包
符号
  • google.golang.org/genproto/googleapis/api/annotations.E_Http
创建时间
2026-08-30T16:56:15Z

契约

  1. annotations.E_Http extension descriptor targets google.protobuf.MethodOptions with extension number 72295728
  2. proto.HasExtension returns true when annotations.E_Http is set on MethodOptions and false when unset
  3. proto.GetExtension retrieves configured HttpRule with GET path pattern from MethodOptions
  4. proto.GetExtension retrieves configured HttpRule with POST path, body selector and additional bindings
  5. proto.ClearExtension removes annotations.E_Http from MethodOptions

文件

  • 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/api/annotations.E_Http in pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20241007155032-5fefd90f89a9
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20241007155032-5fefd90f89a9
Demonstrate these symbols/APIs:
  - google.golang.org/genproto/googleapis/api/annotations.E_Http

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:9ba13af220039cb99576ac2d98ce3d714375ba64ae6ca7de7ef07cb32721bfa5","contract":["annotations.E_Http extension descriptor targets google.protobuf.MethodOptions with extension number 72295728","proto.HasExtension returns true when annotations.E_Http is set on MethodOptions and false when unset","proto.GetExtension retrieves configured HttpRule with GET path pattern from MethodOptions","proto.GetExtension retrieves configured HttpRule with POST path, body selector and additional bindings","proto.ClearExtension removes annotations.E_Http from MethodOptions"],"goal":"verify google.golang.org/genproto/googleapis/api/annotations.E_Http in pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20241007155032-5fefd90f89a9","kind":"HOW","packages":["pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20241007155032-5fefd90f89a9"],"schemaVersion":1,"symbols":["google.golang.org/genproto/googleapis/api/annotations.E_Http"]},"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/api@v0.0.0-20241007155032-5fefd90f89a9"],"schemaVersion":1,"subject":"pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20241007155032-5fefd90f89a9","symbols":["google.golang.org/genproto/googleapis/api/annotations.E_Http"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.22.0

require (
	google.golang.org/genproto/googleapis/api v0.0.0-20241007155032-5fefd90f89a9
	google.golang.org/protobuf v1.35.1
)
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/api v0.0.0-20241007155032-5fefd90f89a9 h1:T6rh4haD3GVYsgEfWExoCZA2o2FmbNyKpTuAxbEFPTg=
google.golang.org/genproto/googleapis/api v0.0.0-20241007155032-5fefd90f89a9/go.mod h1:wp2WsuBYj6j8wUdo3ToZsdxxixbvQNAHqVJrTgi5E5M=
google.golang.org/genproto/googleapis/api v0.0.0-20260825221802-da73d73af1c5 h1:izFU9hz7aeLI/Mi1J0991ae+xcwRLr7hTqWnB/9aIIU=
google.golang.org/genproto/googleapis/api v0.0.0-20260825221802-da73d73af1c5/go.mod h1:3LhxRw4YYkf+ylAfgaY9JlVLFKhokkCV8duhLLe7+t0=
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=
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/api/annotations"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/descriptorpb"
)

// AttachHttpRule attaches a google.api.http rule to method options via E_Http extension.
func AttachHttpRule(opts *descriptorpb.MethodOptions, rule *annotations.HttpRule) {
	proto.SetExtension(opts, annotations.E_Http, rule)
}

// ExtractHttpRule retrieves a google.api.http rule from method options if present.
func ExtractHttpRule(opts *descriptorpb.MethodOptions) (*annotations.HttpRule, bool) {
	if !proto.HasExtension(opts, annotations.E_Http) {
		return nil, false
	}
	ext := proto.GetExtension(opts, annotations.E_Http)
	rule, ok := ext.(*annotations.HttpRule)
	return rule, ok
}

func main() {
	opts := &descriptorpb.MethodOptions{}

	rule := &annotations.HttpRule{
		Pattern: &annotations.HttpRule_Get{
			Get: "/v1/messages/{message_id}",
		},
		Body: "",
	}

	AttachHttpRule(opts, rule)

	extracted, ok := ExtractHttpRule(opts)
	if !ok || extracted == nil {
		fmt.Println("Failed to extract HTTP rule")
		return
	}

	fmt.Printf("HTTP Method GET Path: %s\n", extracted.GetGet())
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify google.golang.org/genproto/googleapis/api/annotations.E_Http in pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20241007155032-5fefd90f89a9",
  "kind": "HOW",
  "packages": [
    "pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20241007155032-5fefd90f89a9"
  ],
  "symbols": [
    "google.golang.org/genproto/googleapis/api/annotations.E_Http"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"

	"google.golang.org/genproto/googleapis/api/annotations"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/descriptorpb"
)

func main() {
	// Assertion 1: annotations.E_Http extension descriptor targets google.protobuf.MethodOptions
	extDesc := annotations.E_Http.TypeDescriptor()
	if extDesc == nil {
		fmt.Fprintf(os.Stderr, "FAIL: annotations.E_Http TypeDescriptor is nil\n")
		os.Exit(1)
	}
	extTarget := string(extDesc.ContainingMessage().FullName())
	if extTarget != "google.protobuf.MethodOptions" {
		fmt.Fprintf(os.Stderr, "FAIL: expected extension target google.protobuf.MethodOptions, got %s\n", extTarget)
		os.Exit(1)
	}
	extNumber := int(extDesc.Number())
	if extNumber != 72295728 {
		fmt.Fprintf(os.Stderr, "FAIL: expected extension number 72295728, got %d\n", extNumber)
		os.Exit(1)
	}

	// Assertion 2: proto.HasExtension returns true when annotations.E_Http is set on MethodOptions and false when unset
	emptyOpts := &descriptorpb.MethodOptions{}
	if proto.HasExtension(emptyOpts, annotations.E_Http) {
		fmt.Fprintf(os.Stderr, "FAIL: HasExtension returned true for empty MethodOptions\n")
		os.Exit(1)
	}

	optsWithExt := &descriptorpb.MethodOptions{}
	ruleGet := &annotations.HttpRule{
		Pattern: &annotations.HttpRule_Get{
			Get: "/v1/books/{book_id}",
		},
	}
	proto.SetExtension(optsWithExt, annotations.E_Http, ruleGet)
	if !proto.HasExtension(optsWithExt, annotations.E_Http) {
		fmt.Fprintf(os.Stderr, "FAIL: HasExtension returned false after SetExtension\n")
		os.Exit(1)
	}

	// Assertion 3: proto.GetExtension retrieves configured HttpRule with GET path pattern
	extVal := proto.GetExtension(optsWithExt, annotations.E_Http)
	retrievedRule, ok := extVal.(*annotations.HttpRule)
	if !ok || retrievedRule == nil {
		fmt.Fprintf(os.Stderr, "FAIL: GetExtension did not return *annotations.HttpRule\n")
		os.Exit(1)
	}
	if retrievedRule.GetGet() != "/v1/books/{book_id}" {
		fmt.Fprintf(os.Stderr, "FAIL: expected GET path /v1/books/{book_id}, got %s\n", retrievedRule.GetGet())
		os.Exit(1)
	}

	// Assertion 4: proto.GetExtension retrieves configured HttpRule with POST path, body selector and additional bindings
	optsComplex := &descriptorpb.MethodOptions{}
	rulePost := &annotations.HttpRule{
		Pattern: &annotations.HttpRule_Post{
			Post: "/v1/books",
		},
		Body: "*",
		AdditionalBindings: []*annotations.HttpRule{
			{
				Pattern: &annotations.HttpRule_Put{
					Put: "/v1/books/{book_id}",
				},
				Body: "book",
			},
		},
	}
	proto.SetExtension(optsComplex, annotations.E_Http, rulePost)
	complexVal := proto.GetExtension(optsComplex, annotations.E_Http)
	complexRule, ok := complexVal.(*annotations.HttpRule)
	if !ok || complexRule == nil {
		fmt.Fprintf(os.Stderr, "FAIL: failed to retrieve complex HttpRule\n")
		os.Exit(1)
	}
	if complexRule.GetPost() != "/v1/books" || complexRule.GetBody() != "*" {
		fmt.Fprintf(os.Stderr, "FAIL: mismatch in POST path or body\n")
		os.Exit(1)
	}
	if len(complexRule.GetAdditionalBindings()) != 1 || complexRule.GetAdditionalBindings()[0].GetPut() != "/v1/books/{book_id}" {
		fmt.Fprintf(os.Stderr, "FAIL: mismatch in AdditionalBindings\n")
		os.Exit(1)
	}

	// Assertion 5: proto.ClearExtension removes annotations.E_Http from MethodOptions
	proto.ClearExtension(optsWithExt, annotations.E_Http)
	if proto.HasExtension(optsWithExt, annotations.E_Http) {
		fmt.Fprintf(os.Stderr, "FAIL: HasExtension returned true after ClearExtension\n")
		os.Exit(1)
	}

	fmt.Println("PASS: google.golang.org/genproto/googleapis/api/annotations.E_Http contract verified")
}

原始种子者

匿名