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

map[string]interface{} encoding #489

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 30 additions & 11 deletions gohcl/encode.go
Expand Up @@ -2,6 +2,7 @@ package gohcl

import (
"fmt"
"github.com/zclconf/go-cty/cty"
"reflect"
"sort"

Expand Down Expand Up @@ -64,6 +65,18 @@ func EncodeAsBlock(val interface{}, blockType string) *hclwrite.Block {
rv = rv.Elem()
ty = rv.Type()
}

if(ty.Kind() == reflect.Map){
block := hclwrite.NewBlock(blockType, nil)
body := block.Body()
for _, e := range rv.MapKeys() {
v := rv.MapIndex(e)
fieldVal := getFieldVal(v)
body.SetAttributeValue(e.String(), fieldVal)
}
return block
}

if ty.Kind() != reflect.Struct {
panic(fmt.Sprintf("value is %s, not struct", ty.Kind()))
}
Expand Down Expand Up @@ -128,17 +141,7 @@ func populateBody(rv reflect.Value, ty reflect.Type, tags *fieldTags, dst *hclwr
prevWasBlock = false
}

valTy, err := gocty.ImpliedType(fieldVal.Interface())
if err != nil {
panic(fmt.Sprintf("cannot encode %T as HCL expression: %s", fieldVal.Interface(), err))
}

val, err := gocty.ToCtyValue(fieldVal.Interface(), valTy)
if err != nil {
// This should never happen, since we should always be able
// to decode into the implied type.
panic(fmt.Sprintf("failed to encode %T as %#v: %s", fieldVal.Interface(), valTy, err))
}
val := getFieldVal(fieldVal)

dst.SetAttributeValue(name, val)

Expand Down Expand Up @@ -189,3 +192,19 @@ func populateBody(rv reflect.Value, ty reflect.Type, tags *fieldTags, dst *hclwr
}
}
}

func getFieldVal(fieldVal reflect.Value) cty.Value {
valTy, err := gocty.ImpliedType(fieldVal.Interface())
if err != nil {
panic(fmt.Sprintf("cannot encode %T as HCL expression: %s", fieldVal.Interface(), err))
}

val, err := gocty.ToCtyValue(fieldVal.Interface(), valTy)
if err != nil {
// This should never happen, since we should always be able
// to decode into the implied type.
panic(fmt.Sprintf("failed to encode %T as %#v: %s", fieldVal.Interface(), valTy, err))
}

return val
}
12 changes: 12 additions & 0 deletions gohcl/encode_test.go
Expand Up @@ -21,6 +21,8 @@ func ExampleEncodeIntoBody() {
Desc string `hcl:"description"`
Constraints *Constraints `hcl:"constraints,block"`
Services []Service `hcl:"service,block"`
Config map[string]interface{} `hcl:"config,block"`
Test map[string]interface{} `hcl:"test,block"`
}

app := App{
Expand All @@ -40,6 +42,8 @@ func ExampleEncodeIntoBody() {
Exe: []string{"./worker"},
},
},
Config: map[string]interface{}{"image": "dockerimage"},
Test: map[string]interface{}{"strings": []string{"test","2134"}},
}

f := hclwrite.NewEmptyFile()
Expand All @@ -61,4 +65,12 @@ func ExampleEncodeIntoBody() {
// service "worker" {
// executable = ["./worker"]
// }
//
//config {
// image = "dockerimage"
//}
//
//test {
// strings = ["test", "2134"]
//}
}