Documentation
¶
Index ¶
- Variables
- func GetMessageByResourceType(resourceType string) (*protogen.Message, error)
- func LiteralSegmentCount(pattern *ParsedPattern) int
- func RegisterAncestors(files []*protogen.File) error
- func RegisterAnnotations(files []*protogen.File) error
- type ParsedPattern
- type ParsedResource
- type RPC
- type Registry
- type UnionVariable
Constants ¶
This section is empty.
Variables ¶
var PackageToRegistry = map[protoreflect.FullName]*Registry{}
PackageToRegistry maps each proto package to its isolated registry.
Functions ¶
func GetMessageByResourceType ¶
GetMessageByResourceType finds a message across all registries by resource type.
func LiteralSegmentCount ¶
func LiteralSegmentCount(pattern *ParsedPattern) int
LiteralSegmentCount returns the number of literal (non-variable) segments of the pattern.
func RegisterAncestors ¶
RegisterAncestors links every resource to its immediate parent within each proto package by matching each of its patterns against the registered prefix patterns. Every pattern contributes a parent-child link, so multi-pattern resources are registered as children of all of their parents.
Unresolvable prefixes are tolerated here: this pass runs over every resource in the package, including ones no generator consumes. Parse reports missing sub-pattern descriptors for the resources that are actually used.
func RegisterAnnotations ¶
RegisterAnnotations scans all files and populates per-package registries with resource descriptors from both file-level resource_definition annotations and message-level resource annotations.
Types ¶
type ParsedPattern ¶
type ParsedPattern struct {
// Resource is the resource this pattern belongs to.
Resource *ParsedResource
// Value is the raw pattern, e.g. "organizations/{organization}/shelves/{shelf}".
Value string
// Variables are the snake_case pattern variables, e.g. ["organization", "shelf"].
Variables []string
// Singleton is true when the pattern ends on a literal segment.
Singleton bool
// Parent is the parent resource's pattern prefixing this one; nil at the root.
Parent *ParsedPattern
// contains filtered or unexported fields
}
ParsedPattern is one resource name pattern of a resource, with its variables and its position in the resource hierarchy resolved.
func SortPatternsBySpecificity ¶
func SortPatternsBySpecificity(patterns []*ParsedPattern) []*ParsedPattern
SortPatternsBySpecificity orders patterns so a name matching several patterns hits the most specific one first: more literal segments first. Patterns with different segment counts never both match a name, so the sort is stable to otherwise preserve declaration order.
func (*ParsedPattern) VariableID ¶
func (p *ParsedPattern) VariableID(goStyle bool) (string, error)
VariableID returns the pattern's own identifier variable, e.g. "shelfId". Singletons have no identifier of their own.
func (*ParsedPattern) VariableIDPtrs ¶
func (p *ParsedPattern) VariableIDPtrs() string
VariableIDPtrs returns the comma-separated addresses of the pattern's identifier variables.
func (*ParsedPattern) VariableIDs ¶
func (p *ParsedPattern) VariableIDs(goStyle bool) string
VariableIDs returns the comma-separated identifier variables of the pattern.
type ParsedResource ¶
type ParsedResource struct {
Desc *annotationspb.ResourceDescriptor
Type string
Patterns []*ParsedPattern
Children []*ParsedResource
}
ParsedResource is a fully resolved resource with its patterns and children.
func Parse ¶
func Parse(reg *Registry, resourceDescriptor *annotationspb.ResourceDescriptor) (*ParsedResource, error)
Parse recursively resolves a resource descriptor into a ParsedResource.
Parsing runs in three phases. The resource is memoized and all of its patterns are scanned *before* any recursive resolution, so that resources reached through a parent/child cycle (e.g. Author → Organization → Note → Author) always observe a complete pattern set. Parent links and children are filled in afterwards; back-edges to in-progress resources are safe because everything is completed by the time the outermost Parse returns.
func ParseFromMessage ¶
func ParseFromMessage(message *protogen.Message) (*ParsedResource, error)
ParseFromMessage resolves a ParsedResource from a protogen message.
func (*ParsedResource) PluralGoName ¶
func (pr *ParsedResource) PluralGoName() string
func (*ParsedResource) SinglePattern ¶
func (pr *ParsedResource) SinglePattern() (*ParsedPattern, error)
SinglePattern returns the resource's only pattern. Generators that do not yet support multi-pattern resources use this as an explicit guard.
func (*ParsedResource) SingularGoName ¶
func (pr *ParsedResource) SingularGoName() string
func (*ParsedResource) SingularSnakeCase ¶
func (pr *ParsedResource) SingularSnakeCase() string
func (*ParsedResource) UnionVariables ¶
func (pr *ParsedResource) UnionVariables() ([]UnionVariable, error)
UnionVariables returns the union of the pattern variables across all patterns of the resource. Variables are ordered by first appearance across patterns, except the resource's own identifier which is always last. Multi-pattern singleton resources are not supported.
type RPC ¶
type RPC struct {
StandardMethod *aippb.StandardMethod
Message *protogen.Message
ParsedResource *ParsedResource
Create, Update, Delete, Get, BatchGet, List, Search bool
}
RPC represents a parsed standard AIP method bound to its target resource.
type Registry ¶
type Registry struct {
MessageTypeToResourceDescriptor map[string]*annotationspb.ResourceDescriptor
ResourceTypeToResourceDescriptor map[string]*annotationspb.ResourceDescriptor
ResourcePatternToResourceDescriptor map[string]*annotationspb.ResourceDescriptor
ResourceTypeToChildResourceTypeSet map[string]map[string]struct{}
ResourceTypeToMessage map[string]*protogen.Message
ParsedResourceTypeToParsedResource map[string]*ParsedResource
}
Registry holds all resource mappings scoped to a single proto package.
func GetOrCreateRegistry ¶
func GetOrCreateRegistry(pkg protoreflect.FullName) *Registry
func NewRegistry ¶
func NewRegistry() *Registry
type UnionVariable ¶
UnionVariable is a variable in the union of a resource's pattern variables.