Documentation ¶
Overview ¶
Package easygen is an easy to use universal code/text generator library.
It can be used as a text or html generator for arbitrary purposes with arbitrary data and templates.
It can be used as a code generator, or anything that is structurally repetitive. Some command line parameter handling code generator are provided as examples, including the Go's built-in flag package, and the viper & cobra package.
Many examples have been provided to showcase its functionality, and different ways to use it.
Index ¶
- Variables
- func Execute(t Template, wr io.Writer, fileNameT string, m EgData) error
- func FuncDefs() template.FuncMap
- func IsExist(fileName string) bool
- func Process(t Template, wr io.Writer, fileNames ...string) error
- func Process0(t Template, wr io.Writer, strTempl string, fileNames ...string) error
- func Process1(t Template, wr io.Writer, fileNameTempl string, fileName string) error
- func Process2(t Template, wr io.Writer, fileNameTempl string, fileNames ...string) error
- type EgBase
- type EgData
- type FuncMap
- type Options
- type Template
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Opts = Options{ExtYaml: ".yaml", ExtJson: ".json", ExtTmpl: ".tmpl"}
Opts holds the actual values from the command line parameters
Functions ¶
func Execute ¶
Execute will execute the Template on the given data map `m`.
Example ¶
for standalone test, change package to `main` and the next func def to, func main() {
package main import ( "fmt" "os" "strings" "github.com/go-easygen/easygen" "github.com/go-easygen/easygen/egCal" "github.com/go-easygen/easygen/egVar" ) type variable struct { Name string } // for standalone test, change package to `main` and the next func def to, // func main() { func main() { easygen.Opts.Debug = 1 tmpl0 := easygen.NewTemplate().Customize() tmpl := tmpl0.Funcs(easygen.FuncDefs()).Funcs(egVar.FuncDefs()).Funcs(egCal.FuncDefs()) // define driving data of any tye v0 := variable{"some-init-method"} // https://godoc.org/github.com/go-easygen/easygen#Execute // provide full template file name with extension easygen.Execute(tmpl, os.Stdout, "test/var0.tmpl", v0) // Demo of using driving data of pure slice/array v1 := []string{"red", "blue", "white"} easygen.Execute(tmpl, os.Stdout, "test/list00.tmpl", v1) // Demo output to string var b strings.Builder easygen.Execute(tmpl, &b, "test/list00f.tmpl", v1) fmt.Print(b.String()) } // To show the full code in GoDoc type dummyExecute struct { }
Output: Input: "some-init-method" Output 1: "SomeInitMethod" Output 2: "SOME_INIT_METHOD" The colors are: red, blue, white, . The colors are: red, blue, white.
func Process ¶
Process will process the standard easygen input: the `fileName` is for both template and data file name, and produce output from the template according to the corresponding driving data. Process() is using the V3's calling convention and *only* works properly in V4+ in the case that there is only one fileName passed to it. If need to pass more files, use Process2() instead.
Example ¶
for standalone test, change package to `main` and the next func def to, func main() {
package main import ( "os" "github.com/go-easygen/easygen" "github.com/go-easygen/easygen/egVar" ) // for standalone test, change package to `main` and the next func def to, // func main() { func main() { tmpl0 := easygen.NewTemplate().Customize() tmpl := tmpl0.Funcs(easygen.FuncDefs()).Funcs(egVar.FuncDefs()) tmplFileName := "test/var0" easygen.Process(tmpl, os.Stdout, tmplFileName) easygen.Process2(tmpl, os.Stdout, tmplFileName, tmplFileName) // To use Execute(), TemplateFileName has to be exact m := easygen.ReadDataFile(tmplFileName + ".yaml") easygen.Execute(tmpl, os.Stdout, tmplFileName+".tmpl", m) } // To show the full code in GoDoc type dummy struct { }
Output: Input: "some-init-method" Output 1: "SomeInitMethod" Output 2: "SOME_INIT_METHOD" Input: "some-init-method" Output 1: "SomeInitMethod" Output 2: "SOME_INIT_METHOD" Input: "some-init-method" Output 1: "SomeInitMethod" Output 2: "SOME_INIT_METHOD"
func Process0 ¶
Process0 will produce output according to the driving data *without* a template file, using the string from strTempl as the template
Example ¶
for standalone test, change package to `main` and the next func def to, func main() {
package main import ( "os" "github.com/go-easygen/easygen" "github.com/go-easygen/easygen/egCal" "github.com/go-easygen/easygen/egVar" ) // for standalone test, change package to `main` and the next func def to, // func main() { func main() { tmpl0 := easygen.NewTemplate().Customize() tmpl := tmpl0.Funcs(easygen.FuncDefs()).Funcs(egVar.FuncDefs()).Funcs(egCal.FuncDefs()) easygen.Process0(tmpl, os.Stdout, "{{.Name}}: {{clk2uc .Name}} {{clk2ss .Name}}\n"+ "Cal: {{add 2 3}}, {{multiply 2 3}}, {{subtract 9 2}}, {{divide 24 3}}\n", "test/var0") } // To show the full code in GoDoc type dummy0 struct { }
Output: some-init-method: SomeInitMethod SOME_INIT_METHOD Cal: 5, 6, 7, 8
Example (List0StrTemplate) ¶
Test string template with list0 data
package main import ( "os" "text/template" "github.com/go-easygen/easygen" ) var tmpl *template.Template func main() { // Equivalent testing on commandline: // easygen -ts '{{range .Colors}}{{.}}, {{end}}' test/list0 easygen.Process0(tmpl, os.Stdout, "{{range .Colors}}{{.}}, {{end}}", "test/list0") }
Output: red, blue, white,
Example (Split0) ¶
Test the string split function in template
package main import ( "os" "text/template" "github.com/go-easygen/easygen" ) var tmpl *template.Template func main() { // Equivalent testing on commandline: // easygen -ts '{{split .Colorlist}}' test/list0 easygen.Process0(tmpl, os.Stdout, `{{split .Colorlist}}`, "test/list0") }
Output: [red blue white]
Example (Split1) ¶
Test the string split function in template again
package main import ( "os" "text/template" "github.com/go-easygen/easygen" ) var tmpl *template.Template func main() { // Equivalent testing on commandline: // easygen -ts '{{range ... {{end}}' test/list0 easygen.Process0(tmpl, os.Stdout, `{{range (split .Colorlist)}}{{.}} {{end}}`, "test/list0") }
Output: red blue white
Example (StringsCmp) ¶
Test string comparison in template
package main import ( "os" "text/template" "github.com/go-easygen/easygen" ) var tmpl *template.Template func main() { // Equivalent testing on commandline: // easygen -ts '{{The {{if ... {{end}}.' test/strings0 easygen.Process0(tmpl, os.Stdout, `The {{if eq .StrTest "-AB-axxb- HTML Html html"}}eq says Yea{{else}}eq says Nay{{end}} but {{if eqf .StrTest "-AB-axxb- HTML Html html"}}eqf says Yea{{else}}eqf says Nay{{end}}.`, "test/strings0") }
Output: The eq says Nay but eqf says Yea.
func Process1 ¶
Process1 will process a *single* case where both template and data file names are given, and produce output according to the given template and driving data files, specified via fileNameTempl and fileName respectively. fileNameTempl is not a comma-separated string, but for a single template file.
func Process2 ¶
Process2 will process the case that *both* template and data file names are given, and produce output according to the given template and driving data files, specified via fileNameTempl and fileNames respectively. fileNameTempl can be a comma-separated string giving many template files
Example (Html) ¶
Test HTML template with list1 data
package main import ( "os" "text/template" "github.com/go-easygen/easygen" ) var tmpl *template.Template func main() { // Equivalent testing on commandline: // easygen -tf test/list1HTML test/list1 easygen.Process2(tmpl, os.Stdout, "test/list1HTML", "test/list1") }
Output: The quoted colors are: "red", "blue", "white", .
Types ¶
type EgBase ¶
EgBase -- EasyGen Template Base
func NewTemplate ¶
func NewTemplate() *EgBase
NewTemplate returns a new Template for this specific package.
type EgData ¶
type EgData interface{}
EgData -- EasyGen driven Data
func ReadDataFile ¶
ReadDataFile reads in the driving data from the given file, which can be optionally without the defined extension
Example ¶
for standalone test, change package to `main` and the next func def to, func main() {
package main import ( "os" "github.com/go-easygen/easygen" "github.com/go-easygen/easygen/egVar" ) func main() { tmplFileName := "test/var0" tmpl0 := easygen.NewTemplate().Customize() tmpl := tmpl0.Funcs(easygen.FuncDefs()).Funcs(egVar.FuncDefs()) // To use Execute(), TemplateFileName has to be exact tmplFileNameFull := tmplFileName + ".tmpl" m := easygen.ReadDataFile(tmplFileName) easygen.Execute(tmpl, os.Stdout, tmplFileNameFull, m) easygen.Opts.Debug = 0 m = easygen.ReadDataFile(tmplFileName + ".yaml") easygen.Execute(tmpl, os.Stdout, tmplFileNameFull, m) tmplFileName = "test/list0j" tmplFileNameFull = tmplFileName + ".tmpl" m = easygen.ReadDataFile(tmplFileName) easygen.Execute(tmpl, os.Stdout, tmplFileNameFull, m) m = easygen.ReadDataFile(tmplFileName + ".json") easygen.Execute(tmpl, os.Stdout, tmplFileNameFull, m) }
Output: Input: "some-init-method" Output 1: "SomeInitMethod" Output 2: "SOME_INIT_METHOD" Input: "some-init-method" Output 1: "SomeInitMethod" Output 2: "SOME_INIT_METHOD" The colors are: red, blue, white, . The colors are: red, blue, white, .
func ReadJsonFile ¶
ReadJsonFile reads given JSON file as EgData
func ReadYamlFile ¶
ReadYamlFile reads given YAML file as EgData
type FuncMap ¶
type FuncMap map[string]interface{}
The FuncMap defined in easygen will shield the dependency of either text or html template, giving an implementation agnostic abstraction that will works for both cases.
type Options ¶
type Options struct { TemplateStr string // template string (in text) ExtYaml string // `extension` of yaml file ExtJson string // `extension` of json file ExtTmpl string // `extension` of template file Debug int // debugging `level` }
The Options struct defines the structure to hold the commandline values
type Template ¶
type Template interface { Execute(wr io.Writer, data interface{}) error ExecuteTemplate(wr io.Writer, name string, data interface{}) error Parse(text string) (*template.Template, error) ParseFiles(filenames ...string) (*template.Template, error) Name() string }
The Template defines the common ground for both text and html Template