From 03208bd3a9e4d22c96cd5f74bf54301753ef8615 Mon Sep 17 00:00:00 2001 From: Juan Manuel Villacorta Campos Date: Fri, 25 Mar 2022 15:37:51 +0000 Subject: [PATCH] Sprig functions added to template system --- go.mod | 12 ++++++++++++ go.sum | 19 +++++++++++++++++++ pkg/generators/template.go | 22 +++++++++++++++++++--- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index a5ac2d6..0815780 100644 --- a/go.mod +++ b/go.mod @@ -8,3 +8,15 @@ require ( github.com/yoheimuta/go-protoparser/v4 v4.5.4 gopkg.in/yaml.v2 v2.4.0 ) + +require ( + github.com/Masterminds/goutils v1.1.1 // indirect + github.com/Masterminds/semver v1.5.0 // indirect + github.com/Masterminds/sprig v2.22.0+incompatible // indirect + github.com/google/uuid v1.3.0 // indirect + github.com/huandu/xstrings v1.3.2 // indirect + github.com/imdario/mergo v0.3.12 // indirect + github.com/mitchellh/copystructure v1.2.0 // indirect + github.com/mitchellh/reflectwalk v1.0.2 // indirect + golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 // indirect +) diff --git a/go.sum b/go.sum index 9af33c1..c07fd88 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,31 @@ +github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI= +github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU= +github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww= +github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y= +github.com/Masterminds/sprig v2.22.0+incompatible h1:z4yfnGrZ7netVz+0EDJ0Wi+5VZCSYp4Z0m2dk6cEM60= +github.com/Masterminds/sprig v2.22.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o= github.com/gertd/go-pluralize v0.2.0 h1:VzWNnxkUo3wkW2Nmp+3ieHSTQQ0LBHeSVxlKsQPQ+UY= github.com/gertd/go-pluralize v0.2.0/go.mod h1:4ouO1Ndf/r7sZMorwp4Sbfw80lUni+sd+o3qJR8L9To= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/huandu/xstrings v1.3.2 h1:L18LIDzqlW6xN2rEkpdV8+oL/IXWJ1APd+vsdYy4Wdw= +github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0= github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= +github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU= +github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= +github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw= +github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s= +github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ= +github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/yoheimuta/go-protoparser/v4 v4.5.4 h1:0/uEpkzBeSmC591cHIm+M7WIfHaP5FOyI4Y32Lef25I= github.com/yoheimuta/go-protoparser/v4 v4.5.4/go.mod h1:AHNNnSWnb0UoL4QgHPiOAg2BniQceFscPI5X/BZNHl8= +golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 h1:S25/rfnfsMVgORT4/J61MJ7rdyseOZOyvLIrZEZ7s6s= +golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/pkg/generators/template.go b/pkg/generators/template.go index 03b4ca6..539f24c 100644 --- a/pkg/generators/template.go +++ b/pkg/generators/template.go @@ -1,16 +1,27 @@ package generators import ( + "bytes" "strings" "text/template" "github.com/gertd/go-pluralize" "github.com/iancoleman/strcase" "github.com/juanvillacortac/ditto/pkg/ast" + + "github.com/Masterminds/sprig" ) -func templateHelpers(models ast.ModelMap, options GenerateConfig) template.FuncMap { - return template.FuncMap{ +func templateHelpers(t *template.Template, models ast.ModelMap, options GenerateConfig) template.FuncMap { + funcs := template.FuncMap{ + "ExecTmpl": func(name string, obj interface{}) string { + buf := &bytes.Buffer{} + err := t.ExecuteTemplate(buf, name, obj) + if err != nil { + panic(err) + } + return buf.String() + }, "Models": func() ast.ModelMap { return models }, "Model": func(modelName string) *ast.Model { return models[modelName] }, "ModelDeps": models.ModelDependencies, @@ -34,6 +45,10 @@ func templateHelpers(models ast.ModelMap, options GenerateConfig) template.FuncM return p.Singular(str) }, } + for k, v := range sprig.FuncMap() { + funcs[k] = v + } + return funcs } type TemplateContext struct { @@ -42,7 +57,8 @@ type TemplateContext struct { } func createTemplate(name string, content string, models ast.ModelMap, options GenerateConfig) (*template.Template, error) { - return template.New(name).Funcs(templateHelpers(models, options)).Parse(content) + t := template.New(name) + return t.Funcs(templateHelpers(t, models, options)).Parse(content) } func execTemplate(t *template.Template, context *TemplateContext) (string, error) {