Skip to content

Commit

Permalink
Add getEncodedJson and getEncodedEscapedJson functions (#1159)
Browse files Browse the repository at this point in the history
* Add getEncodedJson which takes a map and returns a JSON encoded string
* Add getEncodedEscapedJson which takes a map and returns a JSON
  encoded, escaped string
* Update unit tests
  • Loading branch information
GKozakjian authored Sep 1, 2022
1 parent 34a4c18 commit d961fe3
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 13 deletions.
61 changes: 48 additions & 13 deletions internal/template/funcmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package template

import (
"encoding/json"
"fmt"
"regexp"
"strings"
Expand All @@ -24,19 +25,21 @@ import (
)

var funcMap = map[string]interface{}{
"get": get,
"has": has,
"hcl": hcl,
"hclField": hclField,
"merge": merge,
"replace": replace,
"resourceName": resourceName,
"now": time.Now,
"trimSpace": strings.TrimSpace,
"regexReplaceAll": regexReplaceAll,
"makeSlice": makeSlice,
"schemaDescription": schemaDescription,
"substr": substr,
"get": get,
"has": has,
"hcl": hcl,
"hclField": hclField,
"merge": merge,
"replace": replace,
"resourceName": resourceName,
"now": time.Now,
"trimSpace": strings.TrimSpace,
"regexReplaceAll": regexReplaceAll,
"makeSlice": makeSlice,
"schemaDescription": schemaDescription,
"substr": substr,
"getEncodedJSON": getEncodedJSON,
"getEncodedEscapedJSON": getEncodedEscapedJSON,
}

// invalidIDRE defines the invalid characters not allowed in terraform resource names.
Expand Down Expand Up @@ -173,3 +176,35 @@ func substr(s string, start int, length int) (string, error) {
}
return s[start : start+length], nil
}

// getEncodedJSON returns an encoded JSON string from a given map
func getEncodedJSON(m map[string]interface{}) (string, error) {
jsonStr, err := json.Marshal(m)
if err != nil {
return "", fmt.Errorf("JSON marshalling failed due to %w", err)
}
return string(jsonStr), nil
}

// getEncodedEscapedJSON returns an encoded, escaped JSON string from a given map
func getEncodedEscapedJSON(m map[string]interface{}) (string, error) {
jsonStr, err := json.Marshal(m)
if err != nil {
return "", fmt.Errorf("JSON marshalling failed due to %w", err)
}
escapedJSONStr, err := jsonEscape(string(jsonStr))
if err != nil {
return "", fmt.Errorf("JSON escaping failed due to %w", err)
}
return escapedJSONStr, nil
}

// jsonEscape return an escaped JSON string
func jsonEscape(i string) (string, error) {
jsonStr, err := json.Marshal(i)
if err != nil {
return "", fmt.Errorf("JSON marshalling failed due to %w", err)
}
str := string(jsonStr)
return str[1 : len(str)-1], nil
}
66 changes: 66 additions & 0 deletions internal/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,69 @@ func TestSubstr(t *testing.T) {
}
}
}

func TestGetEncodedJSON(t *testing.T) {
tests := []struct {
input map[string]interface{}
want string
}{
{map[string]interface{}{
"testboolfalse": false,
"testbooltrue": true,
"testint": 1,
"teststring": "test",
"testmap": map[string]interface{}{
"testmapstring": "test",
"testmapint": 1,
"testmapboolean": true,
},
}, "{\"testboolfalse\":false,\"testbooltrue\":true,\"testint\":1,\"testmap\":{\"testmapboolean\":true,\"testmapint\":1,\"testmapstring\":\"test\"},\"teststring\":\"test\"}"},
{map[string]interface{}{}, "{}"},
}

for _, tc := range tests {
got, err := getEncodedJSON(tc.input)

if err != nil {
t.Fatalf("received error for valid input:\"%s\", got:%s, "+
", error received:\n%v", tc.input, got, err)
}
if diff := cmp.Diff(got, tc.want); diff != "" {
t.Errorf("getEncodedJSON results differ for input:\"%s\", want:%s, got:%s, "+
"and diff(-want +got):\n%v", tc.input, tc.want, got, diff)
}
}
}

func TestGetEncodedEscapedJSON(t *testing.T) {
tests := []struct {
input map[string]interface{}
want string
}{
{map[string]interface{}{
"testboolfalse": false,
"testbooltrue": true,
"testint": 1,
"teststring": "test",
"testmap": map[string]interface{}{
"testmapstring": "test",
"testmapint": 1,
"testmapboolean": true,
},
}, "{\\\"testboolfalse\\\":false,\\\"testbooltrue\\\":true,\\\"testint\\\":1,\\\"testmap\\\":{\\\"testmapboolean\\\":true,\\\"testmapint\\\":1,\\\"testmapstring\\\":\\\"test\\\"},\\\"teststring\\\":\\\"test\\\"}"},
{map[string]interface{}{}, "{}"},
}

for _, tc := range tests {
got, err := getEncodedEscapedJSON(tc.input)

if err != nil {
t.Fatalf("received error for valid input:\"%s\", got:%s, "+
", error received:\n%v", tc.input, got, err)
}
if diff := cmp.Diff(got, tc.want); diff != "" {
t.Errorf("getEncodedEscapedJSON results differ for input:\"%s\", want:%s, got:%s, "+
"and diff(-want +got):\n%v", tc.input, tc.want, got, diff)
}
}
}

0 comments on commit d961fe3

Please sign in to comment.