apidef

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: Apache-2.0 Imports: 13 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 apidef holds the API surface generated from Google's published definitions (NFR-MNT-001): the google.api.http routes and field behaviours (AIP-203) of the gRPC APIs, read from googleapis at the commit pinned in apis.yaml, and the methods and field annotations of the discovery (JSON/REST) APIs, read from the discovery documents vendored in the google.golang.org/api module pinned in go.mod. A version bump of either is followed by `task generate`, which rewrites zz_*.go.

Services use it to route REST onto their gRPC implementations (internal/transcode) and to validate requests: CheckProto for protobuf requests, CheckJSON for discovery request bodies.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckProto

func CheckProto(fullMethod string, req proto.Message) error

CheckProto applies the field behaviours (AIP-203) of a gRPC method to its request, before the service sees it:

  • OUTPUT_ONLY fields are cleared, as the server must ignore them;
  • REQUIRED fields of the request must be set (see observable), and so must the REQUIRED fields of every message set within it, except in a request carrying an update mask (partial resources are expected there) and except a nested resource's name (AIP-133: create takes the ID separately);
  • an update mask may not name an IMMUTABLE field.

Methods without a generated definition pass unchecked.

func FieldBehaviors

func FieldBehaviors(message string) map[string]Behavior

FieldBehaviors returns the annotated fields of a message (by proto field name), or nil if none of its fields carry a behaviour.

func GoogleapisCommit

func GoogleapisCommit() string

GoogleapisCommit is the googleapis/googleapis commit the proto tables were generated from.

func RPCForInput

func RPCForInput(message string) string

RPCForInput returns the full method name of the one RPC taking request messages of the given type, or "" if none or several do (the IAM and operations requests are shared by every API).

func Revision

func Revision(name, version string) string

Revision returns the pinned discovery document revision of an API.

Types

type Behavior

type Behavior uint8

Behavior is a set of google.api.FieldBehavior values.

const (
	// Required fields must be set by the caller.
	Required Behavior = 1 << iota
	// OutputOnly fields are set by the server; input values are ignored.
	OutputOnly
	// Immutable fields may be set on create but never changed.
	Immutable
	// InputOnly fields are accepted on input and never returned.
	InputOnly
	// Identifier marks a resource's name field.
	Identifier
	// Optional fields are explicitly optional.
	Optional
)

type Binding

type Binding struct {
	Method string // GET, POST, PATCH, PUT, DELETE or a custom verb
	Path   string // path template, e.g. /v1/{parent=projects/*}/topics
	Body   string // "", "*" or a request field name
}

Binding is one google.api.http rule (the primary rule or an additional binding).

type Method

type Method struct {
	Name   string
	Input  string // request message full name
	Output string // response message full name
	HTTP   []Binding
}

Method is one RPC.

func MethodByRPC

func MethodByRPC(fullMethod string) *Method

MethodByRPC returns the method for a gRPC full method name ("/google.pubsub.v1.Publisher/CreateTopic"), or nil.

type RESTAPI

type RESTAPI struct {
	Name        string // e.g. compute
	Version     string // e.g. v1
	Revision    string // the discovery document's revision
	ServicePath string // e.g. compute/v1/
	Methods     []*RESTMethod
	Schemas     map[string]*Schema
	// contains filtered or unexported fields
}

RESTAPI is a discovery (JSON/REST) API.

func REST

func REST(name, version string) *RESTAPI

REST returns a discovery API by name and version, or nil.

func (*RESTAPI) CheckJSON

func (a *RESTAPI) CheckJSON(m *RESTMethod, query url.Values, body []byte) error

CheckJSON validates a request against m's annotations: required query parameters, and required body fields (annotations.required naming m). The errors match the compute API's ("Required field 'resource.name' not specified").

func (*RESTAPI) Handler

func (a *RESTAPI) Handler(h http.Handler) http.Handler

Handler validates each request h serves with CheckJSON first (required parameters and body fields); requests that match no method pass through.

func (*RESTAPI) Match

func (a *RESTAPI) Match(httpMethod, rel string) *RESTMethod

Match returns the method serving an HTTP method and an escaped path relative to the service path (see Rel), or nil.

func (*RESTAPI) Rel

func (a *RESTAPI) Rel(path string) string

Rel returns a request path relative to the API's service path, accepting both the real host form (/compute/v1/projects/...) and the gateway's prefix-stripped form (/v1/projects/...).

type RESTMethod

type RESTMethod struct {
	ID           string   // e.g. compute.addresses.insert
	HTTPMethod   string   // GET, POST, ...
	Path         string   // flat path relative to ServicePath
	Request      string   // request body schema, if any
	RequestParam string   // the body's parameter name in errors (compute: "resource")
	Params       []string // required query parameters
}

RESTMethod is one discovery method.

type Ref

type Ref struct {
	Schema string
	Kind   RefKind
}

Ref is a property's nested schema.

type RefKind

type RefKind uint8

RefKind is how a property holds a nested schema.

const (
	RefObject RefKind = iota // a single object
	RefList                  // an array of objects
	RefMap                   // an object of objects (additionalProperties)
)

type Schema

type Schema struct {
	// Required maps a property to the methods that require it
	// (annotations.required).
	Required map[string][]string
	// OutputOnly lists readOnly properties.
	OutputOnly []string
	// Refs are the properties whose values carry annotated schemas.
	Refs map[string]Ref
}

Schema is the field annotations of one discovery schema. Inline object schemas are named after their parent ("Firewall.allowed[]").

type Service

type Service struct {
	Name    string // fully qualified, e.g. google.pubsub.v1.Publisher
	File    string // googleapis path of the defining .proto
	Methods []*Method
}

Service is a gRPC service and the HTTP bindings of its methods.

func ServiceByName

func ServiceByName(name string) *Service

ServiceByName returns the generated definition of a gRPC service, or nil.

func (*Service) FullMethod

func (s *Service) FullMethod(m *Method) string

FullMethod returns the gRPC full method name of m in s.

func (*Service) Method

func (s *Service) Method(name string) *Method

Method returns the named method, or nil.

Directories

Path Synopsis
internal
apigen command
Command apigen generates internal/apidef's route and field-behaviour tables (NFR-MNT-001) from Google's published API definitions:
Command apigen generates internal/apidef's route and field-behaviour tables (NFR-MNT-001) from Google's published API definitions:

Jump to

Keyboard shortcuts

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