Documentation ¶
Index ¶
- Constants
- Variables
- func FormatFile(path string, typeHint FileType) error
- func GenerateFileFromTemplate(gc GenerationContext, template *template.Template) error
- func GenerateFiles(ctx context.Context, opts ...Options) error
- func LoadTemplates(fsys fs.FS, opts ...TemplateOptions) (tmpl *template.Template, err error)
- func ResolveEnabledLanaiModules(modules ...*LanaiModule) (ret utils.StringSet)
- func WithComponents(comps Components) func(o *Option)
- func WithProject(project Project) func(o *Option)
- func WithRegenRules(rules RegenRules, defaultMode RegenMode) func(o *Option)
- func WithTemplateFS(templateFS fs.FS) func(o *Option)
- type APIGroup
- type Access
- type AccessPreset
- type ApiGenerator
- type ApiOption
- type ApiVerOption
- type ApiVersionGenerator
- type Authentication
- type AuthenticationMethod
- type CleanupGenerator
- type CleanupOption
- type Components
- type Contract
- type ContractNaming
- type DirOption
- type DirectoryGenerator
- type FileGenerator
- type FileOption
- type FileType
- type GenerationContext
- type GenerationData
- type Generator
- type GeneratorOption
- type GeneratorOptions
- type Generators
- type GoSourceFormatter
- type Group
- type LanaiModule
- type LanaiModuleGroup
- type LanaiModules
- type OPAPolicyData
- type OPAPolicyGroup
- type Option
- type Options
- type Project
- type ProjectGroup
- type ProjectMetadata
- type RegenMode
- type RegenRule
- type RegenRules
- type RegoFormatter
- type Security
- type SecurityGroup
- type TemplateDescriptor
- type TemplateFuncProvider
- type TemplateMatcher
- type TemplateOption
- type TemplateOptions
- type TemplateOutputDescriptor
- type TemplateOutputResolver
- type TemplateOutputResolverFunc
- type TemplatePostHook
- type TemplatePreHook
- type TextFormatter
- type YamlFormatter
Constants ¶
const ( KDataProjectName = "ProjectName" KDataRepository = "Repository" KDataProject = "Project" KDataLanaiModules = "LanaiModules" )
Global Keys in template's context data as map
const ( GroupOrderAPI = iota * 100 GroupOrderOPAPolicy GroupOrderSecurity GroupOrderProject )
const (
KDataOPAPolicy = "OPAPolicy"
)
const (
KDataOpenAPI = "OpenAPIData"
)
const (
KDataSecurity = "Security"
)
Variables ¶
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
var DefaultOption = Option{ DefaultRegenMode: RegenModeIgnore, Components: Components{ Contract: Contract{}, Security: Security{ Authentication: Authentication{ Method: AuthOAuth2, }, Access: Access{ Preset: AccessPresetFreestyle, }, }, }, }
var LanaiModuleDependencies = map[*LanaiModule][]*LanaiModule{ LanaiSecurity: {LanaiRedis}, LanaiResServer: {LanaiSecurity}, LanaiConsulSD: {LanaiConsul}, LanaiActuator: {LanaiWeb}, LanaiSwagger: {LanaiWeb}, LanaiCockroach: {LanaiData}, LanaiSecurityScope: {LanaiSecurity}, LanaiDSync: {LanaiConsul}, }
LanaiModuleDependencies some module requires other modules to be initialized. The dependency list doesn't include always-required modules like "bootstrap" and "appconfig"
var SupportedLanaiModules = LanaiModules{ Basic: []*LanaiModule{LanaiAppConfig, LanaiConsul, LanaiVault, LanaiRedis, LanaiTracing}, Web: []*LanaiModule{LanaiWeb, LanaiActuator, LanaiSwagger}, Data: []*LanaiModule{LanaiData, LanaiCockroach}, Integration: []*LanaiModule{LanaiConsulSD, LanaiHttpClient, LanaiSecurityScope, LanaiKafka}, Security: []*LanaiModule{LanaiSecurity, LanaiResServer, LanaiOPA}, Others: []*LanaiModule{LanaiDSync}, }
SupportedLanaiModules is all lanai modules by groups
Functions ¶
func FormatFile ¶
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 LoadTemplates ¶
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 ¶
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 ¶
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)
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 Contract ¶
type Contract struct { Path string Naming ContractNaming }
type ContractNaming ¶
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 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
type GoSourceFormatter ¶
type GoSourceFormatter struct{}
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 ¶
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 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 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{}
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 TemplateFuncProvider ¶
type TemplateMatcher ¶
type TemplateMatcher cmdutils.ChainableMatcher[TemplateDescriptor]
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 ¶
func (fn TemplateOutputResolverFunc) Resolve(ctx context.Context, tmplDesc TemplateDescriptor, data GenerationData) (TemplateOutputDescriptor, error)
type TemplatePostHook ¶
type TemplatePreHook ¶
type TextFormatter ¶
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