Skip to content

Commit 22c9031

Browse files
Enable embedding
1 parent 68281b8 commit 22c9031

File tree

7 files changed

+493
-228
lines changed

7 files changed

+493
-228
lines changed

convert/convert.go

+22-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package convert
22

33
import (
4+
"fmt"
5+
46
"github.com/jumppad-labs/hclconfig/types"
57
"github.com/zclconf/go-cty/cty"
68
"github.com/zclconf/go-cty/cty/gocty"
@@ -18,24 +20,36 @@ func GoToCtyValue(val interface{}) (cty.Value, error) {
1820
}
1921

2022
if r, ok := val.(types.Resource); ok {
21-
typ, err := gocty.ImpliedType(r.Metadata())
23+
ctyMap := ctyVal.AsValueMap()
24+
25+
// add disabled to the parent
26+
ctyMap["disabled"] = cty.BoolVal(r.GetDisabled())
27+
28+
// add depends_on to the parent
29+
depTyp, err := gocty.ImpliedType(r.GetDependsOn())
2230
if err != nil {
2331
return cty.False, err
2432
}
2533

26-
metaVal, err := gocty.ToCtyValue(r.Metadata(), typ)
34+
dep, err := gocty.ToCtyValue(r.GetDependsOn(), depTyp)
2735
if err != nil {
28-
return cty.False, err
36+
return cty.False, fmt.Errorf("unable to convert depends_on to cty: %s", err)
2937
}
38+
ctyMap["depends_on"] = dep
3039

31-
objMap := ctyVal.AsValueMap()
32-
metaMap := metaVal.AsValueMap()
40+
// add the meta properties to the parent
41+
typ, err := gocty.ImpliedType(r.Metadata())
42+
if err != nil {
43+
return cty.False, err
44+
}
3345

34-
for k, v := range metaMap {
35-
objMap[k] = v
46+
metaVal, err := gocty.ToCtyValue(r.Metadata(), typ)
47+
if err != nil {
48+
return cty.False, err
3649
}
3750

38-
ctyVal = cty.ObjectVal(objMap)
51+
ctyMap["meta"] = metaVal
52+
ctyVal = cty.ObjectVal(ctyMap)
3953
}
4054

4155
return ctyVal, nil

dag.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func createCallback(c *Config, wf WalkCallback) func(v dag.Vertex) (diags dag.Di
191191
// all linked values should now have been processed as the graph
192192
// will have handled them first
193193
for _, v := range r.Metadata().Links {
194-
fqdn, err := types.ParseFQRN(v)
194+
fqrn, err := types.ParseFQRN(v)
195195
if err != nil {
196196
pe := errors.ParserError{}
197197
pe.Filename = r.Metadata().File
@@ -243,9 +243,9 @@ func createCallback(c *Config, wf WalkCallback) func(v dag.Vertex) (diags dag.Di
243243
}
244244

245245
// remove the attributes and to get a pure resource ref
246-
fqdn.Attribute = ""
246+
fqrn.Attribute = ""
247247

248-
err = setContextVariableFromPath(ctx, fqdn.String(), ctyRes)
248+
err = setContextVariableFromPath(ctx, fqrn.String(), ctyRes)
249249
if err != nil {
250250
pe := errors.ParserError{}
251251
pe.Filename = r.Metadata().File

errors/parser_error.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ func (p ParserError) Error() string {
5353
codeline := wordwrap.WrapString(lines[i], 70)
5454
codelines := strings.Split(codeline, "\n")
5555

56-
if i == p.Line-1 {
56+
if i == p.Line {
5757
err.WriteString(fmt.Sprintf("\033[1m %5d | %s\033[0m\n", i+1, codelines[0]))
5858
} else {
5959
err.WriteString(fmt.Sprintf("\033[2m %5d | %s\033[0m\n", i+1, codelines[0]))
6060
}
6161

6262
for _, l := range codelines[1:] {
63-
if i == p.Line-1 {
63+
if i == p.Line {
6464
err.WriteString(fmt.Sprintf("\033[1m : %s\033[0m\n", l))
6565
} else {
6666
err.WriteString(fmt.Sprintf("\033[2m : %s\033[0m\n", l))

example/config.hcl

+26
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ resource "postgres" "other1" {
3030
}
3131

3232
resource "postgres" "other2" {
33+
depends_on = ["resource.postgres.other1"]
34+
disabled = false
35+
36+
id = "bollocks"
3337
location = "2.other.location"
3438
port = 5432
3539
db_name = "other2"
@@ -38,6 +42,8 @@ resource "postgres" "other2" {
3842
// by values set by the environment variables HCL_db_username and HCL_db_password
3943
username = variable.db_username
4044
password = variable.db_password
45+
46+
erik_is_a = "cunt"
4147
}
4248

4349
resource "config" "myapp" {
@@ -66,6 +72,26 @@ resource "config" "myapp" {
6672
}
6773
}
6874

75+
output "erik" {
76+
value = resource.postgres.other2.erik_is_a
77+
}
78+
79+
output "id" {
80+
value = resource.postgres.other2.id
81+
}
82+
83+
output "disabled" {
84+
value = resource.postgres.other2.disabled
85+
}
86+
87+
output "depends_on" {
88+
value = resource.postgres.other2.depends_on
89+
}
90+
91+
output "meta_id" {
92+
value = resource.postgres.other2.meta.id
93+
}
94+
6995
// modules can use a git ref to be remotely downloaded from the source
7096
//module "mymodule_1" {
7197
// source = "github.com/jumppad-labs/hclconfig?ref=9173050/example/modules//db"

0 commit comments

Comments
 (0)