mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-02 08:17:26 +00:00
Add a ParseTemplate util function for parsing go text templates easily
This commit is contained in:
parent
582187b6fb
commit
8f660dc24e
@ -12,6 +12,7 @@ go_library(
|
|||||||
name = "go_default_library",
|
name = "go_default_library",
|
||||||
srcs = [
|
srcs = [
|
||||||
"error.go",
|
"error.go",
|
||||||
|
"template.go",
|
||||||
"tokens.go",
|
"tokens.go",
|
||||||
"version.go",
|
"version.go",
|
||||||
],
|
],
|
||||||
@ -32,6 +33,7 @@ go_test(
|
|||||||
name = "go_default_test",
|
name = "go_default_test",
|
||||||
srcs = [
|
srcs = [
|
||||||
"error_test.go",
|
"error_test.go",
|
||||||
|
"template_test.go",
|
||||||
"tokens_test.go",
|
"tokens_test.go",
|
||||||
"version_test.go",
|
"version_test.go",
|
||||||
],
|
],
|
||||||
|
37
cmd/kubeadm/app/util/template.go
Normal file
37
cmd/kubeadm/app/util/template.go
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2016 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"text/template"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TODO: Should be unit-tested
|
||||||
|
func ParseTemplate(strtmpl string, obj interface{}) ([]byte, error) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
tmpl, err := template.New("template").Parse(strtmpl)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error when parsing template: %v", err)
|
||||||
|
}
|
||||||
|
err = tmpl.Execute(&buf, obj)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error when executing template: %v", err)
|
||||||
|
}
|
||||||
|
return buf.Bytes(), nil
|
||||||
|
}
|
90
cmd/kubeadm/app/util/template_test.go
Normal file
90
cmd/kubeadm/app/util/template_test.go
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2017 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
validTmpl = "image: {{ .ImageRepository }}/pause-{{ .Arch }}:3.0"
|
||||||
|
validTmplOut = "image: gcr.io/google_containers/pause-amd64:3.0"
|
||||||
|
doNothing = "image: gcr.io/google_containers/pause-amd64:3.0"
|
||||||
|
invalidTmpl1 = "{{ .baz }/d}"
|
||||||
|
invalidTmpl2 = "{{ !foobar }}"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseTemplate(t *testing.T) {
|
||||||
|
var tmplTests = []struct {
|
||||||
|
template string
|
||||||
|
data interface{}
|
||||||
|
output string
|
||||||
|
errExpected bool
|
||||||
|
}{
|
||||||
|
// should parse a valid template and set the right values
|
||||||
|
{
|
||||||
|
template: validTmpl,
|
||||||
|
data: struct{ ImageRepository, Arch string }{
|
||||||
|
ImageRepository: "gcr.io/google_containers",
|
||||||
|
Arch: "amd64",
|
||||||
|
},
|
||||||
|
output: validTmplOut,
|
||||||
|
errExpected: false,
|
||||||
|
},
|
||||||
|
// should noop if there aren't any {{ .foo }} present
|
||||||
|
{
|
||||||
|
template: doNothing,
|
||||||
|
data: struct{ ImageRepository, Arch string }{
|
||||||
|
ImageRepository: "gcr.io/google_containers",
|
||||||
|
Arch: "amd64",
|
||||||
|
},
|
||||||
|
output: doNothing,
|
||||||
|
errExpected: false,
|
||||||
|
},
|
||||||
|
// invalid syntax, passing nil
|
||||||
|
{
|
||||||
|
template: invalidTmpl1,
|
||||||
|
data: nil,
|
||||||
|
output: "",
|
||||||
|
errExpected: true,
|
||||||
|
},
|
||||||
|
// invalid syntax
|
||||||
|
{
|
||||||
|
template: invalidTmpl2,
|
||||||
|
data: struct{}{},
|
||||||
|
output: "",
|
||||||
|
errExpected: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tmplTests {
|
||||||
|
outbytes, err := ParseTemplate(tt.template, tt.data)
|
||||||
|
if tt.errExpected != (err != nil) {
|
||||||
|
t.Errorf(
|
||||||
|
"failed TestParseTemplate:\n\texpected err: %s\n\t actual: %s",
|
||||||
|
tt.errExpected,
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if tt.output != string(outbytes) {
|
||||||
|
t.Errorf(
|
||||||
|
"failed TestParseTemplate:\n\texpected bytes: %s\n\t actual: %s",
|
||||||
|
tt.output,
|
||||||
|
outbytes,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user