MCPcopy Create free account
hub / github.com/cloudwan/gohan / LoadTemplate

Function LoadTemplate

util/util.go:127–150  ·  view source on GitHub ↗

LoadTemplate loads object from file. suffix of filepath will be used as file type. currently, json and yaml is supported

(filePath string, params interface{})

Source from the content-addressed store, hash-verified

125//LoadTemplate loads object from file. suffix of filepath will be
126// used as file type. currently, json and yaml is supported
127func LoadTemplate(filePath string, params interface{}) (map[string]interface{}, error) {
128 templateSource, err := GetContent(filePath)
129 t := template.Must(template.New("tmpl").Parse(string(templateSource[:])))
130 outputBuffer := bytes.NewBuffer(make([]byte, 0, 100))
131 t.Execute(outputBuffer, params)
132 data := outputBuffer.Bytes()
133 if err != nil {
134 return nil, err
135 }
136 if strings.HasSuffix(filePath, ".json") {
137 var document map[string]interface{}
138 err = json.Unmarshal(data, &document)
139 return document, err
140 } else if strings.HasSuffix(filePath, ".yaml") {
141 var documentYAML map[interface{}]interface{}
142 err = yaml.Unmarshal(data, &documentYAML)
143 if err != nil {
144 return map[string]interface{}{}, err
145 }
146 document := DecodeYAMLLibObject(documentYAML)
147 return document.(map[string]interface{}), nil
148 }
149 return nil, err
150}
151
152//DecodeYAMLLibObject decodes interface format
153//yaml.v2 lib returns map as map[interface{}]interface{} while gohan lib uses map[string]interface{}

Callers 1

ReadConfigMethod · 0.85

Calls 3

GetContentFunction · 0.85
DecodeYAMLLibObjectFunction · 0.85
ParseMethod · 0.65

Tested by

no test coverage detected