markers

package
v0.0.0-...-cece271 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

markers is a helper used to extract marker information from types. A marker is a comment line preceded with `+` that indicates to a generator something about the field or type.

The package returns a Markers interface, which can be used to access markers associated with a struct or a field within a struct.

Example:

inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
markersAccess := pass.ResultOf[markers.Analyzer].(markers.Markers)

// Filter to structs so that we can iterate over fields in a struct.
nodeFilter := []ast.Node{
	(*ast.StructType)(nil),
}

inspect.Preorder(nodeFilter, func(n ast.Node) {
	sTyp, ok := n.(*ast.StructType)
	if !ok {
		return
	}

	if sTyp.Fields == nil {
		return
	}

	for _, field := range sTyp.Fields.List {
		if field == nil || len(field.Names) == 0 {
			continue
		}

		structMarkers := markersAccess.StructMarkers(sTyp)
		fieldMarkers := markersAccess.FieldMarkers(field)

		...
	}
})

The result of StructMarkers or StructFieldMarkers is a MarkerSet which can be used to determine the presence of a marker, and the value of the marker. The MarkerSet is indexed based on the value of the marker, once the prefix `+` is removed.

Additional information about the marker can be found in the Marker struct, for each marker on the field.

Example:

fieldMarkers := markersAccess.FieldMarkers(field)

if fieldMarkers.Has("required") {
	requiredMarker := fieldMarkers["required"]
	...
}

Index

Constants

View Source
const UnnamedExpression = ""

UnnamedExpression is the expression key used when parsing markers that don't have a specific named expression.

An example of a marker without a named expression is "kubebuilder:default:=foo".

An example of a marker with named expressions is "kubebuilder:validation:XValidation:rule='...',message='...'".

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name:       "markers",
	Doc:        "Iterates over declarations within a package and parses the comments to extract markers",
	Run:        run,
	Requires:   []*analysis.Analyzer{inspect.Analyzer},
	ResultType: reflect.TypeOf(newMarkers()),
}

Analyzer is the analyzer for the markers package. It iterates over declarations within a package and parses the comments to extract markers.

Functions

This section is empty.

Types

type Marker

type Marker struct {
	// Identifier is the value of the marker once the leading comment, '+', and expressions are trimmed.
	Identifier string

	// Expressions are the set of expressions that have been specified for the marker
	Expressions map[string]string

	// RawComment is the raw comment line, unfiltered.
	RawComment string

	// Pos is the starting position in the file for the comment line containing the marker.
	Pos token.Pos

	// End is the ending position in the file for the coment line containing the marker.
	End token.Pos
}

Marker represents a marker extracted from a comment on a declaration.

type MarkerSet

type MarkerSet map[string][]Marker

MarkerSet is a set implementation for Markers that uses the Marker identifier as the key, but returns all full Markers with that identifier as the result.

func NewMarkerSet

func NewMarkerSet(markers ...Marker) MarkerSet

NewMarkerSet initialises a new MarkerSet with the provided values. If any markers have the same identifier, they will both be added to the list of markers for that identifier. No duplication checks are implemented.

func (MarkerSet) Has

func (ms MarkerSet) Has(identifier string) bool

Has returns whether marker(s) with the identifier given is present in the MarkerSet. If Has returns true, there is at least one marker with this identifier.

func (MarkerSet) HasWithExpressions

func (ms MarkerSet) HasWithExpressions(identifier string, expressions map[string]string) bool

HasWithExpressions returns whether marker(s) with the identifier and expressions are present in the MarkerSet.

func (MarkerSet) HasWithValue

func (ms MarkerSet) HasWithValue(marker string) bool

HasWithValue returns whether marker(s) with the given identifier and expression values (i.e "kubebuilder:object:root:=true") is present in the MarkerSet.

func (MarkerSet) Insert

func (ms MarkerSet) Insert(markers ...Marker)

Insert adds the given markers to the MarkerSet. If any markers have the same value, the latter marker in the list will take precedence, no duplication checks are implemented.

func (MarkerSet) UnsortedList

func (ms MarkerSet) UnsortedList() []Marker

UnsortedList returns a list of the markers, in no particular order.

type Markers

type Markers interface {
	// FieldMarkers returns markers associated to the field.
	FieldMarkers(*ast.Field) MarkerSet

	// StructMarkers returns markers associated to the given sturct.
	StructMarkers(*ast.StructType) MarkerSet

	// TypeMarkers returns markers associated to the given type.
	TypeMarkers(*ast.TypeSpec) MarkerSet
}

Markers allows access to markers extracted from the go types.

type Registry

type Registry interface {
	// Register adds the provided identifiers to the Registry.
	Register(ids ...string)

	// Match performs a greedy match to determine if an input
	// marker string matches a known marker identifier. It returns
	// the matched identifier and a boolean representing if a match was
	// found. If no match is found, the returned identifier will be an
	// empty string.
	Match(in string) (string, bool)
}

Registry is a thread-safe set of known marker identifiers.

func DefaultRegistry

func DefaultRegistry() Registry

DefaultRegistry is a global registry for known markers. New linters should register the markers they care about during an init() function.

func NewRegistry

func NewRegistry() Registry

NewRegistry creates a new Registry.

Jump to

Keyboard shortcuts

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