istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/util/tmpl/evaluate.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tmpl
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"istio.io/istio/pkg/test"
    21  	"istio.io/istio/pkg/test/util/file"
    22  )
    23  
    24  // Evaluate parses the template and then executes it with the given parameters.
    25  func Evaluate(tpl string, data any) (string, error) {
    26  	t, err := Parse(tpl)
    27  	if err != nil {
    28  		return "", err
    29  	}
    30  
    31  	return Execute(t, data)
    32  }
    33  
    34  func EvaluateFile(filePath string, data any) (string, error) {
    35  	tpl, err := file.AsString(filePath)
    36  	if err != nil {
    37  		return "", err
    38  	}
    39  	return Evaluate(tpl, data)
    40  }
    41  
    42  // EvaluateOrFail calls Evaluate and fails tests if it returns error.
    43  func EvaluateOrFail(t test.Failer, tpl string, data any) string {
    44  	t.Helper()
    45  	s, err := Evaluate(tpl, data)
    46  	if err != nil {
    47  		t.Fatalf("tmpl.EvaluateOrFail: %v", err)
    48  	}
    49  	return s
    50  }
    51  
    52  func EvaluateFileOrFail(t test.Failer, filePath string, data any) string {
    53  	t.Helper()
    54  	s, err := EvaluateFile(filePath, data)
    55  	if err != nil {
    56  		t.Fatalf("tmpl.EvaluateFileOrFail: %v", err)
    57  	}
    58  	return s
    59  }
    60  
    61  // MustEvaluate calls Evaluate and panics if there is an error.
    62  func MustEvaluate(tpl string, data any) string {
    63  	s, err := Evaluate(tpl, data)
    64  	if err != nil {
    65  		panic(fmt.Sprintf("tmpl.MustEvaluate: %v", err))
    66  	}
    67  	return s
    68  }
    69  
    70  func MustEvaluateFile(filePath string, data any) string {
    71  	s, err := EvaluateFile(filePath, data)
    72  	if err != nil {
    73  		panic(fmt.Sprintf("tmpl.MustEvaluate: %v", err))
    74  	}
    75  	return s
    76  }
    77  
    78  // EvaluateAll calls Evaluate the same data args against each of the given templates.
    79  func EvaluateAll(data any, templates ...string) ([]string, error) {
    80  	out := make([]string, 0, len(templates))
    81  	for _, t := range templates {
    82  		content, err := Evaluate(t, data)
    83  		if err != nil {
    84  			return nil, err
    85  		}
    86  		out = append(out, content)
    87  	}
    88  	return out, nil
    89  }
    90  
    91  func EvaluateAllFiles(data any, filePaths ...string) ([]string, error) {
    92  	templates, err := file.AsStringArray(filePaths...)
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  	return EvaluateAll(data, templates...)
    97  }
    98  
    99  func MustEvaluateAll(data any, templates ...string) []string {
   100  	out, err := EvaluateAll(data, templates...)
   101  	if err != nil {
   102  		panic(fmt.Sprintf("tmpl.MustEvaluateAll: %v", err))
   103  	}
   104  	return out
   105  }
   106  
   107  // EvaluateAllOrFail calls Evaluate and fails t if an error occurs.
   108  func EvaluateAllOrFail(t test.Failer, data any, templates ...string) []string {
   109  	t.Helper()
   110  	out, err := EvaluateAll(data, templates...)
   111  	if err != nil {
   112  		t.Fatal(err)
   113  	}
   114  	return out
   115  }
   116  
   117  func EvaluateAllFilesOrFail(t test.Failer, data any, filePaths ...string) []string {
   118  	t.Helper()
   119  	out, err := EvaluateAllFiles(data, filePaths...)
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  	return out
   124  }