CodeSampleX

Beispiel

google.golang.org/protobuf v1.35.1: anypb.New

Verifiziertes Beispiel für golang google.golang.org/protobuf v1.35.1: anypb.New. Der Vertrag lief auf go 1.26 · linux alpine/x64 · docker und bestand.

sha256:d2fa92af10719d962dca0d397c6e438ec222fc879ff080b0bfc9f5b539a2834e

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
1
Signaturschlüssel, die es gebaut haben
1
Deklarierte Umgebung go linux 24 · ubuntu · glibc 2.39 x64 go go go

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
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

Fall

HOW
Ziel
verify google.golang.org/protobuf/types/known/anypb.New in pkg:golang/google.golang.org/protobuf@v1.35.1
Pakete
Symbole
  • google.golang.org/protobuf/types/known/anypb.New
Umgebung
go
Erstellt
2026-08-27T10:33:35Z

Contract

  1. anypb.New packs a proto.Message into an Any message with canonical type URL and marshaled wire format payload.
  2. anypb.New returns an error when passed a nil proto.Message.
  3. Any.UnmarshalTo unmarshals the Any payload into a destination proto.Message.
  4. Any.UnmarshalNew dynamically resolves and unmarshals the Any payload into a new proto.Message instance.
  5. Any.MessageIs and Any.MessageName correctly identify the encapsulated message type.

Dateien

  • PROMPT.md
  • anypb_test.go
  • csx.json
  • go.mod
  • go.sum
  • spec.json

Quellartefakt herunterladen (tar.gz)

Quelltext

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/protobuf/types/known/anypb.New in pkg:golang/google.golang.org/protobuf@v1.35.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/google.golang.org/protobuf@v1.35.1
Demonstrate these symbols/APIs:
  - google.golang.org/protobuf/types/known/anypb.New

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.
anypb_test.go
package anypbdemo_test

import (
	"strings"
	"testing"

	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/known/anypb"
	"google.golang.org/protobuf/types/known/timestamppb"
	"google.golang.org/protobuf/types/known/wrapperspb"
)

func TestAnyNewWithNil(t *testing.T) {
	anyMsg, err := anypb.New(nil)
	if err == nil {
		t.Fatal("expected error when packing nil message, got nil")
	}
	if !strings.Contains(err.Error(), "invalid nil source message") {
		t.Errorf("expected 'invalid nil source message' error, got: %v", err)
	}
	if anyMsg != nil {
		t.Fatalf("expected nil Any when packing nil message, got: %v", anyMsg)
	}
}

func TestAnyNewAndUnmarshal(t *testing.T) {
	orig := wrapperspb.String("csx-verified-payload")

	anyMsg, err := anypb.New(orig)
	if err != nil {
		t.Fatalf("anypb.New failed: %v", err)
	}
	if anyMsg == nil {
		t.Fatal("expected non-nil Any message")
	}

	expectedTypeURL := "type.googleapis.com/google.protobuf.StringValue"
	if anyMsg.GetTypeUrl() != expectedTypeURL {
		t.Errorf("expected TypeUrl %q, got %q", expectedTypeURL, anyMsg.GetTypeUrl())
	}

	if !anyMsg.MessageIs(&wrapperspb.StringValue{}) {
		t.Errorf("MessageIs(&wrapperspb.StringValue{}) should be true")
	}
	if anyMsg.MessageIs(&timestamppb.Timestamp{}) {
		t.Errorf("MessageIs(&timestamppb.Timestamp{}) should be false")
	}

	var dst wrapperspb.StringValue
	if err := anyMsg.UnmarshalTo(&dst); err != nil {
		t.Fatalf("UnmarshalTo failed: %v", err)
	}
	if !proto.Equal(orig, &dst) {
		t.Errorf("expected %v, got %v", orig, &dst)
	}
	if dst.GetValue() != "csx-verified-payload" {
		t.Errorf("expected unmarshaled value %q, got %q", "csx-verified-payload", dst.GetValue())
	}

	unmarshaledNew, err := anyMsg.UnmarshalNew()
	if err != nil {
		t.Fatalf("UnmarshalNew failed: %v", err)
	}
	strVal, ok := unmarshaledNew.(*wrapperspb.StringValue)
	if !ok {
		t.Fatalf("expected *wrapperspb.StringValue, got %T", unmarshaledNew)
	}
	if !proto.Equal(orig, strVal) {
		t.Errorf("expected %v, got %v", orig, strVal)
	}
}

func TestAnyMarshalFrom(t *testing.T) {
	orig := wrapperspb.Int32(12345)

	var anyMsg anypb.Any
	opts := proto.MarshalOptions{
		Deterministic: true,
	}
	if err := anypb.MarshalFrom(&anyMsg, orig, opts); err != nil {
		t.Fatalf("MarshalFrom failed: %v", err)
	}

	var dst wrapperspb.Int32Value
	if err := anyMsg.UnmarshalTo(&dst); err != nil {
		t.Fatalf("UnmarshalTo failed: %v", err)
	}
	if dst.GetValue() != 12345 {
		t.Errorf("expected 12345, got %d", dst.GetValue())
	}
}

func TestAnyMessageName(t *testing.T) {
	orig := timestamppb.Now()
	anyMsg, err := anypb.New(orig)
	if err != nil {
		t.Fatalf("anypb.New failed: %v", err)
	}

	name := anyMsg.MessageName()
	if name != "google.protobuf.Timestamp" {
		t.Errorf("expected MessageName 'google.protobuf.Timestamp', got %q", name)
	}
}
csx.json
{"case":{"caseId":"case:sha256:00cde054eb1038a623f5f8352444276546fef04d83d5d66baa098a3391412ebb","contract":["anypb.New packs a proto.Message into an Any message with canonical type URL and marshaled wire format payload.","anypb.New returns an error when passed a nil proto.Message.","Any.UnmarshalTo unmarshals the Any payload into a destination proto.Message.","Any.UnmarshalNew dynamically resolves and unmarshals the Any payload into a new proto.Message instance.","Any.MessageIs and Any.MessageName correctly identify the encapsulated message type."],"goal":"verify google.golang.org/protobuf/types/known/anypb.New in pkg:golang/google.golang.org/protobuf@v1.35.1","kind":"HOW","packages":["pkg:golang/google.golang.org/protobuf@v1.35.1"],"schemaVersion":1,"symbols":["google.golang.org/protobuf/types/known/anypb.New"]},"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/protobuf@v1.35.1"],"schemaVersion":1,"subject":"pkg:golang/google.golang.org/protobuf@v1.35.1","symbols":["google.golang.org/protobuf/types/known/anypb.New"],"verifierAdapter":"golang@1"}
go.mod
module example.com/anypbdemo

go 1.22

require google.golang.org/protobuf v1.35.1
go.sum
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
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=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify google.golang.org/protobuf/types/known/anypb.New in pkg:golang/google.golang.org/protobuf@v1.35.1",
  "kind": "HOW",
  "packages": [
    "pkg:golang/google.golang.org/protobuf@v1.35.1"
  ],
  "symbols": [
    "google.golang.org/protobuf/types/known/anypb.New"
  ]
}

Ursprungs-Seeder

anonym