CodeSampleX

Exemple

google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9: code.Code_ALREADY_EXISTS

Échantillon vérifié pour golang google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9: code.Code_ALREADY_EXISTS. Le contrat s'est…

sha256:a946a2d3033f92defeee1b6656bd740779d70ecefc98f2c7126631cd7990d66d

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
1
Clés de signature qui l’ont compilé
1
Environnement déclaré linux 24 · ubuntu · glibc 2.39 x64 go

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
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

Cas

HOW
Objectif
verify google.golang.org/genproto/googleapis/rpc/code.Code_ALREADY_EXISTS in pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20241007155032-5fefd90f89a9
Paquets
Symboles
  • google.golang.org/genproto/googleapis/rpc/code.Code_ALREADY_EXISTS
Créé
2026-08-27T10:37:18Z

Contrat

  1. code.Code_ALREADY_EXISTS has numeric enum value 6 and string representation ALREADY_EXISTS
  2. code.Code enum mappings resolve Code_ALREADY_EXISTS in both directions
  3. status.Status creates and serializes an RPC status message with Code_ALREADY_EXISTS
  4. status.Status with Code_ALREADY_EXISTS packs and unpacks errdetails.ResourceInfo Any details

Fichiers

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract.go

Télécharger l’artefact source (tar.gz)

Code source

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/code.Code_ALREADY_EXISTS 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/code.Code_ALREADY_EXISTS

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:2d5dd5f70c413ecc0609e3bebc18e3b1ac2fd0edbbbe58e48543d63bf9480666","contract":["code.Code_ALREADY_EXISTS has numeric enum value 6 and string representation ALREADY_EXISTS","code.Code enum mappings resolve Code_ALREADY_EXISTS in both directions","status.Status creates and serializes an RPC status message with Code_ALREADY_EXISTS","status.Status with Code_ALREADY_EXISTS packs and unpacks errdetails.ResourceInfo Any details"],"goal":"verify google.golang.org/genproto/googleapis/rpc/code.Code_ALREADY_EXISTS 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/code.Code_ALREADY_EXISTS"]},"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/code.Code_ALREADY_EXISTS"],"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() {
	resourceInfo := &errdetails.ResourceInfo{
		ResourceType: "user_account",
		ResourceName: "users/johndoe",
		Description:  "User account with username 'johndoe' already exists",
	}
	anyResourceInfo, err := anypb.New(resourceInfo)
	if err != nil {
		log.Fatalf("failed to pack resource info: %v", err)
	}

	st := &status.Status{
		Code:    int32(code.Code_ALREADY_EXISTS),
		Message: "resource already exists",
		Details: []*anypb.Any{anyResourceInfo},
	}

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

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

	fmt.Printf("Status Code: %d (%s)\n", decoded.GetCode(), code.Code(decoded.GetCode()).String())
	fmt.Printf("Status Message: %s\n", decoded.GetMessage())
	fmt.Printf("Details Count: %d\n", len(decoded.GetDetails()))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify google.golang.org/genproto/googleapis/rpc/code.Code_ALREADY_EXISTS 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/code.Code_ALREADY_EXISTS"
  ]
}
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. code.Code_ALREADY_EXISTS has numeric enum value 6 and string representation ALREADY_EXISTS
	assert(int32(code.Code_ALREADY_EXISTS) == 6, "code.Code_ALREADY_EXISTS numeric value must be 6")
	assert(code.Code_ALREADY_EXISTS.String() == "ALREADY_EXISTS", "code.Code_ALREADY_EXISTS string value must be ALREADY_EXISTS")

	// 2. code.Code enum mappings resolve Code_ALREADY_EXISTS in both directions
	assert(code.Code_name[int32(code.Code_ALREADY_EXISTS)] == "ALREADY_EXISTS", "Code_name mapping for Code_ALREADY_EXISTS must be ALREADY_EXISTS")
	assert(code.Code_value["ALREADY_EXISTS"] == int32(code.Code_ALREADY_EXISTS), "Code_value mapping for ALREADY_EXISTS must match Code_ALREADY_EXISTS")

	// 3. status.Status creates and serializes an RPC status message with Code_ALREADY_EXISTS
	st := &status.Status{
		Code:    int32(code.Code_ALREADY_EXISTS),
		Message: "the resource already exists in database",
	}
	assert(st.GetCode() == int32(code.Code_ALREADY_EXISTS), "status code must equal Code_ALREADY_EXISTS")
	assert(st.GetMessage() == "the resource already exists in database", "status message must match")

	data, err := proto.Marshal(st)
	assert(err == nil, "proto.Marshal(st) must succeed")
	assert(len(data) > 0, "marshaled bytes must not be empty")

	var decoded status.Status
	err = proto.Unmarshal(data, &decoded)
	assert(err == nil, "proto.Unmarshal must succeed")
	assert(decoded.GetCode() == int32(code.Code_ALREADY_EXISTS), "decoded status code must equal Code_ALREADY_EXISTS")
	assert(decoded.GetMessage() == "the resource already exists in database", "decoded status message must match")

	// 4. status.Status with Code_ALREADY_EXISTS packs and unpacks errdetails.ResourceInfo Any details
	resourceInfo := &errdetails.ResourceInfo{
		ResourceType: "record",
		ResourceName: "records/1001",
		Description:  "entity with identifier 1001 already exists",
	}
	anyResource, err := anypb.New(resourceInfo)
	assert(err == nil, "anypb.New(resourceInfo) must succeed")

	st.Details = append(st.Details, anyResource)
	assert(len(st.GetDetails()) == 1, "details count must be 1")

	unpackedResource := &errdetails.ResourceInfo{}
	err = anypb.UnmarshalTo(st.GetDetails()[0], unpackedResource, proto.UnmarshalOptions{})
	assert(err == nil, "unmarshaling ResourceInfo Any detail must succeed")
	assert(unpackedResource.GetResourceType() == "record", "unpacked resource type must match")
	assert(unpackedResource.GetResourceName() == "records/1001", "unpacked resource name must match")
	assert(unpackedResource.GetDescription() == "entity with identifier 1001 already exists", "unpacked description must match")

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

Seeder d'origine

anonyme