Skip to content

Commit 30f81ca

Browse files
authored
Merge pull request #29 from jumppad-labs/f-defaults
Set default values on resources if set on struct annotation
2 parents 234cefe + 6c7509f commit 30f81ca

File tree

6 files changed

+31
-0
lines changed

6 files changed

+31
-0
lines changed

dag.go

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package hclconfig
33
import (
44
"fmt"
55

6+
"github.com/creasty/defaults"
67
"github.com/hashicorp/hcl/v2"
78
"github.com/hashicorp/hcl/v2/gohcl"
89

@@ -267,6 +268,9 @@ func createCallback(c *Config, wf WalkCallback) func(v dag.Vertex) (diags dag.Di
267268
ul := getContextLock(ctx)
268269
defer ul()
269270

271+
// if there are defaults defined on the resource set them
272+
defaults.Set(r)
273+
270274
diag := gohcl.DecodeBody(bdy, ctx, r)
271275
if diag.HasErrors() {
272276
pe := &errors.ParserError{}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ require (
2828
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
2929
github.com/aws/aws-sdk-go v1.55.5 // indirect
3030
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
31+
github.com/creasty/defaults v1.8.0 // indirect
3132
github.com/davecgh/go-spew v1.1.1 // indirect
3233
github.com/felixge/httpsnoop v1.0.4 // indirect
3334
github.com/go-logr/logr v1.4.2 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ github.com/cncf/xds/go v0.0.0-20210805033703-aa0b78936158/go.mod h1:eXthEFrGJvWH
221221
github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
222222
github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
223223
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
224+
github.com/creasty/defaults v1.8.0 h1:z27FJxCAa0JKt3utc0sCImAEb+spPucmKoOdLHvHYKk=
225+
github.com/creasty/defaults v1.8.0/go.mod h1:iGzKe6pbEHnpMPtfDXZEr0NVxWnPTjb1bbDy08fPzYM=
224226
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
225227
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
226228
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

parse_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,26 @@ func TestParseResolvesArrayReferences(t *testing.T) {
181181
require.Equal(t, float64(12), out.Value.([]interface{})[2].(float64))
182182
}
183183

184+
func TestParseSetsDefaultValues(t *testing.T) {
185+
absoluteFolderPath, err := filepath.Abs("./test_fixtures/defaults/container.hcl")
186+
if err != nil {
187+
t.Fatal(err)
188+
}
189+
190+
p := setupParser(t)
191+
192+
c, err := p.ParseFile(absoluteFolderPath)
193+
require.NoError(t, err)
194+
195+
r, err := c.FindResource("resource.container.default")
196+
require.NoError(t, err)
197+
require.NotNil(t, r)
198+
199+
// check default values have been set
200+
cont := r.(*structs.Container)
201+
require.Equal(t, "hello world", cont.Default)
202+
}
203+
184204
func TestLoadsVariableFilesInOptionsOverridingVariableDefaults(t *testing.T) {
185205
absoluteFolderPath, err := filepath.Abs("./test_fixtures/simple")
186206
require.NoError(t, err)

test_fixtures/defaults/container.hcl

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
resource "container" "default" {
2+
}

test_fixtures/structs/container.go

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ type Container struct {
1212
// embedded type holding name, etc
1313
types.ResourceBase `hcl:"rm,remain"`
1414

15+
Default string `hcl:"default,optional" json:"default,omitempty" default:"hello world"` // A default value
16+
1517
Networks []NetworkAttachment `hcl:"network,block" json:"networks,omitempty"` // Attach to the correct network // only when Image is specified
1618
NetworkObj Network `hcl:"networkobj,optional" json:"networkobj,omitempty"` // Reference to an object
1719

0 commit comments

Comments
 (0)