github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/formationconstraint/parse_template.go (about) 1 package formationconstraint 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "text/template" 7 8 "github.com/kyma-incubator/compass/components/director/pkg/inputvalidation" 9 ) 10 11 var templateFuncMap = template.FuncMap{ 12 "toString": func(bytesData []byte) string { 13 config := string(bytesData) 14 if config == "" { 15 config = "\"\"" 16 } 17 18 return config 19 }, 20 } 21 22 // ParseInputTemplate parses tmpl using data and stores the result in dest 23 func ParseInputTemplate(tmpl string, data interface{}, dest interface{}) error { 24 t, err := template.New("").Option("missingkey=zero").Funcs(templateFuncMap).Parse(tmpl) 25 if err != nil { 26 return err 27 } 28 29 res := new(bytes.Buffer) 30 if err = t.Execute(res, data); err != nil { 31 return err 32 } 33 if err = json.Unmarshal(res.Bytes(), dest); err != nil { 34 return err 35 } 36 37 if validatable, ok := dest.(inputvalidation.Validatable); ok { 38 return validatable.Validate() 39 } 40 41 return nil 42 }