generator

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KDataProjectName  = "ProjectName"
	KDataRepository   = "Repository"
	KDataProject      = "Project"
	KDataLanaiModules = "LanaiModules"
)

Global Keys in template's context data as map

View Source
const (
	GroupOrderAPI = iota * 100
	GroupOrderOPAPolicy
	GroupOrderSecurity
	GroupOrderProject
)
View Source
const (
	KDataOPAPolicy = "OPAPolicy"
)
View Source
const (
	KDataOpenAPI = "OpenAPIData"
)
View Source
const (
	KDataSecurity = "Security"
)

Variables

View Source
var (
	LanaiAppConfig     = &LanaiModule{Name: "appconfig", InitPackage: "appconfig/init"}
	LanaiRedis         = &LanaiModule{Name: "redis", InitPackage: "redis"}
	LanaiSecurity      = &LanaiModule{Name: "security", InitPackage: "security"}
	LanaiResServer     = &LanaiModule{Name: "resserver", InitPackage: "security/config/resserver"}
	LanaiWeb           = &LanaiModule{Name: "web", InitPackage: "web/init"}
	LanaiConsul        = &LanaiModule{Name: "consul", InitPackage: "consul/init"}
	LanaiConsulSD      = &LanaiModule{Name: "consulsd", InitPackage: "discovery/consulsd"}
	LanaiVault         = &LanaiModule{Name: "vault", InitPackage: "vault/init"}
	LanaiActuator      = &LanaiModule{Name: "actuator", InitPackage: "actuator/init"}
	LanaiSwagger       = &LanaiModule{Name: "swagger", InitPackage: "swagger"}
	LanaiTracing       = &LanaiModule{Name: "tracing", InitPackage: "tracing/init"}
	LanaiData          = &LanaiModule{Name: "data", InitPackage: "data/init"}
	LanaiCockroach     = &LanaiModule{Name: "cockroach", InitPackage: "cockroach"}
	LanaiKafka         = &LanaiModule{Name: "kafka", InitPackage: "kafka"}
	LanaiHttpClient    = &LanaiModule{Name: "httpclient", InitPackage: "integrate/httpclient"}
	LanaiSecurityScope = &LanaiModule{Name: "scope", InitPackage: "integrate/security/scope"}
	LanaiDSync         = &LanaiModule{Name: "dsync", InitPackage: "dsync"}
	LanaiOPA           = &LanaiModule{Name: "opa", InitPackage: "opa/init", Alias: "opainit"}
)

Lanai Modules Definitions

View Source
var DefaultOption = Option{
	DefaultRegenMode: RegenModeIgnore,
	Components: Components{
		Contract: Contract{},
		Security: Security{
			Authentication: Authentication{
				Method: AuthOAuth2,
			},
			Access: Access{
				Preset: AccessPresetFreestyle,
			},
		},
	},
}

LanaiModuleDependencies some module requires other modules to be initialized. The dependency list doesn't include always-required modules like "bootstrap" and "appconfig"

SupportedLanaiModules is all lanai modules by groups

Functions

func FormatFile

func FormatFile(path string, typeHint FileType) error

FormatFile clean up given file with file path, replace the content with prettier format. When type hint is unknown, this function uses file's extension to deduct file type

func GenerateFileFromTemplate

func GenerateFileFromTemplate(gc GenerationContext, template *template.Template) error

func GenerateFiles

func GenerateFiles(ctx context.Context, opts ...Options) error

func LoadTemplates

func LoadTemplates(fsys fs.FS, opts ...TemplateOptions) (tmpl *template.Template, err error)

func ResolveEnabledLanaiModules

func ResolveEnabledLanaiModules(modules ...*LanaiModule) (ret utils.StringSet)

ResolveEnabledLanaiModules resolve all modules required by given modules, based on dependencies

func WithComponents

func WithComponents(comps Components) func(o *Option)

WithComponents defines what to generate and their settings

func WithProject

func WithProject(project Project) func(o *Option)

WithProject general information about the project to generate

func WithRegenRules

func WithRegenRules(rules RegenRules, defaultMode RegenMode) func(o *Option)

WithRegenRules Set re-generation rules, Fallback to default mode if no rules matches the output file

func WithTemplateFS

func WithTemplateFS(templateFS fs.FS) func(o *Option)

Types

type APIGroup

type APIGroup struct {
	Option
}

func (APIGroup) CustomizeData

func (g APIGroup) CustomizeData(data GenerationData) error

func (APIGroup) CustomizeTemplate

func (g APIGroup) CustomizeTemplate() (TemplateOptions, error)

func (APIGroup) Generators

func (g APIGroup) Generators(opts ...GeneratorOptions) ([]Generator, error)

func (APIGroup) Name

func (g APIGroup) Name() string

func (APIGroup) Order

func (g APIGroup) Order() int

type Access

type Access struct {
	Preset AccessPreset
}

type AccessPreset

type AccessPreset string
const (
	AccessPresetFreestyle AccessPreset = `freestyle`
	AccessPresetOPA       AccessPreset = `opa`
)

type ApiGenerator

type ApiGenerator struct {
	// contains filtered or unexported fields
}

func (*ApiGenerator) Generate

func (g *ApiGenerator) Generate(ctx context.Context, tmplDesc TemplateDescriptor) error

func (*ApiGenerator) Order

func (g *ApiGenerator) Order() int

type ApiOption

type ApiOption struct {
	GeneratorOption
	Matcher TemplateMatcher
	Prefix  string
	Order   int
}

type ApiVerOption

type ApiVerOption struct {
	GeneratorOption
}

type ApiVersionGenerator

type ApiVersionGenerator struct {
	// contains filtered or unexported fields
}

ApiVersionGenerator generate 1 file per API version, based on OpenAPI specs

func (*ApiVersionGenerator) Generate

func (g *ApiVersionGenerator) Generate(ctx context.Context, tmplDesc TemplateDescriptor) error

type Authentication

type Authentication struct {
	Method AuthenticationMethod
}

type AuthenticationMethod

type AuthenticationMethod string
const (
	AuthNone   AuthenticationMethod = `none`
	AuthOAuth2 AuthenticationMethod = `oauth2`
)

type CleanupGenerator

type CleanupGenerator struct {
	// contains filtered or unexported fields
}

CleanupGenerator will read delete.*.tmpl files, and delete any generated code that matches the regex defined in them, as well as any empty directories after the deletion

func (*CleanupGenerator) Generate

func (g *CleanupGenerator) Generate(_ context.Context, tmplDesc TemplateDescriptor) error

func (*CleanupGenerator) Order

func (g *CleanupGenerator) Order() int

type CleanupOption

type CleanupOption struct {
	GeneratorOption
	Order int
}

type Components

type Components struct {
	Contract Contract
	Security Security
}

type Contract

type Contract struct {
	Path   string
	Naming ContractNaming
}

type ContractNaming

type ContractNaming struct {
	RegExps map[string]string
}

type DirOption

type DirOption struct {
	GeneratorOption
	Matcher        TemplateMatcher
	OutputResolver TemplateOutputResolver
}

type DirectoryGenerator

type DirectoryGenerator struct {
	// contains filtered or unexported fields
}

func (*DirectoryGenerator) Generate

func (d *DirectoryGenerator) Generate(ctx context.Context, tmplDesc TemplateDescriptor) error

type FileGenerator

type FileGenerator struct {
	// contains filtered or unexported fields
}

FileGenerator is a basic generator that generates 1 file based on the templatePath being used

func (*FileGenerator) Generate

func (g *FileGenerator) Generate(ctx context.Context, tmplDesc TemplateDescriptor) error

func (*FileGenerator) Order

func (g *FileGenerator) Order() int

type FileOption

type FileOption struct {
	GeneratorOption
	Matcher        TemplateMatcher
	OutputResolver TemplateOutputResolver
	Prefix         string
	Order          int
}

type FileType

type FileType string
const (
	FileTypeUnknown FileType = ""
	FileTypeGo      FileType = "go"
	FileTypeYaml    FileType = "yaml"
	FileTypeRego    FileType = "rego"
)

type GenerationContext

type GenerationContext struct {
	// contains filtered or unexported fields
}

func NewGenerationContext

func NewGenerationContext(templatePath string, filename string, regenMode RegenMode, model interface{}) *GenerationContext

type GenerationData

type GenerationData map[string]interface{}

func NewGenerationData

func NewGenerationData(p *Project) GenerationData

func (GenerationData) ProjectMetadata

func (d GenerationData) ProjectMetadata() *ProjectMetadata

type Generator

type Generator interface {
	// Generate files based on given template.
	// Provided template could be
	// 	- Path to a loaded template file
	// 	- Directory path in the template FS
	//
	// Generator's implementation decide what to with the given template, including:
	// 	- Creating directories
	//  - Generating single file
	//  - Generating multiple files
	Generate(ctx context.Context, tmplDesc TemplateDescriptor) error
}

Generator interface for various code generation

type GeneratorOption

type GeneratorOption struct {
	Option
	Template *template.Template
	Data     GenerationData
}

type GeneratorOptions

type GeneratorOptions func(opt *GeneratorOption)

type Generators

type Generators struct {
	Option
	// contains filtered or unexported fields
}

func NewGenerators

func NewGenerators(opts ...Options) Generators

func (*Generators) Generate

func (g *Generators) Generate(ctx context.Context) error

type GoSourceFormatter

type GoSourceFormatter struct{}

func (GoSourceFormatter) Format

func (f GoSourceFormatter) Format(in []byte) ([]byte, error)

type Group

type Group interface {
	order.Ordered

	// Name unique identifier of the group
	Name() string

	// CustomizeTemplate is used to customize *template.Template for group specific need
	CustomizeTemplate() (TemplateOptions, error)

	// CustomizeData customize data and load group specific data. All data is shared with other groups.
	// This function of all groups are called in order before any invocation of their Generators().
	// The results are propagate to other groups and served to Generators() for constructing generators.
	// Important: For each top-level component in Data should be owned by the group that generating it.
	// 			  Other groups can read it but should not change or replace it.
	//			  The exception is "ProjectMetadata" object, which all groups can add to it
	CustomizeData(data GenerationData) error

	// Generators prepare generators with proper generator list based on options. Group can decide the group is not
	// applicable, in such case, Group can return empty list without error
	// The returned generators should be sorted.
	Generators(opts ...GeneratorOptions) ([]Generator, error)
}

Group is a collection of concrete generator instances that work together to generate set of files and directories for one particular components. Group should be always named and ordered

type LanaiModule

type LanaiModule struct {
	Name        string
	Alias       string
	InitPackage string
}

func (LanaiModule) ImportAlias

func (m LanaiModule) ImportAlias() string

ImportAlias return alias for importing. Return empty if no alias is needed

func (LanaiModule) ImportPath

func (m LanaiModule) ImportPath(root string) string

func (LanaiModule) Ref

func (m LanaiModule) Ref() string

Ref returns package reference when using the package in code, always not empty The result could be either the name or alias. The behavior should match ImportAlias

type LanaiModuleGroup

type LanaiModuleGroup []*LanaiModule

func (LanaiModuleGroup) FilterByName

func (g LanaiModuleGroup) FilterByName(filter utils.StringSet) []*LanaiModule

type LanaiModules

type LanaiModules struct {
	Basic       LanaiModuleGroup
	Web         LanaiModuleGroup
	Data        LanaiModuleGroup
	Integration LanaiModuleGroup
	Security    LanaiModuleGroup
	Others      LanaiModuleGroup
}

func (LanaiModules) Modules

func (m LanaiModules) Modules() (ret LanaiModuleGroup)

type OPAPolicyData

type OPAPolicyData struct {
	APIPackage string
}

type OPAPolicyGroup

type OPAPolicyGroup struct {
	Option
}

OPAPolicyGroup generate placeholder for OPA policies. This group is not responsible to setup security init source code. SecurityGroup would do that

func (OPAPolicyGroup) CustomizeData

func (g OPAPolicyGroup) CustomizeData(data GenerationData) error

func (OPAPolicyGroup) CustomizeTemplate

func (g OPAPolicyGroup) CustomizeTemplate() (TemplateOptions, error)

func (OPAPolicyGroup) Generators

func (g OPAPolicyGroup) Generators(opts ...GeneratorOptions) ([]Generator, error)

func (OPAPolicyGroup) Name

func (g OPAPolicyGroup) Name() string

func (OPAPolicyGroup) Order

func (g OPAPolicyGroup) Order() int

type Option

type Option struct {
	// Project general project information
	Project Project

	// Components defines what to generate and their settings
	Components Components

	// TemplateFS filesystem containing templates. Could be embed.FS or os.DirFS
	TemplateFS fs.FS

	// DefaultRegenMode default output file operation mode during re-generation
	DefaultRegenMode RegenMode

	// RegenRules rules of output file operation mode during re-generation
	RegenRules RegenRules
}

type Options

type Options func(opt *Option)

type Project

type Project struct {
	Name        string
	Module      string
	Description string
	Port        int
	ContextPath string
}

type ProjectGroup

type ProjectGroup struct {
	Option
}

func (ProjectGroup) CustomizeData

func (g ProjectGroup) CustomizeData(data GenerationData) error

func (ProjectGroup) CustomizeTemplate

func (g ProjectGroup) CustomizeTemplate() (TemplateOptions, error)

func (ProjectGroup) Generators

func (g ProjectGroup) Generators(opts ...GeneratorOptions) ([]Generator, error)

func (ProjectGroup) Name

func (g ProjectGroup) Name() string

func (ProjectGroup) Order

func (g ProjectGroup) Order() int

type ProjectMetadata

type ProjectMetadata struct {
	Project
	EnabledModules utils.StringSet
}

type RegenMode

type RegenMode string

RegenMode file operation mode when re-generating.

const (
	RegenModeIgnore    RegenMode = "ignore"
	RegenModeReference RegenMode = "reference"
	RegenModeOverwrite RegenMode = "overwrite"
)

type RegenRule

type RegenRule struct {
	// Pattern wildcard pattern of output file path
	Pattern string
	// Mode regeneration mode on matched output files in case of changes. (ignore, overwrite, reference, etc.)
	Mode RegenMode
}

RegenRule file operation rules during re-generation

type RegenRules

type RegenRules []RegenRule

type RegoFormatter

type RegoFormatter struct{}

func (RegoFormatter) Format

func (f RegoFormatter) Format(in []byte) ([]byte, error)

type Security

type Security struct {
	Authentication Authentication
	Access         Access
}

type SecurityGroup

type SecurityGroup struct {
	Option
}

func (SecurityGroup) CustomizeData

func (g SecurityGroup) CustomizeData(data GenerationData) error

func (SecurityGroup) CustomizeTemplate

func (g SecurityGroup) CustomizeTemplate() (TemplateOptions, error)

func (SecurityGroup) Generators

func (g SecurityGroup) Generators(opts ...GeneratorOptions) ([]Generator, error)

func (SecurityGroup) Name

func (g SecurityGroup) Name() string

func (SecurityGroup) Order

func (g SecurityGroup) Order() int

type TemplateDescriptor

type TemplateDescriptor struct {
	Path     string
	FileInfo fs.FileInfo
}

type TemplateFuncProvider

type TemplateFuncProvider func(funcMap template.FuncMap) (template.FuncMap, error)

type TemplateOption

type TemplateOption struct {
	Pattern       string
	FuncProviders []TemplateFuncProvider
	PreloadHooks  []TemplatePreHook
	PostHooks     []TemplatePostHook
}

type TemplateOptions

type TemplateOptions func(opt *TemplateOption)

type TemplateOutputDescriptor

type TemplateOutputDescriptor struct {
	Path string
}

type TemplateOutputResolver

type TemplateOutputResolver interface {
	Resolve(ctx context.Context, tmplDesc TemplateDescriptor, data GenerationData) (TemplateOutputDescriptor, error)
}

type TemplateOutputResolverFunc

type TemplateOutputResolverFunc func(ctx context.Context, tmplDesc TemplateDescriptor, data GenerationData) (TemplateOutputDescriptor, error)

func (TemplateOutputResolverFunc) Resolve

type TemplatePostHook

type TemplatePostHook func(tmpl *template.Template) (*template.Template, error)

type TemplatePreHook

type TemplatePreHook func(tmpl *template.Template, tmplPaths []string) (*template.Template, []string, error)

type TextFormatter

type TextFormatter interface {
	Format(in []byte) ([]byte, error)
}

type YamlFormatter

type YamlFormatter struct{}

YamlFormatter format single yaml document text (without "---"). Add empty line between each top level component if root document is an object or list

func (YamlFormatter) Format

func (f YamlFormatter) Format(in []byte) ([]byte, error)

Directories

Path Synopsis
go

Jump to

Keyboard shortcuts

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