CodeSampleX

Exemple

google.golang.org/protobuf v1.32.0: structpb.ListValue.AsSlice

Échantillon vérifié pour golang google.golang.org/protobuf v1.32.0: structpb.ListValue.AsSlice. Le contrat s'est exécuté sur go 1.26 · linux debian/x64 …

sha256:0c8bf0d6d0c4843a7bc30b92394cf8375636fe79ea07518385c87bd409d383e7

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é go linux 24 · ubuntu · glibc 2.39 x64 go go go

Environnements des exécutions de vérification

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

Cas

HOW
Objectif
verify google.golang.org/protobuf/types/known/structpb.ListValue.AsSlice in pkg:golang/google.golang.org/protobuf@v1.32.0
Paquets
Symboles
  • google.golang.org/protobuf/types/known/structpb.ListValue.AsSlice
Environnement
go
Créé
2026-09-01T06:12:52Z

Contrat

  1. structpb.ListValue.AsSlice converts a ListValue containing primitives (strings, booleans, float64 numbers, nil) into a standard Go []interface{} slice.
  2. structpb.ListValue.AsSlice recursively unpacks nested Struct and ListValue elements into map[string]interface{} and []interface{} structures.
  3. Calling structpb.ListValue.AsSlice on a nil *structpb.ListValue receiver safely returns an empty non-nil []interface{} slice.
  4. Calling structpb.ListValue.AsSlice on an empty ListValue returns an empty non-nil []interface{} slice.

Fichiers

  • NOTES.md
  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • listvalue_test.go
  • spec.json

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

Code source

NOTES.md
# Verification of google.golang.org/protobuf/types/known/structpb.ListValue.AsSlice

This sample tests and verifies conversion semantics of `*structpb.ListValue` to standard Go slices via `ListValue.AsSlice()` in `pkg:golang/google.golang.org/protobuf@v1.32.0`.

## Verified Behaviors

1. **Primitive Slice Conversion (`ListValue.AsSlice`)**:
   - `structpb.NewList` creates a `*structpb.ListValue` from primitive Go types (string, bool, numeric types converted to float64, and nil).
   - Calling `AsSlice()` returns a `[]interface{}` with the unpacked primitive values.

2. **Recursive Unpacking of Nested Structures**:
   - For elements that are nested `*structpb.Struct` or `*structpb.ListValue`, `AsSlice()` recursively unpacks them into `map[string]interface{}` and `[]interface{}` structures.

3. **Nil Receiver Safety**:
   - Calling `AsSlice()` on a `nil` `*structpb.ListValue` receiver safely returns an empty non-nil `[]interface{}{}` without panicking.

4. **Empty List Representation**:
   - Calling `AsSlice()` on an empty `*structpb.ListValue` returns an empty non-nil `[]interface{}{}`.
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/structpb.ListValue.AsSlice in pkg:golang/google.golang.org/protobuf@v1.32.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/google.golang.org/protobuf@v1.32.0
Demonstrate these symbols/APIs:
  - google.golang.org/protobuf/types/known/structpb.ListValue.AsSlice
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

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:051ac490ecb5eb6d7e2245522975e05be9d59184f8a442cb4588391d0515ac07","contract":["structpb.ListValue.AsSlice converts a ListValue containing primitives (strings, booleans, float64 numbers, nil) into a standard Go []interface{} slice.","structpb.ListValue.AsSlice recursively unpacks nested Struct and ListValue elements into map[string]interface{} and []interface{} structures.","Calling structpb.ListValue.AsSlice on a nil *structpb.ListValue receiver safely returns an empty non-nil []interface{} slice.","Calling structpb.ListValue.AsSlice on an empty ListValue returns an empty non-nil []interface{} slice."],"goal":"verify google.golang.org/protobuf/types/known/structpb.ListValue.AsSlice in pkg:golang/google.golang.org/protobuf@v1.32.0","kind":"HOW","packages":["pkg:golang/google.golang.org/protobuf@v1.32.0"],"schemaVersion":1,"symbols":["google.golang.org/protobuf/types/known/structpb.ListValue.AsSlice"]},"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.32.0"],"schemaVersion":1,"subject":"pkg:golang/google.golang.org/protobuf@v1.32.0","symbols":["google.golang.org/protobuf/types/known/structpb.ListValue.AsSlice"],"verifierAdapter":"golang@1"}
go.mod
module example.com/listvaluesample

go 1.26.6

require google.golang.org/protobuf v1.32.0
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.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
listvalue_test.go
package listvaluesample_test

import (
	"reflect"
	"testing"

	"google.golang.org/protobuf/types/known/structpb"
)

// TestListValueAsSlicePrimitives verifies that ListValue.AsSlice converts protobuf ListValue
// containing primitive values (strings, bools, numbers, and null) into a []interface{} slice.
func TestListValueAsSlicePrimitives(t *testing.T) {
	inputSlice := []interface{}{
		"hello",
		true,
		float64(42),
		float64(3.14159),
		nil,
	}

	listVal, err := structpb.NewList(inputSlice)
	if err != nil {
		t.Fatalf("structpb.NewList failed: %v", err)
	}

	gotSlice := listVal.AsSlice()
	if !reflect.DeepEqual(inputSlice, gotSlice) {
		t.Fatalf("AsSlice() mismatch for primitives:\nwant: %#v\ngot:  %#v", inputSlice, gotSlice)
	}
}

// TestListValueAsSliceNestedStructures verifies that ListValue.AsSlice recursively converts
// nested structpb.Struct and structpb.ListValue into map[string]interface{} and []interface{}.
func TestListValueAsSliceNestedStructures(t *testing.T) {
	nestedData := []interface{}{
		map[string]interface{}{
			"id":   "item-1",
			"tags": []interface{}{"alpha", "beta"},
		},
		[]interface{}{
			float64(1),
			float64(2),
			map[string]interface{}{"active": true},
		},
	}

	listVal, err := structpb.NewList(nestedData)
	if err != nil {
		t.Fatalf("structpb.NewList failed for nested structures: %v", err)
	}

	gotSlice := listVal.AsSlice()
	if !reflect.DeepEqual(nestedData, gotSlice) {
		t.Fatalf("AsSlice() mismatch for nested structures:\nwant: %#v\ngot:  %#v", nestedData, gotSlice)
	}
}

// TestListValueAsSliceNilReceiver verifies that calling AsSlice on a nil *structpb.ListValue receiver
// safely returns an empty non-nil []interface{} slice without panicking.
func TestListValueAsSliceNilReceiver(t *testing.T) {
	var nilList *structpb.ListValue
	gotSlice := nilList.AsSlice()

	if gotSlice == nil {
		t.Fatalf("expected non-nil slice when calling AsSlice on nil receiver, got nil")
	}
	if len(gotSlice) != 0 {
		t.Fatalf("expected empty slice when calling AsSlice on nil receiver, got len %d", len(gotSlice))
	}
}

// TestListValueAsSliceEmptyList verifies that calling AsSlice on an empty ListValue returns an empty slice.
func TestListValueAsSliceEmptyList(t *testing.T) {
	emptyList, err := structpb.NewList([]interface{}{})
	if err != nil {
		t.Fatalf("structpb.NewList([]interface{}{}) failed: %v", err)
	}

	gotSlice := emptyList.AsSlice()
	if gotSlice == nil {
		t.Fatalf("expected non-nil slice for empty ListValue")
	}
	if len(gotSlice) != 0 {
		t.Fatalf("expected len 0, got %d", len(gotSlice))
	}

	// Also verify with NewList(nil)
	nilParamVal, err := structpb.NewList(nil)
	if err != nil {
		t.Fatalf("structpb.NewList(nil) failed: %v", err)
	}
	if got := nilParamVal.AsSlice(); len(got) != 0 {
		t.Fatalf("expected len 0 for NewList(nil), got %d", len(got))
	}
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify google.golang.org/protobuf/types/known/structpb.ListValue.AsSlice in pkg:golang/google.golang.org/protobuf@v1.32.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/google.golang.org/protobuf@v1.32.0"
  ],
  "symbols": [
    "google.golang.org/protobuf/types/known/structpb.ListValue.AsSlice"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}

Seeder d'origine

anonyme