github.com/zsuzhengdu/helm@v3.0.0-beta.3+incompatible/pkg/engine/funcs.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package engine
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/json"
    22  	"strings"
    23  	"text/template"
    24  
    25  	"github.com/BurntSushi/toml"
    26  	"github.com/Masterminds/sprig"
    27  	yaml "gopkg.in/yaml.v2"
    28  )
    29  
    30  // funcMap returns a mapping of all of the functions that Engine has.
    31  //
    32  // Because some functions are late-bound (e.g. contain context-sensitive
    33  // data), the functions may not all perform identically outside of an Engine
    34  // as they will inside of an Engine.
    35  //
    36  // Known late-bound functions:
    37  //
    38  //	- "include"
    39  //	- "tpl"
    40  //
    41  // These are late-bound in Engine.Render().  The
    42  // version included in the FuncMap is a placeholder.
    43  //
    44  func funcMap() template.FuncMap {
    45  	f := sprig.TxtFuncMap()
    46  	delete(f, "env")
    47  	delete(f, "expandenv")
    48  
    49  	// Add some extra functionality
    50  	extra := template.FuncMap{
    51  		"toToml":   toTOML,
    52  		"toYaml":   toYAML,
    53  		"fromYaml": fromYAML,
    54  		"toJson":   toJSON,
    55  		"fromJson": fromJSON,
    56  
    57  		// This is a placeholder for the "include" function, which is
    58  		// late-bound to a template. By declaring it here, we preserve the
    59  		// integrity of the linter.
    60  		"include":  func(string, interface{}) string { return "not implemented" },
    61  		"tpl":      func(string, interface{}) interface{} { return "not implemented" },
    62  		"required": func(string, interface{}) (interface{}, error) { return "not implemented", nil },
    63  	}
    64  
    65  	for k, v := range extra {
    66  		f[k] = v
    67  	}
    68  
    69  	return f
    70  }
    71  
    72  // toYAML takes an interface, marshals it to yaml, and returns a string. It will
    73  // always return a string, even on marshal error (empty string).
    74  //
    75  // This is designed to be called from a template.
    76  func toYAML(v interface{}) string {
    77  	data, err := yaml.Marshal(v)
    78  	if err != nil {
    79  		// Swallow errors inside of a template.
    80  		return ""
    81  	}
    82  	return strings.TrimSuffix(string(data), "\n")
    83  }
    84  
    85  // fromYAML converts a YAML document into a map[string]interface{}.
    86  //
    87  // This is not a general-purpose YAML parser, and will not parse all valid
    88  // YAML documents. Additionally, because its intended use is within templates
    89  // it tolerates errors. It will insert the returned error message string into
    90  // m["Error"] in the returned map.
    91  func fromYAML(str string) map[string]interface{} {
    92  	m := map[string]interface{}{}
    93  
    94  	if err := yaml.Unmarshal([]byte(str), &m); err != nil {
    95  		m["Error"] = err.Error()
    96  	}
    97  	return m
    98  }
    99  
   100  // toTOML takes an interface, marshals it to toml, and returns a string. It will
   101  // always return a string, even on marshal error (empty string).
   102  //
   103  // This is designed to be called from a template.
   104  func toTOML(v interface{}) string {
   105  	b := bytes.NewBuffer(nil)
   106  	e := toml.NewEncoder(b)
   107  	err := e.Encode(v)
   108  	if err != nil {
   109  		return err.Error()
   110  	}
   111  	return b.String()
   112  }
   113  
   114  // toJSON takes an interface, marshals it to json, and returns a string. It will
   115  // always return a string, even on marshal error (empty string).
   116  //
   117  // This is designed to be called from a template.
   118  func toJSON(v interface{}) string {
   119  	data, err := json.Marshal(v)
   120  	if err != nil {
   121  		// Swallow errors inside of a template.
   122  		return ""
   123  	}
   124  	return string(data)
   125  }
   126  
   127  // fromJSON converts a JSON document into a map[string]interface{}.
   128  //
   129  // This is not a general-purpose JSON parser, and will not parse all valid
   130  // JSON documents. Additionally, because its intended use is within templates
   131  // it tolerates errors. It will insert the returned error message string into
   132  // m["Error"] in the returned map.
   133  func fromJSON(str string) map[string]interface{} {
   134  	m := make(map[string]interface{})
   135  
   136  	if err := json.Unmarshal([]byte(str), &m); err != nil {
   137  		m["Error"] = err.Error()
   138  	}
   139  	return m
   140  }