codegen

package
v0.0.1-test Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2022 License: AGPL-3.0 Imports: 37 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractPluginTrees

func ExtractPluginTrees(parent fs.FS, lib thema.Library) (map[string]PluginTreeOrErr, error)

ExtractPluginTrees attempts to create a *pfs.Tree for each of the top-level child directories in the provided fs.FS.

Errors returned from pfs.ParsePluginFS are placed in the option map. Only filesystem traversal and read errors will result in a non-nil second return value.

func GenGrafanaHeader

func GenGrafanaHeader(vars HeaderVars) string

GenGrafanaHeader creates standard header elements for generated Grafana files.

func MapCUEImportToTS

func MapCUEImportToTS(path string) (string, error)

MapCUEImportToTS maps the provided CUE import path to the corresponding TypeScript import path in generated code.

Providing an import path that is not allowed results in an error. If a nil error and empty string are returned, the import path should be dropped in generated code.

Types

type CoremodelDeclaration

type CoremodelDeclaration struct {
	Lineage thema.Lineage
	// Absolute path to the coremodel's coremodel.cue file.
	LineagePath string
	// Path to the coremodel's coremodel.cue file relative to repo root.
	RelativePath string
	// Indicates whether the coremodel is considered canonical or not. Generated
	// code from not-yet-canonical coremodels should include appropriate caveats in
	// documentation and possibly be hidden from external public API surface areas.
	IsCanonical bool

	// Indicates whether the coremodel represents an API type, and should therefore
	// be included in API client code generation.
	IsAPIType bool
}

CoremodelDeclaration contains the results of statically analyzing a Grafana directory for a Thema lineage.

func ExtractLineage

func ExtractLineage(path string, lib thema.Library) (*CoremodelDeclaration, error)

ExtractLineage loads a Grafana Thema lineage from the filesystem.

The provided path must be the absolute path to the file containing the lineage to be loaded.

This loading approach is intended primarily for use with code generators, or other use cases external to grafana-server backend. For code within grafana-server, prefer lineage loaders provided in e.g. pkg/coremodel/*.

func (*CoremodelDeclaration) GenerateGoCoremodel

func (cd *CoremodelDeclaration) GenerateGoCoremodel(path string) (WriteDiffer, error)

GenerateGoCoremodel generates a standard Go model struct and coremodel implementation from a coremodel CUE declaration.

The provided path must be a directory. Generated code files will be written to that path. The final element of the path must match the Lineage.Name().

func (*CoremodelDeclaration) GenerateTypescriptCoremodel

func (cd *CoremodelDeclaration) GenerateTypescriptCoremodel() (*tsast.File, error)

func (*CoremodelDeclaration) PathVersion

func (cd *CoremodelDeclaration) PathVersion() string

PathVersion returns the string path element to use for the latest schema. "x" if not yet canonical, otherwise, "v<major>"

type GoGenConfig

type GoGenConfig struct {
	// Types indicates whether corresponding Go types should be generated from the
	// latest version in the lineage(s).
	Types bool

	// ThemaBindings indicates whether Thema bindings (an implementation of
	// ["github.com/grafana/thema".LineageFactory]) should be generated for
	// lineage(s).
	ThemaBindings bool

	// DocPathPrefix allows the caller to optionally specify a path to be prefixed
	// onto paths generated for documentation. This is useful for io/fs-based code
	// generators, which typically only have knowledge of paths relative to the fs.FS
	// root, typically an encapsulated subpath, but docs are easier to understand when
	// paths are relative to a repository root.
	//
	// Note that all paths are normalized to use slashes, regardless of the
	// OS running the code generator.
	DocPathPrefix string
}

type HeaderVars

type HeaderVars = tvars_autogen_header

type PluginTree

type PluginTree pfs.Tree

PluginTree is a pfs.Tree. It exists so we can add methods for code generation to it.

It is, for now, tailored specifically to Grafana core's codegen needs.

func (*PluginTree) GenerateGo

func (pt *PluginTree) GenerateGo(path string, cfg GoGenConfig) (WriteDiffer, error)

func (*PluginTree) GenerateTypeScriptAST

func (pt *PluginTree) GenerateTypeScriptAST() (*tsast.File, error)

type PluginTreeOrErr

type PluginTreeOrErr struct {
	Err  error
	Tree *PluginTree
}

PluginTreeOrErr represents either a *pfs.Tree, or the error that occurred while trying to create one. TODO replace with generic option type after go 1.18

type TreeAndPath

type TreeAndPath struct {
	Tree *PluginTree
	// path relative to path prefix UUUGHHH (basically {panel,datasource}/<dir>}
	Path string
}

FIXME unexport this and refactor, this is way too one-off to be in here

type WriteDiffer

type WriteDiffer map[string][]byte

WriteDiffer is a pseudo-filesystem that supports batch-writing its contents to the real filesystem, or batch-comparing its contents to the real filesystem. Its intended use is for idiomatic `go generate`-style code generators, where it is expected that the results of codegen are committed to version control.

In such cases, the normal behavior of a generator is to write files to disk, but in CI, that behavior should change to verify that what is already on disk is identical to the results of code generation. This allows CI to ensure that the results of code generation are always up to date. WriteDiffer supports these related behaviors through its Write() and Verify() methods, respectively.

Note that the statelessness of WriteDiffer means that, if a particular input to the code generator goes away, it will not notice generated files left behind if their inputs are removed. TODO introduce a search/match system

func GenPluginTreeList

func GenPluginTreeList(trees []TreeAndPath, prefix, target string, ref bool) (WriteDiffer, error)

FIXME unexport this and refactor, this is way too one-off to be in here

func GenerateCoremodelRegistry

func GenerateCoremodelRegistry(path string, ecl []*CoremodelDeclaration) (WriteDiffer, error)

GenerateCoremodelRegistry produces Go files that define a registry with references to all the Go code that is expected to be generated from the provided lineages.

func NewWriteDiffer

func NewWriteDiffer() WriteDiffer

func (WriteDiffer) Merge

func (wd WriteDiffer) Merge(wd2 WriteDiffer) error

Merge combines all the entries from the provided WriteDiffer into the callee WriteDiffer. Duplicate paths result in an error.

func (WriteDiffer) Verify

func (wd WriteDiffer) Verify() error

Verify checks the contents of each file against the filesystem. It emits an error if any of its contained files differ.

func (WriteDiffer) Write

func (wd WriteDiffer) Write() error

Write writes all of the files to their indicated paths.

Jump to

Keyboard shortcuts

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