Skip to content

Wiki: easygen builtin functions #25

@suntong

Description

@suntong

easygen builtin functions

easygen/template.go

Lines 29 to 103 in 60daafd

// 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
*/

easygen/t_strings_test.go

Lines 35 to 219 in 23ee694

// == standard strings functions
{
`{{ stringsContains "seafood" "foo" }}`,
"true",
},
{
`{{ stringsContains "seafood" "bar" }}`,
"false",
},
{
`{{ stringsContainsAny "team" "i" }}`,
"false",
},
{
`{{ stringsContainsAny "failure" "u & i" }}`,
"true",
},
{
`{{ stringsCount "cheese" "e" }}`,
"3",
},
{
`{{ stringsEqualFold "Go" "go" }}`,
"true",
},
{
`{{ stringsFields " foo bar baz " }}`,
"[foo bar baz]",
},
{
`{{ stringsIndex "go gopher" "go" }}`,
"0",
},
{
`{{ stringsLastIndex "go gopher" "go" }}`,
"3",
},
{
`ba{{ stringsRepeat "na" 2 }}`,
"banana",
},
{
`{{ stringsReplace "oink oink oink" "k" "ky" 2 }}`,
"oinky oinky oink",
},
{
`{{ stringsReplace "oink oink oink" "oink" "moo" -1 }}`,
"moo moo moo",
},
{
`{{ stringsSplitN "a,b,c" "," 0 }}`,
`[]`,
},
{
`{{ stringsSplitAfter "a,b,c" "," }}`,
`[a, b, c]`,
},
{
`{{ stringsSplitAfterN "a,b,c" "," 2 }}`,
`[a, b,c]`,
},
{
`{{ stringsTitle "her royal highness" }}`,
"Her Royal Highness",
},
{
`{{ stringsToLower "Gopher" }}`,
"gopher",
},
{
`{{ stringsToUpper "Gopher" }}`,
"GOPHER",
},
{
`{{ stringsTrimSpace " \t\n a lone gopher \n\t\r\n" }}`,
"a lone gopher",
},
{
`{{ stringsTrim " !!! Achtung !!! " "! " }}`,
"Achtung",
},
{
`{{ stringsTrimPrefix "Goodbye,, world!" "Goodbye," }}`,
", world!",
},
{
`{{ stringsTrimSuffix "Hello, goodbye, etc!" "goodbye, etc!" }}`,
"Hello, ",
},
// aliases
{
`The {{if eq .StrTest "these rights belong to those people"}}eq says Yea{{else}}eq says Nay{{end}} but {{if eqf .StrTest "these rights belong to those people"}}eqf says Yea{{else}}eqf says Nay{{end}}.`,
"The eq says Nay but eqf says Yea.",
},
{
`{{$s := sprintf .StrTest}} {{$s}}`,
" These rights belong to those people",
},
{
`{{$s := sprintf "%s, %.2f" .StrTest 12.3456}} {{$s}}`,
" These rights belong to those people, 12.35",
},
// == standard regexp functions
{
`{{ regexpFindString "peach punch" "p([a-z]+)ch" }}`,
"peach",
},
{
`{{ regexpFindAllString "peach punch" "p([a-z]+)ch" -1 }}`,
"[peach punch]",
},
{
`{{ regexpFindAllString "peach punch" "p([a-z]+)ch" 1 }}`,
"[peach]",
},
{
`{{ regexpFindStringIndex "peach punch" "p([a-z]+)ch" }}`,
"[0 5]",
},
{
`{{ regexpFindStringSubmatch "peach punch" "p([a-z]+)ch" }}`,
"[peach ea]",
},
{
`{{ regexpFindStringSubmatchIndex "peach punch" "p([a-z]+)ch" }}`,
"[0 5 1 3]",
},
//
{
`{{ regexpMatchString "HTTPS://site/" "(?i)^http" }}`,
"true",
},
//
{
`{{ regexpReplaceAllLiteralString "html HTML Html aa uml bb Uml" "(?i)html|uml" "XML" }}`,
"XML XML XML aa XML bb XML",
},
{
`{{ regexpReplaceAllString .StrTest "(?i)th[eo]se" "the" }}`,
"the rights belong to the people",
},
{
`{{ regexpReplaceAllString "This and these are for THOSE people" "(?i)(this|th[eo]se)" "<b>${1}</b>" }}`,
"<b>This</b> and <b>these</b> are for <b>THOSE</b> people",
},
// {
// `{{ regexpReplaceAllStringFunc "a peach" "p([a-z]+)ch" stringsToUpper }}`,
// "a PEACH",
// },
// == my added strings functions
{
`{{ coalesce "a" }}`,
"a",
},
{
`{{ coalesce "" "b" }}`,
"b",
},
{
`{{ coalesce "" "" "c" }}`,
"c",
},
{
`{{ coalesce "" "" "" "" }}`,
"",
},
{
`{{ coalesce "" }}`,
"",
},
{
`{{ coalesce .StrTest "Something else" }}`,
"These rights belong to those people",
},
{
`{{ coalesce .StrEmpty "Something else" }}`,
"Something else",
},
// NOK! The following will failed the template.Execute
// before the coalesce function can do anything
// {
// `{{ coalesce .StrNone "Not exist" }}`,
// "Not exist",
// },

egVar builtin functions

Notes:

easygen/egVar/EgVar.go

Lines 32 to 40 in 5fd01ce

ls - LowerSnakeCase: lower_snake_case
ss - ScreamingSnakeCase: SCREAMING_SNAKE_CASE
lk - KebabCase: kebab-case
ScreamingKebabCase: SCREAMING-KEBAB-CASE
hh - HttpHeaderCase: HTTP-Header-Case
UpperCamelCase: UpperCamelCase (renders HTTP as Http)
LowerCamelCase: lowerCamelCase (renders HTTP as Http)
uc - UpperCamelCaseKeepCaps: UpperCamelCaseKeepCaps (renders HTTP as HTTP)
lc - LowerCamelCaseKeepCaps: lowerCamelCaseKeepCaps (renders HTTP as hTTP)

Functions

easygen/egVar/EgVar.go

Lines 90 to 118 in 5fd01ce

"cls2lc": cls2lc.String,
"cls2uc": cls2uc.String,
"cls2ss": cls2ss.String,
"cls2lk": cls2lk.String,
"cls2hh": cls2hh.String,
"css2lc": css2lc.String,
"css2uc": css2uc.String,
"css2ls": css2ls.String,
"css2lk": css2lk.String,
"css2hh": css2hh.String,
"clk2lc": clk2lc.String,
"clk2uc": clk2uc.String,
"clk2ls": clk2ls.String,
"clk2ss": clk2ss.String,
"clc2uc": clc2uc.String,
"clc2ls": clc2ls.String,
"clc2ss": clc2ss.String,
"cuc2lc": cuc2lc.String,
"cuc2ls": cuc2ls.String,
"cuc2ss": cuc2ss.String,
// Aliases, for backwards-compatibility with earlier (>= v1) versions
"ck2lc": clk2lc.String,
"ck2uc": clk2uc.String,
"ck2ls": clk2ls.String,
"ck2ss": clk2ss.String,

Examples

fmt.Println("\n## From lk - KebabCase => CamelCase/SnakeCase")
easygen.Process0(tmpl, os.Stdout,
"{{.Name}}: {{clk2lc .Name}} {{clk2uc .Name}}\n", "../test/listfunc2")
easygen.Process0(tmpl, os.Stdout,
"{{.Name}}: {{clk2ls .Name}} {{clk2ss .Name}}\n", "../test/listfunc2")
fmt.Println("\n## From ls/ss - LowerSnakeCase/ScreamingSnakeCase")
easygen.Process0(tmpl, os.Stdout,
"{{clk2ls .Name}} {{clk2ss .Name}} =>\n", "../test/listfunc2")
fmt.Println("### From ls")
easygen.Process0(tmpl, os.Stdout,
" {{clk2ls .Name | cls2lc}} {{clk2ls .Name | cls2uc}}\n", "../test/listfunc2")
easygen.Process0(tmpl, os.Stdout,
" {{clk2ls .Name | cls2ss}} {{clk2ls .Name | cls2lk}} {{clk2ls .Name | cls2hh}}\n", "../test/listfunc2")
easygen.Process0(tmpl, os.Stdout,
" {{clk2ls .NameHTTP | cls2lc}} {{clk2ls .NameHTTP | cls2uc}}\n", "../test/listfunc2")
easygen.Process0(tmpl, os.Stdout,
" {{clk2ls .NameHTTP | cls2ss}} {{clk2ls .NameHTTP | cls2lk}} {{clk2ls .NameHTTP | cls2hh}}\n", "../test/listfunc2")
fmt.Println("### From ss")
easygen.Process0(tmpl, os.Stdout,
" {{clk2ss .Name | css2lc}} {{clk2ss .Name | css2uc}}\n", "../test/listfunc2")
easygen.Process0(tmpl, os.Stdout,
" {{clk2ss .Name | css2ls}} {{clk2ss .Name | css2lk}} {{clk2ss .Name | css2hh}}\n", "../test/listfunc2")
easygen.Process0(tmpl, os.Stdout,
" {{clk2ss .NameHTTP | css2lc}} {{clk2ss .NameHTTP | css2uc}}\n", "../test/listfunc2")
easygen.Process0(tmpl, os.Stdout,
" {{clk2ss .NameHTTP | css2ls}} {{clk2ss .NameHTTP | css2lk}} {{clk2ss .NameHTTP | css2hh}}\n", "../test/listfunc2")
fmt.Println("\n## From lc/uc - LowerCamelCase/UpperCamelCaseKeepCaps")
easygen.Process0(tmpl, os.Stdout,
"{{clk2lc .Name}} {{clk2uc .Name}} =>\n", "../test/listfunc2")
easygen.Process0(tmpl, os.Stdout,
"{{clk2lc .Name}}: {{clk2lc .Name | clc2uc}} {{clk2lc .Name | clc2ls}} {{clk2lc .Name | clc2ss}}\n", "../test/listfunc2")
easygen.Process0(tmpl, os.Stdout,
"{{clk2uc .Name}}: {{clk2uc .Name | cuc2lc}} {{clk2uc .Name | cuc2ls}} {{clk2uc .Name | cuc2ss}}\n", "../test/listfunc2")
easygen.Process0(tmpl, os.Stdout,
"{{.NameMixed}}: {{clc2uc .NameMixed}} {{clc2uc .NameMixed | cuc2lc}} {{clc2ls .NameMixed}} {{clc2ls .NameMixed | cls2lc}}\n", "../test/listfunc2")
easygen.Process0(tmpl, os.Stdout,
"{{.NameMixed}}: {{clc2ls .NameMixed | cls2ss}} {{clc2ls .NameMixed | cls2lk}} {{clc2ls .NameMixed | cls2hh}}\n", "../test/listfunc2")
// Output:
//
// ## From lk - KebabCase => CamelCase/SnakeCase
// some-init-method: someInitMethod SomeInitMethod
// some-init-method: some_init_method SOME_INIT_METHOD
//
// ## From ls/ss - LowerSnakeCase/ScreamingSnakeCase
// some_init_method SOME_INIT_METHOD =>
// ### From ls
// someInitMethod SomeInitMethod
// SOME_INIT_METHOD some-init-method Some-Init-Method
// httpHeaderCase HttpHeaderCase
// HTTP_HEADER_CASE http-header-case HTTP-Header-Case
// ### From ss
// someINITMETHOD SOMEINITMETHOD
// some_init_method some-init-method Some-Init-Method
// httpHEADERCASE HTTPHEADERCASE
// http_header_case http-header-case HTTP-Header-Case
//
// ## From lc/uc - LowerCamelCase/UpperCamelCaseKeepCaps
// someInitMethod SomeInitMethod =>
// someInitMethod: SomeInitMethod some_init_method SOME_INIT_METHOD
// SomeInitMethod: someInitMethod some_init_method SOME_INIT_METHOD
// some_InitMethod: Some_InitMethod some_InitMethod some__init_method someInitMethod
// some_InitMethod: SOME__INIT_METHOD some--init-method Some--Init-Method

egFilePath builtin functions

//"fpAbs": filepath.Abs,
"fpBase": filepath.Base,
"fpClean": filepath.Clean,
"fpDir": filepath.Dir,
//"fpEvalSymlinks": filepath.EvalSymlinks,
"fpExt": filepath.Ext,
"fpFromSlash": filepath.FromSlash,
//"fpGlob": filepath.Glob,
"fpHasPrefix": filepath.HasPrefix,
"fpIsAbs": filepath.IsAbs,
"fpJoin": filepath.Join,
//"fpMatch": filepath.Match,
"fpRel": filepath.Rel,
//"fpSplit": filepath.Split,
//"fpSplitList": filepath.SplitList,
"fpToSlash": filepath.ToSlash,
"fpVolumeName": filepath.VolumeName,
"isDir": IsDir,
"basename": Basename,

Examples

"Base: {{fpBase `/a/b.c`}}, {{fpBase `/b.c/`}}, {{fpBase `./b.c`}}, {{fpBase `b.c`}}\n"+
"Clean: {{fpClean `/a//b.c/./..`}}, {{fpClean `//b.c///`}}, {{fpClean `/../b.c`}}\n"+
"Dir: {{fpDir `/a/b/c`}}, {{fpDir `/b/c/`}}, {{fpDir `./b/c`}}, {{fpDir `b.c`}}\n"+
"Ext: {{fpExt `index`}}, {{fpExt `index.js`}}, {{fpExt `main.test.js`}}\n"+
"Join: {{fpJoin `/a` `b` `c` `a//` `//b////c`}}, {{fpJoin `a` `b/c`}}, {{fpJoin `a/b` `c`}}\n"+
// "IsDir: {{isDir `.`}}, {{isDir `not-exist`}}.\n"+
"IsDir: {{isDir `.`}}.\n"+
"Basename: {{basename `/a/b.c`}}, {{basename `/b.c/`}}, {{basename `b.c`}}, {{basename `bc`}}",
"../test/var0")
// Output:
// Base: b.c, b.c, b.c, b.c
// Clean: /a, /b.c, /b.c
// Dir: /a/b, /b/c, b, .
// Ext: , .js, .js
// Join: /a/b/c/a/b/c, a/b/c, a/b/c
// IsDir: true.
// Basename: b, b, b, bc

egCal builtin calculation functions

easygen/egCal/EgCal.go

Lines 24 to 27 in 5fd01ce

"add": add,
"subtract": subtract,
"multiply": multiply,
"divide": divide,

Examples

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")
// Output:
// some-init-method: SomeInitMethod SOME_INIT_METHOD
// Cal: 5, 6, 7, 8

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions