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 Add(b, a int) int
- func ArgsA(vs ...interface{}) []interface{}
- func ArgsM(kvs ...interface{}) (map[string]interface{}, error)
- func Coalesce(s ...interface{}) string
- func Date(fmt string) string
- func Execute(t Template, wr io.Writer, fileNameT string, m EgData) error
- func Execute0(t Template, wr io.Writer, strTempl string, m EgData) error
- func FuncDefs() template.FuncMap
- func Indent(spaces int, v string) string
- func IsExist(fileName string) bool
- func Iterate(ic ...string) []int
- func Minus(b, a int) int
- func Minus1(n int) int
- func PIndent(spaces int, v string) string
- 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
- func Quote4shell(s string) string
- func RegexpFindAllString(s string, regExp string, n int) []string
- func RegexpFindAllStringIndex(s string, regExp string, n int) [][]int
- func RegexpFindAllStringSubmatch(s string, regExp string, n int) [][]string
- func RegexpFindAllStringSubmatchIndex(s string, regExp string, n int) [][]int
- func RegexpFindString(s string, regExp string) string
- func RegexpFindStringIndex(s string, regExp string) (loc []int)
- func RegexpFindStringSubmatch(s string, regExp string) []string
- func RegexpFindStringSubmatchIndex(s string, regExp string) []int
- func RegexpMatchString(s string, regExp string) bool
- func RegexpReplaceAllLiteralString(src, regExp string, repl string) string
- func RegexpReplaceAllString(src, regExp string, repl string) string
- func RegexpReplaceAllStringFunc(src string, regExp string, repl func(string) string) string
- func RegexpSplit(s string, regExp string, n int) []string
- func Substr(a string, nums ...interface{}) (string, error)
- func Timestamp(s ...string) (string, error)
- type EgBase
- type EgData
- type EgKey
- 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 ArgsA ¶
func ArgsA(vs ...interface{}) []interface{}
ArgsA creates an array slice from the given arguments
func Coalesce ¶
func Coalesce(s ...interface{}) string
Coalesce function takes two or more string arguments and returns the first argument that is not empty. The result is empty only if all the arguments are empty.
func Execute ¶
Execute will execute the Template from fileNameT 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/egFilePath" "github.com/go-easygen/easygen/egVar" ) func main() { easygen.Opts.Debug = 1 tmpl0 := easygen.NewTemplate().Customize() tmpl := tmpl0.Funcs(easygen.FuncDefs()).Funcs(egFilePath.FuncDefs()). Funcs(egVar.FuncDefs()).Funcs(egCal.FuncDefs()) // define driving data v0 := easygen.EgData{"Name": "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 slice/array v1 := easygen.EgData{"v": []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()) }
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 Execute0 ¶
Execute0 will execute the Template given as strTempl with the given data map `m` (i.e., no template file and no data file). It parses text template strTempl then applies it to to the specified data object m, and writes the output to wr. If an error occurs executing the template or writing its output, execution stops, but partial results may already have been written to the output writer. A template may be executed safely in parallel, although if parallel executions share a Writer the output may be interleaved.
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" ) // for standalone test, change package to `main` and the next func def to, // func main() { func main() { easygen.Opts.Debug = 1 tmpl := easygen.NewTemplate().Funcs(easygen.FuncDefs()) // define driving data v0 := easygen.EgData{"v": "some-init-method"} // https://godoc.org/github.com/go-easygen/easygen#Execute0 // provide template string, not file name easygen.Execute0(tmpl, os.Stdout, "{{stringsToUpper .v}}\n", v0) // Demo of using driving data of slice/array v1 := easygen.EgData{"v": []string{"red", "blue", "white"}} easygen.Execute0(tmpl, os.Stdout, "The colors are: {{range .v}}{{.}}, {{end}}.\n", v1) // Demo output to string var b strings.Builder easygen.Execute0(tmpl, &b, "The colors are: {{range $i, $color := .v}}{{$color}}{{if lt $i ($.v | len | minus1)}}, {{end}}{{end}}.\n", v1) fmt.Print(b.String()) } // To show the full code in GoDoc type dummyExecute0 struct { }
Output: 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. However, the fileName can be a comma-separated string for multiple data files.
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", .
func Quote4shell ¶
Quote4shell -- quote file name for shell. So "%bob's file" will be quoted as '%bob'\”s file'
func RegexpFindAllString ¶
RegexpFindAllString is wrapper for regexp.FindAllString
func RegexpFindAllStringIndex ¶
RegexpFindAllStringIndex is wrapper for regexp.FindAllStringIndex
func RegexpFindAllStringSubmatch ¶
RegexpFindAllStringSubmatch is wrapper for regexp.FindAllStringSubmatch
func RegexpFindAllStringSubmatchIndex ¶
RegexpFindAllStringSubmatchIndex is wrapper for regexp.FindAllStringSubmatchIndex
func RegexpFindString ¶
RegexpFindString is wrapper for regexp.FindString
func RegexpFindStringIndex ¶
RegexpFindStringIndex is wrapper for regexp.FindStringIndex
func RegexpFindStringSubmatch ¶
RegexpFindStringSubmatch is wrapper for regexp.FindStringSubmatch
func RegexpFindStringSubmatchIndex ¶
RegexpFindStringSubmatchIndex is wrapper for regexp.FindStringSubmatchIndex
func RegexpMatchString ¶
RegexpMatchString is wrapper for regexp.MatchString
func RegexpReplaceAllLiteralString ¶
RegexpReplaceAllLiteralString is wrapper for regexp.ReplaceAllLiteralString
func RegexpReplaceAllString ¶
RegexpReplaceAllString is wrapper for regexp.ReplaceAllString
func RegexpReplaceAllStringFunc ¶
RegexpReplaceAllStringFunc is wrapper for regexp.ReplaceAllStringFunc
func RegexpSplit ¶
RegexpSplit is wrapper for regexp.Split
func Substr ¶
Substr extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters.
It normally takes two parameters: start and length. It can also take one parameter: start, i.e. length is omitted, in which case the substring starting from start until the end of the string will be returned.
To extract characters from the end of the string, use a negative start number.
In addition, borrowing from the extended behavior described at http://php.net/substr, if length is given and is negative, then that many characters will be omitted from the end of string.
Types ¶
type EgBase ¶
EgBase -- EasyGen Template Base
stringsCompare is wrapper for strings.Compare stringsContains is wrapper for strings.Contains stringsContainsAny is wrapper for strings.ContainsAny stringsContainsRune is wrapper for strings.ContainsRune stringsCount is wrapper for strings.Count stringsEqualFold is wrapper for strings.EqualFold stringsFields is wrapper for strings.Fields stringsFieldsFunc is wrapper for strings.FieldsFunc stringsHasPrefix is wrapper for strings.HasPrefix stringsHasSuffix is wrapper for strings.HasSuffix stringsIndex is wrapper for strings.Index stringsIndexAny is wrapper for strings.IndexAny stringsIndexByte is wrapper for strings.IndexByte stringsIndexFunc is wrapper for strings.IndexFunc stringsIndexRune is wrapper for strings.IndexRune stringsJoin is wrapper for strings.Join stringsLastIndex is wrapper for strings.LastIndex stringsLastIndexAny is wrapper for strings.LastIndexAny stringsLastIndexByte is wrapper for strings.LastIndexByte stringsLastIndexFunc is wrapper for strings.LastIndexFunc stringsMap is wrapper for strings.Map stringsRepeat is wrapper for strings.Repeat stringsReplace is wrapper for strings.Replace stringsSplit is wrapper for strings.Split stringsSplitAfter is wrapper for strings.SplitAfter stringsSplitAfterN is wrapper for strings.SplitAfterN stringsSplitN is wrapper for strings.SplitN stringsTitle is wrapper for strings.Title stringsToLower is wrapper for strings.ToLower stringsToLowerSpecial is wrapper for strings.ToLowerSpecial stringsToTitle is wrapper for strings.ToTitle stringsToTitleSpecial is wrapper for strings.ToTitleSpecial stringsToUpper is wrapper for strings.ToUpper stringsToUpperSpecial is wrapper for strings.ToUpperSpecial stringsTrim is wrapper for strings.Trim stringsTrimFunc is wrapper for strings.TrimFunc stringsTrimLeft is wrapper for strings.TrimLeft stringsTrimLeftFunc is wrapper for strings.TrimLeftFunc stringsTrimPrefix is wrapper for strings.TrimPrefix stringsTrimRight is wrapper for strings.TrimRight stringsTrimRightFunc is wrapper for strings.TrimRightFunc stringsTrimSpace is wrapper for strings.TrimSpace stringsTrimSuffix is wrapper for strings.TrimSuffix eqf is wrapper for strings.EqualFold split is wrapper for strings.Fields sprintf is wrapper for fmt.Sprintf regexpFindAllString is template function for RegexpFindAllString regexpFindAllStringIndex is template function for RegexpFindAllStringIndex regexpFindAllStringSubmatch is template function for RegexpFindAllStringSubmatch regexpFindAllStringSubmatchIndex is template function for RegexpFindAllStringSubmatchIndex regexpFindString is template function for RegexpFindString regexpFindStringIndex is template function for RegexpFindStringIndex regexpFindStringSubmatch is template function for RegexpFindStringSubmatch regexpFindStringSubmatchIndex is template function for RegexpFindStringSubmatchIndex regexpMatchString is template function for RegexpMatchString regexpReplaceAllLiteralString is template function for RegexpReplaceAllLiteralString regexpReplaceAllString is template function for RegexpReplaceAllString regexpReplaceAllStringFunc is template function for RegexpReplaceAllStringFunc regexpSplit is template function for RegexpSplit ENV is template function for os.Getenv substr is template function for Substr coalesce is template function for Coalesce quote4shell is template function for Quote4shell minus1 is template function for Minus1 date is template function for Date timestamp is template function for Timestamp
func NewTemplate ¶
func NewTemplate() *EgBase
NewTemplate returns a new Template for this specific package.
type EgData ¶
type EgData map[EgKey]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 ReadDataFiles ¶
ReadDataFiles reads in the driving data from the given file, which can be optionally without the defined extension, and can be a comma-separated string for multiple data files.
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
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
cmd
|
|
easygen
Command easygen is an easy to use universal code/text generator.
|
Command easygen is an easy to use universal code/text generator. |
Package egCal provides generic calculation functionalities.
|
Package egCal provides generic calculation functionalities. |
Package egFilePath provides filepath manupilation functionalities.
|
Package egFilePath provides filepath manupilation functionalities. |
Package egVar provides variable naming functionalities.
|
Package egVar provides variable naming functionalities. |