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

Implement gohcl.EvalContext #569

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Implement gohcl.EvalContext function and add simple test case.
  • Loading branch information
incubator4 committed Nov 7, 2022
commit 5400c06426ec4e3afe58cc73bd9ee6ac0eeb28d4
94 changes: 94 additions & 0 deletions gohcl/eval_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package gohcl

import (
"bytes"
"fmt"
"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"
"reflect"
)

func EvalContext(v interface{}) *hcl.EvalContext {
return &hcl.EvalContext{
Variables: structMapVal(v),
}
}

func structMapVal(v interface{}) map[string]cty.Value {
rt := reflect.TypeOf(v)
rv := reflect.ValueOf(v)

if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
}
if rv.Kind() == reflect.Ptr {
rv = rv.Elem()
}

var variables = make(map[string]cty.Value)

for index := 0; index < rt.NumField(); index++ {
key := rt.Field(index)
value := rv.Field(index)

if !value.IsZero() {
k := marshalKey(key.Name)
//k := key.Name
variables[k] = reflectVal(value)
}

}
return variables

}

func reflectVal(v reflect.Value) cty.Value {
switch v.Kind() {
case reflect.Int:
return cty.NumberIntVal(v.Int())
case reflect.String:
return cty.StringVal(v.String())
case reflect.Struct:
return structVal(v)
case reflect.Slice:
return sliceVal(v)
default:
panic(fmt.Sprintf("target value must be pointer to int, string, slice, struct or map, not %s", v.String()))
}
}

func sliceVal(v reflect.Value) cty.Value {
elems := []cty.Value{}
for i := 0; i < v.Len(); i++ {
elems = append(elems, reflectVal(v.Index(i)))
}
return cty.TupleVal(elems)
}

func structVal(v reflect.Value) cty.Value {
var ctyVals = make(map[string]cty.Value)
for index := 0; index < v.Type().NumField(); index++ {
key := v.Type().Field(index)
value := v.Field(index)
ctyVals[marshalKey(key.Name)] = reflectVal(value)
}
return cty.MapVal(ctyVals)
}

func marshalKey(input string) string {
if input == "" {
return ""
}
var output bytes.Buffer
for index, letter := range input {
if letter < 96 {
letter = letter + 32
if index > 0 {
output.WriteString("_")
}

}
output.WriteRune(letter)
}
return output.String()
}
91 changes: 91 additions & 0 deletions gohcl/eval_context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package gohcl

import (
"bytes"
"fmt"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"
"testing"
)

var (
valueComparer = cmp.Comparer(cty.Value.RawEquals)
)

func TestEvalContext(t *testing.T) {

type ServiceConfig struct {
Type string `hcl:"type,label"`
Name string `hcl:"name,label"`
ListenAddr string `hcl:"listen_addr"`
}
type Config struct {
IOMode string `hcl:"io_mode"`
Services []ServiceConfig `hcl:"service,block"`
}

type Context struct {
Pid string
}

tests := []struct {
Input interface{}
Output hcl.EvalContext
}{
{
Input: &Context{
Pid: "fake-pid",
},
Output: hcl.EvalContext{
Variables: map[string]cty.Value{
"pid": cty.StringVal("fake-pid"),
},
},
},
{
Input: &Config{
IOMode: "fake-mode",
Services: []ServiceConfig{
{
Type: "t",
Name: "n",
ListenAddr: "addr",
},
},
},
Output: hcl.EvalContext{
Variables: map[string]cty.Value{
"i_o_mode": cty.StringVal("fake-mode"),
"services": cty.TupleVal([]cty.Value{
cty.MapVal(map[string]cty.Value{
"type": cty.StringVal("t"),
"name": cty.StringVal("n"),
"listen_addr": cty.StringVal("addr"),
}),
}),
},
},
},
}

for index, test := range tests {
t.Run(fmt.Sprintf("test-%d", index), func(t *testing.T) {
realOutput := EvalContext(test.Input)

gotVal := realOutput.Variables
wantVal := test.Output.Variables

if !cmp.Equal(gotVal, wantVal, valueComparer) {
diff := cmp.Diff(gotVal, wantVal, cmp.Comparer(func(a, b []byte) bool {
return bytes.Equal(a, b)
}))
t.Errorf(
"wrong result\nvalue: %#v\ngot: %#v\nwant: %#v\ndiff: %s",
test.Input, gotVal, wantVal, diff,
)
}

})
}
}