CodeSampleX

示例

google.golang.org/genproto/googleapis/api v0.0.0-20260406210006-6f92a3bedf2d: httpbody.HttpBody

已验证示例 — golang google.golang.org/genproto/googleapis/api v0.0.0-20260406210006-6f92a3bedf2d: httpbody.HttpBody. contract 在 go 1.26 · linux alpine/x64 …

sha256:78c89b104adb271bce262e442609e9a8f173de6b4ee4f9d53cb3a4a69c9c02c9

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

案例

HOW
目标
verify google.golang.org/genproto/googleapis/api/httpbody.HttpBody in pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20260406210006-6f92a3bedf2d
包
符号
  • google.golang.org/genproto/googleapis/api/httpbody.HttpBody
创建时间
2026-08-28T10:17:38Z

契约

  1. httpbody.HttpBody encapsulates raw payload bytes with HTTP content-type header string
  2. httpbody.HttpBody serializes and deserializes accurately with protobuf wire format
  3. httpbody.HttpBody stores arbitrary application metadata extensions as protobuf Any messages
  4. httpbody.HttpBody provides safe getters with default values for empty and nil instances

文件

  • 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/httpbody.HttpBody in pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20260406210006-6f92a3bedf2d
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20260406210006-6f92a3bedf2d
Demonstrate these symbols/APIs:
  - google.golang.org/genproto/googleapis/api/httpbody.HttpBody

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:73e31a04656fa30ab22ee59fa0149a0d09bfa2b46cccf834ed85f470e0a2b5a6","contract":["httpbody.HttpBody encapsulates raw payload bytes with HTTP content-type header string","httpbody.HttpBody serializes and deserializes accurately with protobuf wire format","httpbody.HttpBody stores arbitrary application metadata extensions as protobuf Any messages","httpbody.HttpBody provides safe getters with default values for empty and nil instances"],"goal":"verify google.golang.org/genproto/googleapis/api/httpbody.HttpBody in pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20260406210006-6f92a3bedf2d","kind":"HOW","packages":["pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20260406210006-6f92a3bedf2d"],"schemaVersion":1,"symbols":["google.golang.org/genproto/googleapis/api/httpbody.HttpBody"]},"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-20260406210006-6f92a3bedf2d"],"schemaVersion":1,"subject":"pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20260406210006-6f92a3bedf2d","symbols":["google.golang.org/genproto/googleapis/api/httpbody.HttpBody"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.25.0

require (
	google.golang.org/genproto/googleapis/api v0.0.0-20260406210006-6f92a3bedf2d
	google.golang.org/protobuf v1.36.11
)
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-20260406210006-6f92a3bedf2d h1:/aDRtSZJjyLQzm75d+a1wOJaqyKBMvIAfeQmoa3ORiI=
google.golang.org/genproto/googleapis/api v0.0.0-20260406210006-6f92a3bedf2d/go.mod h1:etfGUgejTiadZAUaEP14NP97xi1RGeawqkjDARA/UOs=
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
main.go
package main

import (
	"bytes"
	"fmt"
	"log"

	"google.golang.org/genproto/googleapis/api/httpbody"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/known/anypb"
)

func main() {
	// Create an HttpBody representing an HTTP payload with metadata
	metaBody := &httpbody.HttpBody{
		ContentType: "text/plain",
		Data:        []byte("metadata payload"),
	}
	metaAny, err := anypb.New(metaBody)
	if err != nil {
		log.Fatalf("failed to create anypb extension: %v", err)
	}

	body := &httpbody.HttpBody{
		ContentType: "application/json",
		Data:        []byte(`{"status":"success","items":[1,2,3]}`),
		Extensions:  []*anypb.Any{metaAny},
	}

	data, err := proto.Marshal(body)
	if err != nil {
		log.Fatalf("failed to marshal HttpBody: %v", err)
	}

	var decoded httpbody.HttpBody
	if err := proto.Unmarshal(data, &decoded); err != nil {
		log.Fatalf("failed to unmarshal HttpBody: %v", err)
	}

	fmt.Printf("Content-Type: %s\n", decoded.GetContentType())
	fmt.Printf("Data: %s\n", string(decoded.GetData()))
	fmt.Printf("Extensions count: %d\n", len(decoded.GetExtensions()))
	if !bytes.Equal(decoded.GetData(), body.GetData()) {
		log.Fatalf("decoded data mismatch")
	}
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify google.golang.org/genproto/googleapis/api/httpbody.HttpBody in pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20260406210006-6f92a3bedf2d",
  "kind": "HOW",
  "packages": [
    "pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20260406210006-6f92a3bedf2d"
  ],
  "symbols": [
    "google.golang.org/genproto/googleapis/api/httpbody.HttpBody"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"fmt"
	"os"

	"google.golang.org/genproto/googleapis/api/httpbody"
	"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. httpbody.HttpBody encapsulates raw payload bytes with HTTP content-type header string
	payload := []byte(`{"name":"test-resource","count":42}`)
	body := &httpbody.HttpBody{
		ContentType: "application/json",
		Data:        payload,
	}
	assert(body.GetContentType() == "application/json", "ContentType must match initial value")
	assert(bytes.Equal(body.GetData(), payload), "Data must match payload bytes")
	assert(len(body.GetExtensions()) == 0, "Extensions must be empty by default")

	// 2. httpbody.HttpBody serializes and deserializes accurately with protobuf wire format
	wireData, err := proto.Marshal(body)
	assert(err == nil, "proto.Marshal(body) must succeed")
	assert(len(wireData) > 0, "marshaled bytes must not be empty")

	var decoded httpbody.HttpBody
	err = proto.Unmarshal(wireData, &decoded)
	assert(err == nil, "proto.Unmarshal(wireData) must succeed")
	assert(decoded.GetContentType() == body.GetContentType(), "decoded ContentType must match")
	assert(bytes.Equal(decoded.GetData(), body.GetData()), "decoded Data must match original bytes")
	assert(len(decoded.GetExtensions()) == 0, "decoded Extensions must remain empty")

	// 3. httpbody.HttpBody stores arbitrary application metadata extensions as protobuf Any messages
	nested := &httpbody.HttpBody{
		ContentType: "text/plain",
		Data:        []byte("extension-data"),
	}
	extAny, err := anypb.New(nested)
	assert(err == nil, "anypb.New(nested) must succeed")

	bodyWithExt := &httpbody.HttpBody{
		ContentType: "application/octet-stream",
		Data:        []byte{0xDE, 0xAD, 0xBE, 0xEF},
		Extensions:  []*anypb.Any{extAny},
	}
	assert(len(bodyWithExt.GetExtensions()) == 1, "Extensions length must be 1")

	extWire, err := proto.Marshal(bodyWithExt)
	assert(err == nil, "proto.Marshal(bodyWithExt) must succeed")

	var decodedWithExt httpbody.HttpBody
	err = proto.Unmarshal(extWire, &decodedWithExt)
	assert(err == nil, "proto.Unmarshal(extWire) must succeed")
	assert(len(decodedWithExt.GetExtensions()) == 1, "decoded Extensions length must be 1")

	var unmarshaledExt httpbody.HttpBody
	err = anypb.UnmarshalTo(decodedWithExt.GetExtensions()[0], &unmarshaledExt, proto.UnmarshalOptions{})
	assert(err == nil, "anypb.UnmarshalTo must succeed for decoded extension")
	assert(unmarshaledExt.GetContentType() == "text/plain", "unmarshaled extension ContentType must match")
	assert(bytes.Equal(unmarshaledExt.GetData(), []byte("extension-data")), "unmarshaled extension Data must match")

	// 4. httpbody.HttpBody provides safe getters with default values for empty and nil instances
	emptyBody := &httpbody.HttpBody{}
	assert(emptyBody.GetContentType() == "", "emptyBody.GetContentType() must return empty string")
	assert(emptyBody.GetData() == nil, "emptyBody.GetData() must return nil")
	assert(emptyBody.GetExtensions() == nil, "emptyBody.GetExtensions() must return nil")

	var nilBody *httpbody.HttpBody
	assert(nilBody.GetContentType() == "", "nilBody.GetContentType() must return empty string")
	assert(nilBody.GetData() == nil, "nilBody.GetData() must return nil")
	assert(nilBody.GetExtensions() == nil, "nilBody.GetExtensions() must return nil")

	fmt.Println("All contract assertions for httpbody.HttpBody passed successfully.")
}

原始种子者

匿名