Help us shape the future of Go! Take the 2026 Go Developer Survey

httprule

package
v0.50.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package httprule provides a parser for the path templates found in [google.api.http] annotations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseResourcePattern added in v0.8.0

func ParseResourcePattern(pathTemplate string) (*api.PathTemplate, error)

ParseResourcePattern parses a Resource Pattern, defined at the [google.api.resource annotation].

This is different from `ParseSegments` because it does not expect a leading `/`.

func ParseSegments

func ParseSegments(pathTemplate string) (*api.PathTemplate, error)

ParseSegments parses a PathTemplate, defined at the google.api.http annotation.

The following documentation was copied and adapted from the C++ HTTP Annotation parser

A `google.api.http` annotation describes how to convert gRPC RPCs to HTTP URLs. The description uses a "path template", showing what portions of the URL path are replaced with values from the gRPC request message.

These path templates follow a specific grammar. The grammar is defined by:

Template = "/" Segments [ Verb ] ;
Segments = Segment { "/" Segment } ;
Segment  = "*" | "**" | LITERAL | Variable ;
Variable = "{" FieldPath [ "=" Segments ] "}" ;
FieldPath = IDENT { "." IDENT } ;
Verb     = ":" LITERAL ;

The specific notation is not defined, but it seems inspired by Backus-Naur Form. In this notation, `{ ... }` allows repetition.

The documentation goes on to say:

A variable template must not contain other variables.

So the grammar is better defined by:

Template = "/" Segments [ Verb ] ;
Segments = Segment { "/" Segment } ;
Segment  = Variable | PlainSegment;
PlainSegment  = "*" | "**" | LITERAL ;
Variable = "{" FieldPath [ "=" PlainSegments ] "}" ;
PlainSegments = PlainSegment { "/" PlainSegment };
FieldPath = IDENT { "." IDENT } ;
Verb     = ":" LITERAL ;

Neither "IDENT" nor "LITERAL" are defined. From context we can infer that IDENT must be a valid proto3 identifier, so matching the regular expression `[A-Za-z][A-Za-z0-9_]*`. Likewise, we can infer that LITERAL must be a path segment in a URL. RFC 3986 provides a definition for these, which we summarize as:

Segment = pchar { pchar } pchar = unreserved | pct-encoded | sub-delims | ":" | "@" unreserved = ALPHA | DIGIT | "-" | "." | "_" | "~" pct-encoded = "%" HEXDIG HEXDIG sub-delims = "!" | "$" | "&" | "'" | "(" | ")" | "*" | "+" | "," | ";" | "="

ALPHA = [A-Za-z] DIGIT = [0-9] HEXDIG = [0-9A-Fa-f]

Because pchar includes special characters like ':' and '*', which are part of the HTTP Rule spec, we define LITERAL as the following subset of pchar:

LITERAL = unreserved | pct-encoded { unreserved | pct-encoded }

Types

type Identifier

type Identifier string

Identifier is an identifier in a path template.

type Literal

type Literal string

Literal is a literal in a path template.

Jump to

Keyboard shortcuts

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