Skip to content

Commit

Permalink
Fix a panic for unknown cty types, return cty.NullVal of type cty.Str…
Browse files Browse the repository at this point in the history
…ing as fallback #44

Add test for json 'null' values
  • Loading branch information
Marcel Ludwig committed Oct 26, 2020
1 parent 6149d68 commit ca5b4c0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
6 changes: 5 additions & 1 deletion eval/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func TestNewHTTPContext(t *testing.T) {
method = req.method
title = req.json_body.slice.foo
`, http.Header{"method": {http.MethodGet}}},
{"Variables / GET /w json body & null value", http.MethodGet, http.Header{"Content-Type": {"application/json"}}, bytes.NewBufferString(`{"json": null}`), "", baseCtx, `
method = req.method
title = req.json_body.json
`, http.Header{"method": {http.MethodGet}, "title": {""}}},
}

log, _ := test.NewNullLogger()
Expand Down Expand Up @@ -134,7 +138,7 @@ func TestNewHTTPContext(t *testing.T) {

result := seetie.ValueToStringSlice(cv)
if !reflect.DeepEqual(v, result) {
t.Errorf("Expected %q, got: %v", v, cv)
t.Errorf("Expected %q, got: %#v", v, cv)
}
}
})
Expand Down
14 changes: 9 additions & 5 deletions internal/seetie/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"math/big"
"net/http"
"net/url"
"reflect"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -81,7 +80,7 @@ func GoToValue(v interface{}) cty.Value {
case map[string]interface{}:
return MapToValue(v.(map[string]interface{}))
}
return cty.NilVal
return cty.NullVal(cty.String)
}

func MapToValue(m map[string]interface{}) cty.Value {
Expand All @@ -96,8 +95,6 @@ func MapToValue(m map[string]interface{}) cty.Value {
continue
}
switch v.(type) {
case bool, float64, string:
ctyMap[k] = GoToValue(v)
case []string:
var list []interface{}
for _, s := range v.([]string) {
Expand All @@ -109,7 +106,7 @@ func MapToValue(m map[string]interface{}) cty.Value {
case map[string]interface{}:
ctyMap[k] = MapToValue(v.(map[string]interface{}))
default:
panic(reflect.TypeOf(v).String() + " not implemented")
ctyMap[k] = GoToValue(v)
}
}

Expand Down Expand Up @@ -168,6 +165,10 @@ var whitespaceRegex = regexp.MustCompile(`^\s*$`)
// ValueToString explicitly drops all other (unknown) types and
// converts non whitespace strings or numbers to its string representation.
func ValueToString(v cty.Value) string {
if v.IsNull() {
return ""
}

switch v.Type() {
case cty.String:
str := v.AsString()
Expand Down Expand Up @@ -221,6 +222,9 @@ func ToString(s interface{}) string {

// isTuple checks by type name since tuple is not comparable by type.
func isTuple(v cty.Value) bool {
if v.IsNull() {
return false
}
return v.Type().FriendlyNameForConstraint() == "tuple"
}

Expand Down

0 comments on commit ca5b4c0

Please sign in to comment.