CodeSampleX

Exemple

github.com/hashicorp/hcl v1.0.0: Parse

Échantillon vérifié pour golang github.com/hashicorp/hcl v1.0.0: Parse. Le contrat s'est exécuté sur go 1.26 · linux debian/x64 · docker et a réussi.

sha256:bf12f12b3610a9217c6644335249a48c25ba1d9c7fb91522ed54b0cd26a56bd9

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 debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-10-27

Cas

HOW
Objectif
verify hcl.Parse in pkg:golang/github.com/hashicorp/hcl@v1.0.0
Paquets
Symboles
  • hcl.Parse
Créé
2026-10-27T17:41:55Z

Contrat

  1. hcl.Parse parses native HCL text into an *ast.File with an *ast.ObjectList root node that hcl.DecodeObject decodes into a Go struct
  2. hcl.Parse auto-detects JSON text and produces an equivalent *ast.File that decodes to the identical struct value
  3. hcl.Parse returns a non-nil error on malformed HCL or malformed JSON syntax

Fichiers

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • 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 hcl.Parse in pkg:golang/github.com/hashicorp/hcl@v1.0.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/hashicorp/hcl@v1.0.0
Demonstrate these symbols/APIs:
  - hcl.Parse

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:4c6fbeafe1cc515b056ffe56a53ac9a3d2d1e409e036d188dc237a791a7b0c9e","contract":["hcl.Parse parses native HCL text into an *ast.File with an *ast.ObjectList root node that hcl.DecodeObject decodes into a Go struct","hcl.Parse auto-detects JSON text and produces an equivalent *ast.File that decodes to the identical struct value","hcl.Parse returns a non-nil error on malformed HCL or malformed JSON syntax"],"goal":"verify hcl.Parse in pkg:golang/github.com/hashicorp/hcl@v1.0.0","kind":"HOW","packages":["pkg:golang/github.com/hashicorp/hcl@v1.0.0"],"schemaVersion":1,"symbols":["hcl.Parse"]},"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/github.com/hashicorp/hcl@v1.0.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/hashicorp/hcl@v1.0.0","symbols":["hcl.Parse"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.21

require github.com/hashicorp/hcl v1.0.0
go.sum
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify hcl.Parse in pkg:golang/github.com/hashicorp/hcl@v1.0.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/hashicorp/hcl@v1.0.0"
  ],
  "symbols": [
    "hcl.Parse"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"
	"reflect"

	"github.com/hashicorp/hcl"
	"github.com/hashicorp/hcl/hcl/ast"
)

type ServiceSpec struct {
	Address  string   `hcl:"address"`
	Port     int      `hcl:"port"`
	Active   bool     `hcl:"active"`
	Features []string `hcl:"features"`
}

type RootConfig struct {
	Region  string                 `hcl:"region"`
	Service map[string]ServiceSpec `hcl:"service"`
}

func main() {
	hclText := `
region = "us-east"

service "gateway" {
  address  = "localhost"
  port     = 9000
  active   = true
  features = ["tls", "metrics"]
}
`

	// 1. hcl.Parse parses HCL native syntax into a non-nil *ast.File with an *ast.ObjectList root node.
	hclAST, err := hcl.Parse(hclText)
	if err != nil {
		fmt.Fprintf(os.Stderr, "hcl.Parse HCL failed: %v\n", err)
		os.Exit(1)
	}
	if hclAST == nil || hclAST.Node == nil {
		fmt.Fprintf(os.Stderr, "expected non-nil AST file and root node\n")
		os.Exit(1)
	}
	objList, ok := hclAST.Node.(*ast.ObjectList)
	if !ok || len(objList.Items) != 2 {
		fmt.Fprintf(os.Stderr, "expected *ast.ObjectList with 2 items, got %T (%v)\n", hclAST.Node, hclAST.Node)
		os.Exit(1)
	}

	var fromHCL RootConfig
	if err := hcl.DecodeObject(&fromHCL, hclAST); err != nil {
		fmt.Fprintf(os.Stderr, "hcl.DecodeObject from HCL AST failed: %v\n", err)
		os.Exit(1)
	}
	gw, ok := fromHCL.Service["gateway"]
	if !ok || fromHCL.Region != "us-east" || gw.Address != "localhost" || gw.Port != 9000 || !gw.Active || !reflect.DeepEqual(gw.Features, []string{"tls", "metrics"}) {
		fmt.Fprintf(os.Stderr, "unexpected decoded HCL struct: %+v\n", fromHCL)
		os.Exit(1)
	}

	// 2. hcl.Parse auto-detects JSON input (leading '{') and produces an equivalent *ast.File.
	jsonText := `{
  "region": "us-east",
  "service": {
    "gateway": {
      "address": "localhost",
      "port": 9000,
      "active": true,
      "features": ["tls", "metrics"]
    }
  }
}`
	jsonAST, err := hcl.Parse(jsonText)
	if err != nil {
		fmt.Fprintf(os.Stderr, "hcl.Parse JSON failed: %v\n", err)
		os.Exit(1)
	}
	var fromJSON RootConfig
	if err := hcl.DecodeObject(&fromJSON, jsonAST); err != nil {
		fmt.Fprintf(os.Stderr, "hcl.DecodeObject from JSON AST failed: %v\n", err)
		os.Exit(1)
	}
	if !reflect.DeepEqual(fromHCL, fromJSON) {
		fmt.Fprintf(os.Stderr, "HCL and JSON AST decode mismatch: %+v vs %+v\n", fromHCL, fromJSON)
		os.Exit(1)
	}

	// 3. hcl.Parse returns a non-nil error for malformed HCL or malformed JSON input.
	if _, err := hcl.Parse("region = \"unclosed"); err == nil {
		fmt.Fprintf(os.Stderr, "expected error from hcl.Parse on malformed HCL\n")
		os.Exit(1)
	}
	if _, err := hcl.Parse("{invalid json"); err == nil {
		fmt.Fprintf(os.Stderr, "expected error from hcl.Parse on malformed JSON\n")
		os.Exit(1)
	}
}

Seeder d'origine

anonyme