v2 – k8s.io/gengo/v2 Index | Files | Directories

package gengo

import "k8s.io/gengo/v2"

Package gengo is a code-generation framework.

Index

Constants

const StdBuildTag = "ignore_autogenerated"

StdBuildTag is a suggested build-tag which tools can use both as an argument to GoBoilerplate and to Execute.

const StdGeneratedBy = "// Code generated by GENERATOR_NAME. DO NOT EDIT."

StdGeneratedBy is a suggested "generated by" line which tools can use as an argument to GoBoilerplate.

Functions

func Execute

func Execute(nameSystems namer.NameSystems, defaultSystem string, getTargets func(*generator.Context) []generator.Target, buildTag string, patterns []string) error

Execute implements most of a tool's main loop.

func ExtractCommentTags

func ExtractCommentTags(marker string, lines []string) map[string][]string

ExtractCommentTags parses comments for lines of the form:

'marker' + "key=value".

Values are optional; "" is the default. A tag can be specified more than one time and all values are returned. If the resulting map has an entry for a key, the value (a slice) is guaranteed to have at least 1 element.

Example: if you pass "+" for 'marker', and the following lines are in the comments:

+foo=value1
+bar
+foo=value2
+baz="qux"

Then this function will return:

map[string][]string{"foo":{"value1, "value2"}, "bar": {""}, "baz": {`"qux"`}}

Deprecated: Use ExtractFunctionStyleCommentTags.

func ExtractFunctionStyleCommentTags

func ExtractFunctionStyleCommentTags(marker string, tagNames []string, lines []string) (map[string][]Tag, error)

ExtractFunctionStyleCommentTags parses comments for special metadata tags. The marker argument should be unique enough to identify the tags needed, and should not be a marker for tags you don't want, or else the caller takes responsibility for making that distinction.

The tagNames argument is a list of specific tags being extracted. If this is nil or empty, all lines which match the marker are considered. If this is specified, only lines with begin with marker + one of the tags will be considered. This is useful when a common marker is used which may match lines which fail this syntax (e.g. which predate this definition).

This function looks for input lines of the following forms:

The arg is optional. If not specified (either as "key=value" or as "key()=value"), the resulting Tag will have an empty Args list.

The value is optional. If not specified, the resulting Tag will have "" as the value.

Tag comment-lines may have a trailing end-of-line comment.

The map returned here is keyed by the Tag's name without args.

A tag can be specified more than one time and all values are returned. If the resulting map has an entry for a key, the value (a slice) is guaranteed to have at least 1 element.

Example: if you pass "+" as the marker, and the following lines are in the comments:

+foo=val1  // foo
+bar
+foo=val2  // also foo
+baz="qux"
+foo(arg)  // still foo

Then this function will return:

	map[string][]Tag{
 	"foo": []Tag{{
			Name: "foo",
			Args: nil,
			Value: "val1",
		}, {
			Name: "foo",
			Args: nil,
			Value: "val2",
		}, {
			Name: "foo",
			Args: []string{"arg"},
			Value: "",
		}, {
			Name: "bar",
			Args: nil,
			Value: ""
		}, {
			Name: "baz",
			Args: nil,
			Value: "\"qux\""
	}}

This function should be preferred instead of ExtractCommentTags.

func ExtractSingleBoolCommentTag

func ExtractSingleBoolCommentTag(marker string, key string, defaultVal bool, lines []string) (bool, error)

ExtractSingleBoolCommentTag parses comments for lines of the form:

'marker' + "key=value1"

If the tag is not found, the default value is returned. Values are asserted to be boolean ("true" or "false"), and any other value will cause an error to be returned. If the key has multiple values, the first one will be used.

func GoBoilerplate

func GoBoilerplate(headerFile, buildTag, generatedBy string) ([]byte, error)

GoBoilerplate returns the Go file header: - an optional build tag (negative, set it to ignore generated code) - an optional boilerplate file - an optional "generated by" comment

Types

type Tag

type Tag struct {
	// Name is the name of the tag with no arguments.
	Name string
	// Args is a list of optional arguments to the tag.
	Args []string
	// Value is the value of the tag.
	Value string
}

Tag represents a single comment tag.

func (Tag) String

func (t Tag) String() string

Source Files

comments.go execute.go

Directories

PathSynopsis
examples
examples/kilroykilroy is a trivial gengo/v2 program which adds a tag-method to types.
examples/pointuhpointuh is a trivial gengo/v2 program which consider its inputs, and emits to new packages the same types, except for structs, where all fields are pointers.
examples/tracertracer is a trivial gengo/v2 program which prints the various hooks as they are called.
generatorPackage generator defines an interface for code generators to implement.
namerPackage namer has support for making different type naming systems.
parserPackage parser provides code to parse go files, type-check them, extract the types.
parser/tags
typesPackage types contains go type information, packaged in a way that makes auto-generation convenient, whether by template or straight go functions.
Version
v2.0.0-20250207200755-1244d31929d7 (latest)
Published
Feb 7, 2025
Platform
linux/amd64
Imports
11 packages
Last checked
2 weeks ago

Tools for package owners.