Skip to content

Commit

Permalink
Enable embedding
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasjackson committed Feb 28, 2024
1 parent 68281b8 commit ebe8eb6
Show file tree
Hide file tree
Showing 7 changed files with 493 additions and 228 deletions.
30 changes: 22 additions & 8 deletions convert/convert.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package convert

import (
"fmt"

"github.com/jumppad-labs/hclconfig/types"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/gocty"
Expand All @@ -18,24 +20,36 @@ func GoToCtyValue(val interface{}) (cty.Value, error) {
}

if r, ok := val.(types.Resource); ok {
typ, err := gocty.ImpliedType(r.Metadata())
ctyMap := ctyVal.AsValueMap()

// add disabled to the parent
ctyMap["disabled"] = cty.BoolVal(r.GetDisabled())

// add depends_on to the parent
depTyp, err := gocty.ImpliedType(r.GetDependsOn())
if err != nil {
return cty.False, err
}

metaVal, err := gocty.ToCtyValue(r.Metadata(), typ)
dep, err := gocty.ToCtyValue(r.GetDependsOn(), depTyp)
if err != nil {
return cty.False, err
return cty.False, fmt.Errorf("unable to convert depends_on to cty: %s", err)
}
ctyMap["depends_on"] = dep

objMap := ctyVal.AsValueMap()
metaMap := metaVal.AsValueMap()
// add the meta properties to the parent
typ, err := gocty.ImpliedType(r.Metadata())
if err != nil {
return cty.False, err
}

for k, v := range metaMap {
objMap[k] = v
metaVal, err := gocty.ToCtyValue(r.Metadata(), typ)
if err != nil {
return cty.False, err
}

ctyVal = cty.ObjectVal(objMap)
ctyMap["meta"] = metaVal
ctyVal = cty.ObjectVal(ctyMap)
}

return ctyVal, nil
Expand Down
6 changes: 3 additions & 3 deletions dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func createCallback(c *Config, wf WalkCallback) func(v dag.Vertex) (diags dag.Di
// all linked values should now have been processed as the graph
// will have handled them first
for _, v := range r.Metadata().Links {
fqdn, err := types.ParseFQRN(v)
fqrn, err := types.ParseFQRN(v)
if err != nil {
pe := errors.ParserError{}
pe.Filename = r.Metadata().File
Expand Down Expand Up @@ -243,9 +243,9 @@ func createCallback(c *Config, wf WalkCallback) func(v dag.Vertex) (diags dag.Di
}

// remove the attributes and to get a pure resource ref
fqdn.Attribute = ""
fqrn.Attribute = ""

err = setContextVariableFromPath(ctx, fqdn.String(), ctyRes)
err = setContextVariableFromPath(ctx, fqrn.String(), ctyRes)
if err != nil {
pe := errors.ParserError{}
pe.Filename = r.Metadata().File
Expand Down
4 changes: 2 additions & 2 deletions errors/parser_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ func (p ParserError) Error() string {
codeline := wordwrap.WrapString(lines[i], 70)
codelines := strings.Split(codeline, "\n")

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

for _, l := range codelines[1:] {
if i == p.Line-1 {
if i == p.Line {
err.WriteString(fmt.Sprintf("\033[1m : %s\033[0m\n", l))
} else {
err.WriteString(fmt.Sprintf("\033[2m : %s\033[0m\n", l))
Expand Down
26 changes: 26 additions & 0 deletions example/config.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ resource "postgres" "other1" {
}

resource "postgres" "other2" {
depends_on = ["resource.postgres.other1"]
disabled = false

id = "lekker.broodje"
location = "2.other.location"
port = 5432
db_name = "other2"
Expand All @@ -38,6 +42,8 @@ resource "postgres" "other2" {
// by values set by the environment variables HCL_db_username and HCL_db_password
username = variable.db_username
password = variable.db_password

erik_is_a = "a Dutchman"
}

resource "config" "myapp" {
Expand Down Expand Up @@ -66,6 +72,26 @@ resource "config" "myapp" {
}
}

output "erik" {
value = resource.postgres.other2.erik_is_a
}

output "id" {
value = resource.postgres.other2.id
}

output "disabled" {
value = resource.postgres.other2.disabled
}

output "depends_on" {
value = resource.postgres.other2.depends_on
}

output "meta_id" {
value = resource.postgres.other2.meta.id
}

// modules can use a git ref to be remotely downloaded from the source
//module "mymodule_1" {
// source = "github.com/jumppad-labs/hclconfig?ref=9173050/example/modules//db"
Expand Down

0 comments on commit ebe8eb6

Please sign in to comment.