Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup unused code #1010

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions tooling/templatize/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -270,10 +269,7 @@ func PreprocessContent(content []byte, vars map[string]any) ([]byte, error) {
}

func PreprocessContentIntoWriter(content []byte, vars map[string]any, writer io.Writer) error {
funcMap := template.FuncMap{
"json": jsonEncoder,
}
tmpl, err := template.New("file").Funcs(funcMap).Parse(string(content))
tmpl, err := template.New("file").Parse(string(content))
if err != nil {
return fmt.Errorf("failed to parse template: %w", err)
}
Expand All @@ -283,15 +279,3 @@ func PreprocessContentIntoWriter(content []byte, vars map[string]any, writer io.
}
return nil
}

func jsonEncoder(value interface{}) (string, error) {
valueType := reflect.TypeOf(value)
if valueType.Kind() == reflect.String {
return value.(string), nil
}
jsonBytes, err := json.Marshal(value)
if err != nil {
return "", err
}
return string(jsonBytes), nil
}
46 changes: 0 additions & 46 deletions tooling/templatize/pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"

"github.com/Azure/ARO-HCP/tooling/templatize/internal/testutil"
Expand Down Expand Up @@ -309,48 +308,3 @@ func TestPreprocessContentMissingKey(t *testing.T) {
})
}
}

func TestPreprocessContentJson(t *testing.T) {
templateContent := "{{ .variable | json }}"

testCases := []struct {
name string
value any
expectedResult string
}{
{
name: "string value",
value: "foo",
expectedResult: "foo",
},
{
name: "array value",
value: []string{"foo", "bar"},
expectedResult: "[\"foo\",\"bar\"]",
},
{
name: "int value",
value: 42,
expectedResult: "42",
},
{
name: "bool value",
value: true,
expectedResult: "true",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
processed, err := PreprocessContent(
[]byte(templateContent),
map[string]any{
"variable": tc.value,
},
)
assert.Nil(t, err)
if diff := cmp.Diff(string(processed), string(tc.expectedResult)); diff != "" {
t.Errorf("got diff between expected and actual result\ndiff:\n%s\n\nIf this is expected, re-run the test with `UPDATE=true go test ./...` to update the fixtures.", diff)
}
})
}
}
Loading