format

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package format holds the one description of each input file and checks a file's tree against it.

Index

Constants

View Source
const AgentExpect = "an IPv4 address and a port from 1 to 65535, such as 127.0.0.1:2000"

AgentExpect is exported because the reader's message for an agent its pattern admits but ParseAgent refuses must be the check's, and the reader's tooLarge cannot walk the provider's Either to read it from the description.

Variables

View Source
var (
	MatchKinds   = []string{"prefix", "path", "pattern"}
	ServicePorts = []Field{port("http", "The service's HTTP port.", false), port("grpc", "The service's gRPC port.", false)}
)

MatchKinds are the keys of which a route writes exactly one, and ServicePorts the port keys of a service, each named by its protocol. The decoder reads a route's match and a service's ports through these two lists, so a key added to either is read without a second list to keep in step.

View Source
var Methods = Field{
	Doc:  "The RPC methods the gateway routes, each with what the check is told about it.",
	Kind: List,
	Item: &methodField,
}

Methods describes the RPC methods file: one entry per method. Which service a method goes to is decided by the rpc entry of the proxyfile, not here.

View Source
var Origins = Field{
	Doc:  "The origins file: the CORS settings and, for each environment, its hosts and the browser origins it admits.",
	Kind: Map,
	Fields: []Field{
		corsField,
		{Name: "environments", Doc: "The environments, each with its hosts and the browser origins it admits.", Kind: List, Required: true,
			Item: &Field{Doc: "One environment.", Kind: Map, Fields: slices.Concat([]Field{
				{Name: "name", Doc: "The environment's name.", Kind: Text, Required: true},
			}, environmentLists)}},
	},
}

The rules for hosts, origins and patterns belong to the origins reader, which names the environment in its messages; this description checks only the keys and the kinds.

View Source
var Proxyfile = Field{
	Doc:  "A proxyfile: one gateway, the services behind it and the routes to them, from which proxyfile writes an Envoy configuration.",
	Kind: Map,
	Fields: []Field{
		{Name: "namespace", Doc: "The namespace of the gateway's own metadata, which the routes and the request log use; proxyfile when not written.",
			Kind: Text, Pattern: `^[a-z][a-z0-9._-]*$`, Expect: "lower-case letters, digits, dots, - and _, starting with a letter"},
		port("listen", "The port of the public listener, on 0.0.0.0.", true),
		port("admin", "The port of the admin listener, on 127.0.0.1; no admin listener when not written.", false),
		{Name: "limits", Doc: "Limits on memory, connections and request headers.", Kind: Map, Fields: []Field{
			{Name: "heap", Doc: "The heap budget: the gateway shrinks its heap at 90% of it and refuses new requests at 98%.",
				Kind: Text, Pattern: model.SizePattern, Expect: sizeExpect},
			{Name: "connections", Doc: "The most connections at once, from clients and to each service.", Kind: Int, Min: 1, Max: largestUint32},
			{Name: "request_headers", Doc: "The largest header block and the most header fields a request may carry.", Kind: Map, Fields: []Field{
				{Name: "size", Doc: "The largest header block of a request.", Kind: Text, Required: true, Pattern: model.SizePattern, Expect: sizeExpect},
				{Name: "count", Doc: "The most header fields in a request.", Kind: Int, Required: true, Min: 1, Max: largestUint32},
			}},
		}},
		{Name: "timeouts", Doc: "How long a request, an idle request and an idle connection to a service may last.", Kind: Map, Fields: []Field{
			timeOrNone("request", "The longest a whole request may take."),
			timeOrNone("idle", "The longest a request may go without sending or receiving data."),
			timeOrNone("upstream_idle", "The longest a connection to a service is kept open with no request on it."),
		}},
		{Name: "services", Doc: "The services behind the gateway, on the same host, by name.", Kind: Dict, Required: true,
			Key: &Field{Name: "service name", Kind: Text, Pattern: serviceNamePattern, Expect: serviceNameExpect},
			Item: &Field{Doc: "One service: a port per protocol, an optional Host header and an optional address.", Kind: Map, Fields: slices.Concat(ServicePorts, []Field{
				{Name: "host_header", Doc: "The Host header a request carries when a route sends it to this service.",
					Kind: Text, Pattern: `^[A-Za-z0-9.:\[\]-]+$`, Expect: "a host, with an optional port"},
				{Name: "address", Doc: "The service's IP address; 127.0.0.1 when not written.", Kind: Text, Pattern: `^[^\t\n\f\r ]+$`, Expect: "an IP address"},
			})}},
		{Name: "auth", Doc: "The external check every request goes through before it reaches a service.", Kind: Map, Fields: []Field{
			{Name: "ask", Doc: "The service and the path the gateway asks.", Kind: Text, Required: true,
				Pattern: `^[a-z][a-z0-9-]* /[^\t\n\f\r ]*$`, Expect: "a service and a path, such as accounts /check"},
			{Name: "timeout", Doc: "How long the gateway waits for the answer.", Kind: Text, Required: true,
				Pattern: model.TimePattern, Expect: "a time such as 2s"},
			{Name: "enabled", Doc: "true runs the check on every request it applies to, false on none.", Kind: Bool, Required: true},
			{Name: "when_unavailable", Doc: "What happens to a request when the check cannot be asked: allow lets it through, deny refuses it.",
				Kind: Enum, Required: true, Values: []string{"allow", "deny"}},

			{Name: "only_with_header", Doc: "The check runs only on requests that carry this header.",
				Kind: Text, Pattern: lowerHeaderPattern, Expect: lowerHeaderExpect},
			headers("send", "The request headers the check is sent; a name ending in * is a prefix."),
			headers("accept", "The headers of the check's answer that reach the service; a name ending in * is a prefix."),
		}},
		headers("remove_from_clients", "Headers removed from every client request before routing and before any filter; a name ending in * is a prefix."),
		corsField,
		{Name: "origins", Doc: "The browser origins each environment admits, by environment name; or, as in 1.0, the name of an origins file, relative to this file.",
			Kind: Either, Required: true, Expect: "a mapping of environments by name, or a file name", Choices: []Field{
				{Kind: Text, Pattern: `^[^\t\n\f\r ]+$`, Expect: "a file name"},
				{Kind: Dict, Key: &Field{Name: "environment name", Kind: Text, Pattern: `[\s\S]`, Expect: "one character or more"},
					Item: &Field{Doc: "One environment: its hosts and the browser origins it admits.", Kind: Map, Fields: environmentLists}},
			}},
		{Name: "routes", Doc: "The routes, tried in the order written; the rpc entry marks where the RPC routes go.", Kind: List, Required: true,
			Item: &Field{Kind: Either, Expect: "a mapping: a route or an rpc entry", Choices: []Field{
				{Doc: "The place in the order where the RPC routes go.", Kind: Map, Select: "rpc", Fields: []Field{
					{Name: "rpc", Doc: "The RPC routes: one exact-path route for each RPC method.", Kind: Map, Required: true, Fields: []Field{
						retries("How many times an RPC call that never reached its service is tried again."),
						{Name: "to", Doc: "The service the RPC methods of each package go to.", Kind: Dict, Required: true,
							Key: &Field{Name: "package pattern", Kind: Text,
								Pattern: `^(\*|[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)*(\.\*)?)$`,
								Expect:  "*, a package name ending in .*, or a full service name"},
							Item: &Field{Kind: Either, Expect: "a service name, or a mapping with service and name", Choices: []Field{
								serviceName("", "The service the methods go to.", false),
								{Doc: "The service the methods go to, and the name of their routes.", Kind: Map, Fields: []Field{
									serviceName("service", "The service the methods go to.", true),
									{Name: "name", Doc: "The name of the routes; the service's name followed by -rpc when not written.",
										Kind: Text, Pattern: routeNamePattern, Expect: routeNameExpect},
								}},
							}}},
					}},
				}},
				{Doc: "A route: a name, what it matches and the service it goes to.", Kind: Map, OneOf: MatchKinds, Fields: []Field{
					{Name: "name", Doc: "The route's name.", Kind: Text, Required: true, Pattern: routeNamePattern, Expect: routeNameExpect},
					path("prefix", "Matches paths that start with this text."),
					path("path", "Matches this path exactly."),

					{Name: "pattern", Doc: "Matches paths by this regular expression, in RE2 syntax.", Kind: Text,
						Pattern: `[\s\S]`, Expect: "a regular expression that is not empty"},
					{Name: "method", Doc: "Matches only requests with this HTTP method.", Kind: Text, Pattern: `^[A-Z]+$`, Expect: "an HTTP method in capitals, such as POST"},
					serviceName("to", "The service the route goes to.", true),
					path("rewrite", "Replaces the matched prefix or path before the request goes to the service."),
					{Name: "auth", Doc: "off sends the route's requests to the service without the check.", Kind: Enum, Values: []string{"off"}},
					retries("How many times a request that never reached the service is tried again."),
				}},
			}}},
		{Name: "rpc", Doc: "The RPC methods the gateway routes; the routes' rpc entry maps them to services.", Kind: Map,
			OneOf: []string{"methods", "proto"}, Fields: []Field{
				{Name: "methods", Doc: "One entry for each RPC method the gateway routes, as a build writes them from the gRPC descriptors.",
					Kind: List, Item: &methodField},
				protoField,
			}},
		{Name: "log", Doc: "Which requests the request log writes.", Kind: Map, Fields: []Field{
			{Name: "status_from", Doc: "The lowest status that is always logged.", Kind: Int, Required: true, Min: 100, Max: 599},
			{Name: "sample", Doc: "The share of the other requests that is logged.", Kind: Text, Required: true,
				Pattern: model.PercentPattern, Expect: "a whole percentage such as 1%"},
		}},
		tracingField,
	},
}

Proxyfile describes the file proxyfile reads: every key, its kind and its rule. The meaning rules that need more than one key, such as a route naming a declared service, belong to the reader.

Functions

func Check

func Check(spec Field, n *tree.Node) []model.Problem

Check returns every problem of n against spec, sorted, so one run names them all.

func Schema

func Schema(spec Field) ([]byte, error)

Schema writes spec as a JSON Schema (draft 2020-12), so an editor checks a file with the keys, kinds and patterns the checker uses. Every schema object is a Go map, which encoding/json writes with its keys sorted, and every list keeps the description's order, so one description always gives the same bytes.

Types

type Field

type Field struct {
	Name     string // the key; "" for the root, list items and dictionary values
	Doc      string // one sentence, the schema's description
	Kind     Kind
	Required bool
	Fields   []Field  // Map
	OneOf    []string // Map: exactly one of these keys
	Item     *Field   // List items; Dict values
	Key      *Field   // Dict: the keys the writer chooses (Name says what they are, Pattern and Expect their rule)
	// Pattern (Text and Key) runs in two engines: Go's RE2 in the checker, and an editor's ECMAScript engine through
	// the JSON Schema, which writes it unchanged. So it keeps to what both read alike. The package fails to load on a
	// form Go refuses, such as a lookahead, an atomic group or a possessive repeat. Of the forms Go accepts,
	// ECMAScript refuses a flag set for the rest of the pattern, such as (?i), and (?P<name>; it reads \A and \z as
	// letters or refuses them; and \s, \S and a . outside brackets match other characters in it: write Go's \S out as
	// [^\t\n\f\r ]. [\s\S], every character, is the same in both.
	Pattern  string
	Expect   string   // Text, Key, Either: completes "must be ..."
	Min, Max int64    // Int; Max 0 is no maximum. int64, not int, so a uint32 limit fits on a 32-bit build too
	Values   []string // Enum
	Choices  []Field  // Either: a Text choice, and Map or Dict choices
	Select   string   // a Map choice of an Either: chosen when the node has this key; the Map or Dict choice without Select is the default
}

Field describes one key of a file format. The checker reads it, and `proxyfile schema` prints it as JSON Schema, so the keys an editor offers are the keys the program accepts.

func (*Field) Child

func (f *Field) Child(name string) *Field

Child returns the description of the key name of a Map, or nil. A later stage that refuses a value the check accepted quotes the child's Expect, so both stages describe a value in the same words.

type Kind

type Kind int

Kind is what a key's value must be. Map has the keys its Fields name and Dict the keys its writer chooses; Either is whichever of its Choices fits the value.

const (
	Map Kind = iota + 1
	Dict
	List
	Text
	Int
	Bool
	Enum
	Either
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL