generate

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2024 License: BSD-3-Clause Imports: 18 Imported by: 0

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

View Source
var KiMethodsTmpl = template.Must(template.New("KiMethods").
	Funcs(template.FuncMap{
		"HasEmbedDirective": HasEmbedDirective,
		"HasNoNewDirective": HasNoNewDirective,
		"DocToComment":      gtigen.DocToComment,
		"KiPkg":             KiPkg,
	}).Parse(
	`
	{{if not (HasNoNewDirective .)}}
	// New{{.LocalName}} adds a new [{{.LocalName}}] with the given name to the given parent:
	{{DocToComment .Doc}}
	func New{{.LocalName}}(par {{KiPkg .}}Ki, name ...string) *{{.LocalName}} {
		return par.NewChild({{.LocalName}}Type, name...).(*{{.LocalName}})
	}
	{{end}}

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

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

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

Functions

func Generate

func Generate(c *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 GetWebcoreExamples added in v0.0.3

func GetWebcoreExamples(c *config.Config) (ordmap.Map[string, []byte], error)

GetWebcoreExamples collects and returns all of the webcore examples.

func HasEmbedDirective

func HasEmbedDirective(typ *gtigen.Type) bool

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

func HasNoNewDirective

func HasNoNewDirective(typ *gtigen.Type) bool

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

func KiPkg

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

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

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

func Webcore added in v0.0.3

func Webcore(c *config.Config) error

Webcore does any necessary generation for webcore.

func WriteWebcoregen added in v0.0.3

func WriteWebcoregen(c *config.Config, examples ordmap.Map[string, []byte]) error

WriteWebcoregen constructs the webcoregen.go file from the given examples.

Types

This section is empty.

Jump to

Keyboard shortcuts

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