CodeSampleX

示例

github.com/cncf/xds/go v0.0.0-20251210132809-ee656c7534f5: v3.ResourceLocator

已验证示例 — golang github.com/cncf/xds/go v0.0.0-20251210132809-ee656c7534f5: v3.ResourceLocator. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过.

sha256:9fde4f31cf98f9549e178e1603f698684f784a61892aa00b6cd89f2960bedf00

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

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 go

验证运行环境

环境 契约 阶段 运行日期
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-15

案例

HOW
目标
verify github.com/cncf/xds/go/xds/core/v3.ResourceLocator in pkg:golang/github.com/cncf/xds/go@v0.0.0-20251210132809-ee656c7534f5
包
符号
  • github.com/cncf/xds/go/xds/core/v3.ResourceLocator
创建时间
2026-10-15T01:36:14Z

契约

  1. ResourceLocator initializes with scheme, id, authority, resource type, exact context, and directives
  2. ResourceLocator getters safely return default values on nil and empty receivers without panic
  3. ResourceLocator validation passes for valid XDSTP, HTTP, and FILE schemes with non-empty resource type and nested alt directives
  4. ResourceLocator validation returns an error when resource type is empty or scheme is out of range
  5. ResourceLocator round-trips through Protobuf binary wire format preserving equality
  6. proto.Clone creates an independent deep copy of ResourceLocator

文件

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • spec.json
  • test/main.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 github.com/cncf/xds/go/xds/core/v3.ResourceLocator in pkg:golang/github.com/cncf/xds/go@v0.0.0-20251210132809-ee656c7534f5
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/cncf/xds/go@v0.0.0-20251210132809-ee656c7534f5
Demonstrate these symbols/APIs:
  - github.com/cncf/xds/go/xds/core/v3.ResourceLocator

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:5dc9dd00707e3d49b55da02a65b6a553e012b6a322b31000aac408f7680718a1","contract":["ResourceLocator initializes with scheme, id, authority, resource type, exact context, and directives","ResourceLocator getters safely return default values on nil and empty receivers without panic","ResourceLocator validation passes for valid XDSTP, HTTP, and FILE schemes with non-empty resource type and nested alt directives","ResourceLocator validation returns an error when resource type is empty or scheme is out of range","ResourceLocator round-trips through Protobuf binary wire format preserving equality","proto.Clone creates an independent deep copy of ResourceLocator"],"goal":"verify github.com/cncf/xds/go/xds/core/v3.ResourceLocator in pkg:golang/github.com/cncf/xds/go@v0.0.0-20251210132809-ee656c7534f5","kind":"HOW","packages":["pkg:golang/github.com/cncf/xds/go@v0.0.0-20251210132809-ee656c7534f5"],"schemaVersion":1,"symbols":["github.com/cncf/xds/go/xds/core/v3.ResourceLocator"]},"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/cncf/xds/go@v0.0.0-20251210132809-ee656c7534f5"],"schemaVersion":1,"subject":"pkg:golang/github.com/cncf/xds/go@v0.0.0-20251210132809-ee656c7534f5","symbols":["github.com/cncf/xds/go/xds/core/v3.ResourceLocator"],"verifierAdapter":"golang@1"}
go.mod
module example.com/resourcelocator

go 1.24

require (
	github.com/cncf/xds/go v0.0.0-20251210132809-ee656c7534f5
	google.golang.org/protobuf v1.36.10
)

require github.com/envoyproxy/protoc-gen-validate v1.2.1 // indirect
go.sum
github.com/cncf/xds/go v0.0.0-20251210132809-ee656c7534f5 h1:6xNmx7iTtyBRev0+D/Tv1FZd4SCg8axKApyNyRsAt/w=
github.com/cncf/xds/go v0.0.0-20251210132809-ee656c7534f5/go.mod h1:KdCmV+x/BuvyMxRnYBlmVaq4OLiKW6iRQfvC62cvdkI=
github.com/envoyproxy/protoc-gen-validate v1.2.1 h1:DEo3O99U8j4hBFwbJfrz9VtgcDfUKS7KJ7spH3d86P8=
github.com/envoyproxy/protoc-gen-validate v1.2.1/go.mod h1:d/C80l/jxXLdfEIhX1W2TmLfsJ31lvEjwamM4DxlWXU=
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/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE=
google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/cncf/xds/go/xds/core/v3.ResourceLocator in pkg:golang/github.com/cncf/xds/go@v0.0.0-20251210132809-ee656c7534f5",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/cncf/xds/go@v0.0.0-20251210132809-ee656c7534f5"
  ],
  "symbols": [
    "github.com/cncf/xds/go/xds/core/v3.ResourceLocator"
  ]
}
test/main.go
package main

import (
	"fmt"
	"os"

	corev3 "github.com/cncf/xds/go/xds/core/v3"
	"google.golang.org/protobuf/proto"
)

func check(ok bool, msg string) {
	if !ok {
		fmt.Fprintf(os.Stderr, "contract failed: %s\n", msg)
		os.Exit(1)
	}
}

func main() {
	// 1. ResourceLocator initializes with scheme, id, authority, resource type, exact context, and directives
	{
		loc := &corev3.ResourceLocator{
			Scheme:       corev3.ResourceLocator_XDSTP,
			Id:           "route-primary",
			Authority:    "example.com",
			ResourceType: "envoy.config.route.v3.RouteConfiguration",
			ContextParamSpecifier: &corev3.ResourceLocator_ExactContext{
				ExactContext: &corev3.ContextParams{
					Params: map[string]string{
						"region": "us-east",
					},
				},
			},
			Directives: []*corev3.ResourceLocator_Directive{
				{
					Directive: &corev3.ResourceLocator_Directive_Entry{
						Entry: "main-entry",
					},
				},
			},
		}
		check(loc.GetScheme() == corev3.ResourceLocator_XDSTP, "GetScheme mismatch")
		check(loc.GetId() == "route-primary", "GetId mismatch")
		check(loc.GetAuthority() == "example.com", "GetAuthority mismatch")
		check(loc.GetResourceType() == "envoy.config.route.v3.RouteConfiguration", "GetResourceType mismatch")
		check(loc.GetExactContext() != nil, "GetExactContext nil")
		check(loc.GetExactContext().GetParams()["region"] == "us-east", "ExactContext param mismatch")
		check(len(loc.GetDirectives()) == 1, "GetDirectives length mismatch")
		check(loc.GetDirectives()[0].GetEntry() == "main-entry", "Directive entry mismatch")
	}

	// 2. ResourceLocator getters safely return default values on nil and empty receivers without panic
	{
		var nilLoc *corev3.ResourceLocator
		check(nilLoc.GetScheme() == corev3.ResourceLocator_XDSTP, "nil GetScheme should be XDSTP")
		check(nilLoc.GetId() == "", "nil GetId should be empty")
		check(nilLoc.GetAuthority() == "", "nil GetAuthority should be empty")
		check(nilLoc.GetResourceType() == "", "nil GetResourceType should be empty")
		check(nilLoc.GetContextParamSpecifier() == nil, "nil GetContextParamSpecifier should be nil")
		check(nilLoc.GetExactContext() == nil, "nil GetExactContext should be nil")
		check(nilLoc.GetDirectives() == nil, "nil GetDirectives should be nil")
		check(nilLoc.Validate() == nil, "nil Validate should return nil")
		check(nilLoc.ValidateAll() == nil, "nil ValidateAll should return nil")

		emptyLoc := &corev3.ResourceLocator{}
		check(emptyLoc.GetScheme() == corev3.ResourceLocator_XDSTP, "empty GetScheme should be XDSTP")
		check(emptyLoc.GetId() == "", "empty GetId should be empty")
		check(emptyLoc.GetAuthority() == "", "empty GetAuthority should be empty")
		check(emptyLoc.GetResourceType() == "", "empty GetResourceType should be empty")
		check(emptyLoc.GetExactContext() == nil, "empty GetExactContext should be nil")
		check(emptyLoc.GetDirectives() == nil, "empty GetDirectives should be nil")
	}

	// 3. ResourceLocator validation passes for valid XDSTP, HTTP, and FILE schemes with non-empty resource type and nested alt directives
	{
		validXdstp := &corev3.ResourceLocator{
			Scheme:       corev3.ResourceLocator_XDSTP,
			ResourceType: "envoy.config.listener.v3.Listener",
		}
		check(validXdstp.Validate() == nil, "validXdstp Validate failed")
		check(validXdstp.ValidateAll() == nil, "validXdstp ValidateAll failed")

		validWithAlt := &corev3.ResourceLocator{
			Scheme:       corev3.ResourceLocator_HTTP,
			Authority:    "example.com",
			ResourceType: "envoy.config.cluster.v3.Cluster",
			Directives: []*corev3.ResourceLocator_Directive{
				{
					Directive: &corev3.ResourceLocator_Directive_Alt{
						Alt: &corev3.ResourceLocator{
							Scheme:       corev3.ResourceLocator_FILE,
							ResourceType: "envoy.config.cluster.v3.Cluster",
						},
					},
				},
			},
		}
		check(validWithAlt.Validate() == nil, "validWithAlt Validate failed")
		check(validWithAlt.ValidateAll() == nil, "validWithAlt ValidateAll failed")
	}

	// 4. ResourceLocator validation returns an error when resource type is empty or scheme is out of range
	{
		missingType := &corev3.ResourceLocator{
			Scheme:       corev3.ResourceLocator_XDSTP,
			ResourceType: "",
		}
		check(missingType.Validate() != nil, "expected error for empty ResourceType")

		badScheme := &corev3.ResourceLocator{
			Scheme:       corev3.ResourceLocator_Scheme(42),
			ResourceType: "envoy.config.listener.v3.Listener",
		}
		check(badScheme.Validate() != nil, "expected error for invalid Scheme")
		check(badScheme.ValidateAll() != nil, "expected ValidateAll error for invalid Scheme")
	}

	// 5. ResourceLocator round-trips through Protobuf binary wire format preserving equality
	{
		original := &corev3.ResourceLocator{
			Scheme:       corev3.ResourceLocator_XDSTP,
			Id:           "listener-01",
			Authority:    "example.com",
			ResourceType: "envoy.config.listener.v3.Listener",
			ContextParamSpecifier: &corev3.ResourceLocator_ExactContext{
				ExactContext: &corev3.ContextParams{
					Params: map[string]string{"zone": "az1"},
				},
			},
		}
		data, err := proto.Marshal(original)
		check(err == nil && len(data) > 0, "proto.Marshal failed")

		decoded := &corev3.ResourceLocator{}
		err = proto.Unmarshal(data, decoded)
		check(err == nil, "proto.Unmarshal failed")
		check(proto.Equal(original, decoded), "proto.Equal failed after round-trip")
	}

	// 6. proto.Clone creates an independent deep copy of ResourceLocator
	{
		src := &corev3.ResourceLocator{
			Scheme:       corev3.ResourceLocator_XDSTP,
			Id:           "src-id",
			Authority:    "example.com",
			ResourceType: "envoy.config.listener.v3.Listener",
			ContextParamSpecifier: &corev3.ResourceLocator_ExactContext{
				ExactContext: &corev3.ContextParams{
					Params: map[string]string{"tier": "frontend"},
				},
			},
		}
		dst := proto.Clone(src).(*corev3.ResourceLocator)
		check(proto.Equal(src, dst), "cloned message not equal to source")

		dst.Id = "modified-id"
		dst.GetExactContext().GetParams()["tier"] = "backend"
		check(src.GetId() == "src-id", "mutating clone affected source Id")
		check(src.GetExactContext().GetParams()["tier"] == "frontend", "mutating clone affected source ExactContext")
	}

	fmt.Println("PASS")
}

原始种子者

匿名