generate

package
v0.1.43 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2023 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package generate provides the generation of useful methods, variables, and constants for Goki code.

Index

Constants

This section is empty.

Variables

View Source
var KiMethodsTmpl = template.Must(template.New("KiMethods").
	Funcs(template.FuncMap{
		"HasEmbedDirective": HasEmbedDirective,
		"HasNoNewDirective": HasNoNewDirective,
		"KiPkg":             KiPkg,
	}).Parse(
	`
	{{if not (HasNoNewDirective .)}}
	// New{{.Name}} adds a new [{{.Name}}] with the given name
	// to the given parent. If the name is unspecified, it defaults
	// to the ID (kebab-case) name of the type, plus the
	// [{{KiPkg .}}Ki.NumLifetimeChildren] of the given parent.
	func New{{.Name}}(par {{KiPkg .}}Ki, name ...string) *{{.Name}} {
		return par.NewChild({{.Name}}Type, name...).(*{{.Name}})
	}
	{{end}}

	// KiType returns the [*gti.Type] of [{{.Name}}]
	func (t *{{.Name}}) KiType() *gti.Type {
		return {{.Name}}Type
	}

	// New returns a new [*{{.Name}}] value
	func (t *{{.Name}}) New() {{KiPkg .}}Ki {
		return &{{.Name}}{}
	}
	
	{{if HasEmbedDirective .}}
	// {{.Name}}Embedder is an interface that all types that embed {{.Name}} satisfy
	type {{.Name}}Embedder interface {
		As{{.Name}}() *{{.Name}}
	}
	
	// As{{.Name}} returns the given value as a value of type {{.Name}} if the type
	// of the given value embeds {{.Name}}, or nil otherwise
	func As{{.Name}}(k {{KiPkg .}}Ki) *{{.Name}} {
		if k == nil || k.This() == nil {
			return nil
		}
		if t, ok := k.({{.Name}}Embedder); ok {
			return t.As{{.Name}}()
		}
		return nil
	}
	
	// As{{.Name}} satisfies the [{{.Name}}Embedder] interface
	func (t *{{.Name}}) As{{.Name}}() *{{.Name}} {
		return t
	}
	{{end}}
	`,
))

KiMethodsTmpl is a template that contains the methods and functions specific to [ki.Ki] types.

Functions

func Generate

func Generate(cfg *config.Config) error

Generate is the main entry point to code generation that does all of the generation according to the given config info. It overrides the [config.Config.Generate.Gtigen.InterfaceConfigs] info.

func HasEmbedDirective added in v0.1.23

func HasEmbedDirective(typ *gtigen.Type) bool

HasEmbedDirective returns whether the given gtigen.Type has a "goki:embedder" comment directive. This function is used in KiMethodsTmpl.

func HasNoNewDirective added in v0.1.23

func HasNoNewDirective(typ *gtigen.Type) bool

HasNoNewDirective returns whether the given gtigen.Type has a "goki:no-new" comment directive. This function is used in KiMethodsTmpl.

func KiPkg added in v0.1.23

func KiPkg(typ *gtigen.Type) string

KiPkg returns the package identifier for the ki package in the context of the given type ("" if it is already in the ki package, and "ki." otherwise)

func ParsePackages added in v0.1.16

func ParsePackages(cfg *config.Config) ([]*packages.Package, error)

ParsePackages parses the package(s) based on the given config info.

Types

This section is empty.

Jump to

Keyboard shortcuts

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