codegen

package module
v0.0.0-...-6e85089 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2021 License: MIT Imports: 9 Imported by: 3

README

Intro

基于 go template 的代码生成工具

Feature

  • 制定模板目录,支持多种语言输出

Usage

see examples/

Appendix 1.模板文件名命名约定

<dir>/<file_name>.<template_type>.<ext>.tpl

每个 part 的两端空格将被 trim 掉

  • <dir>: 可以是任意目录结构,生成的文件将保持相同结构
  • <file_name> 生成文件名
  • <template_type> 不同的模板类型,模板生成器将会传入对应的模板对象
  • <ext> 生成文件名的拓展名

visitor/a.type.go.tpl -> 将会生成 visitor/a.go

<template_type>有这样的约定


<template_type> ::= <type_name> (<object_count>)?;

<object_count> ::= '[' (<INTEGER> | '*') ']';

<INTEGER> ::= { num > 1 }

example: a.type[1].go.tpl 或者 a.type[*].go.tpl

template_type可以为数组时,可以显式通过[]指定每次执行模板时,需要传入的对象数量,*代表全部传递。

生成的文件名将是<file_name>.<ext>

<file_name>的约定

<file_name>支持 go template 语法,提供的 object 格式如下

type TemplateContext struct {
  T <type> // template_type对象, 可能为数组或对象
  Index int // template_type为数组时,生成到当前的数组
}

// Head returns head of T, if T is slice
func (*TemplateContext) Head() interface{}

example {{ .T.name }}.type[1].go.tpl,将会生成

Expression.go
ConstExpr.go
...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BundleConfig

type BundleConfig struct {
	Output   OutputConfig
	Template TemplateConfig
}

BundleConfig ...

type FuncsLoader

type FuncsLoader interface {
	LoadFuncs() (template.FuncMap, error)
}

FuncsLoader load funcMap lazily

type FuncsLoaderFunc

type FuncsLoaderFunc func() (template.FuncMap, error)

FuncsLoaderFunc ...

func (FuncsLoaderFunc) LoadFuncs

func (f FuncsLoaderFunc) LoadFuncs() (template.FuncMap, error)

LoadFuncs ...

type Iterator

type Iterator interface {
	Iterate(handler func(val interface{}, idx int) error) error
}

Iterator iterator

type IteratorFunc

type IteratorFunc func() func() IteratorFuncItem

IteratorFunc func impls Iterator

func (IteratorFunc) Iterate

func (f IteratorFunc) Iterate(handler func(val interface{}, idx int) error) error

Iterate iterate the Iterator

type IteratorFuncItem

type IteratorFuncItem struct {
	Valid bool
	Val   interface{}
}

IteratorFuncItem item for IteratorFunc

type OutputConfig

type OutputConfig struct {
	Dir string `cli:"usage=output dir" validate:"required"`
}

OutputConfig config for output

type StoreLoader

type StoreLoader interface {
	LoadStore() (TypeStore, error)
}

StoreLoader load store lazily

type StoreLoaderFunc

type StoreLoaderFunc func() (TypeStore, error)

StoreLoaderFunc ...

func (StoreLoaderFunc) LoadStore

func (l StoreLoaderFunc) LoadStore() (TypeStore, error)

LoadStore ...

type TemplateConfig

type TemplateConfig struct {
	Dir string `cli:"usage=template dir" validate:"required"`
}

TemplateConfig config for template

type TemplateContext

type TemplateContext struct {
	T     interface{}
	Index int
}

TemplateContext context for FileName template

func (*TemplateContext) Head

func (tc *TemplateContext) Head() interface{}

Head return first element of T if T is slice otherwise, return T

type Tool

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

Tool tool

func (*Tool) Run

func (t *Tool) Run() error

Run runs tool

type ToolBuilder

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

ToolBuilder for Driver

func NewToolBuilder

func NewToolBuilder() *ToolBuilder

NewToolBuilder new ToolBuilder

func (*ToolBuilder) Build

func (b *ToolBuilder) Build() (*Tool, error)

Build builds Driver

func (*ToolBuilder) WithConfig

func (b *ToolBuilder) WithConfig(conf ToolConfig) *ToolBuilder

WithConfig builder with config

func (*ToolBuilder) WithFuncsLoader

func (b *ToolBuilder) WithFuncsLoader(loader FuncsLoader) *ToolBuilder

WithFuncsLoader funcs loader

func (*ToolBuilder) WithStoreLoader

func (b *ToolBuilder) WithStoreLoader(loader StoreLoader) *ToolBuilder

WithStoreLoader store loader

type ToolConfig

type ToolConfig struct {
	BundleConfig `mapstructure:",squash"`

	Extras []BundleConfig
}

ToolConfig config for Tool

type TypeStore

type TypeStore interface {
	// GetObjectIterator get iterator for object
	GetObjectIterator(templateType string, unit int) (Iterator, error)
}

TypeStore contains type for template context

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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