Skip to content

Commit

Permalink
support more complex types in seetie eval conversion #44
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Ludwig committed Oct 23, 2020
1 parent 37c7256 commit 6b070a5
Showing 1 changed file with 45 additions and 5 deletions.
50 changes: 45 additions & 5 deletions internal/seetie/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math/big"
"net/http"
"net/url"
"reflect"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -56,22 +57,61 @@ func ValuesMapToValue(m url.Values) cty.Value {
return MapToValue(result)
}

func ListToValue(l []interface{}) cty.Value {
if len(l) == 0 {
return cty.NilVal
}
var list []cty.Value
for _, v := range l {
list = append(list, GoToValue(v))
}
return cty.TupleVal(list)
}

func GoToValue(v interface{}) cty.Value {
switch v.(type) {
case string:
return cty.StringVal(ToString(v))
case bool:
return cty.BoolVal(v.(bool))
case float64:
return cty.NumberFloatVal(v.(float64))
case []interface{}:
return ListToValue(v.([]interface{}))
case map[string]interface{}:
return MapToValue(v.(map[string]interface{}))
}
return cty.NilVal
}

func MapToValue(m map[string]interface{}) cty.Value {
if m == nil {
return cty.MapValEmpty(cty.String)
return cty.NilVal
}

ctyMap := make(map[string]cty.Value)

for k, v := range m {
if validKey.MatchString(k) {
ctyMap[k] = cty.StringVal(ToString(v))
if !validKey.MatchString(k) {
continue
}
switch v.(type) {
case bool, float64, string:
ctyMap[k] = GoToValue(v)
case []interface{}:
ctyMap[k] = ListToValue(v.([]interface{}))
case map[string]interface{}:
ctyMap[k] = MapToValue(v.(map[string]interface{}))
default:
panic(reflect.TypeOf(v).String() + " not implemented")
}
}

if len(ctyMap) == 0 {
return cty.MapValEmpty(cty.String)
return cty.NilVal
}
return cty.MapVal(ctyMap)

return cty.ObjectVal(ctyMap)
}

func HeaderToMapValue(headers http.Header) cty.Value {
Expand Down

0 comments on commit 6b070a5

Please sign in to comment.