Skip to content

Commit 12acb40

Browse files
authored
Dcarbone/duration config value (#3)
* adding time.Duration as default ConfigValue func * adding test workflow
1 parent e145dbb commit 12acb40

File tree

5 files changed

+99
-30
lines changed

5 files changed

+99
-30
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @dcarbone

.github/dependabot.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "github-actions"
4+
directory: "/"
5+
schedule:
6+
interval: "daily"
7+
- package-ecosystem: "gomod"
8+
directory: "/"
9+
schedule:
10+
interval: "daily"

.github/workflows/tests.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
on:
2+
pull_request:
3+
paths:
4+
- "**.go"
5+
6+
push:
7+
branches:
8+
- "main"
9+
paths:
10+
- ".github/workflows/tests.yaml"
11+
- "**.go"
12+
13+
jobs:
14+
tests:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v3
18+
- uses: actions/setup-go@v3
19+
with:
20+
go-version: 1.17
21+
22+
- run: go mod tidy -compat=1.17 && go mod vendor
23+
- working-directory: acctest
24+
run: go test -v

acctest/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"strings"
66
"sync"
7+
"time"
78

89
"github.com/dcarbone/terraform-plugin-framework-utils/internal/util"
910
)
@@ -52,6 +53,12 @@ func DefaultConfigValueFuncs() map[string]ConfigValueFunc {
5253
}
5354
},
5455

56+
// time values
57+
58+
util.KeyFN(time.Nanosecond): func(v interface{}) string {
59+
return ConfigValue(v.(time.Duration).String())
60+
},
61+
5562
// slices
5663

5764
util.KeyFN(make([]interface{}, 0)): func(v interface{}) string {

acctest/config_test.go

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,66 @@ import (
55
"math"
66
"reflect"
77
"testing"
8+
"time"
89

910
"github.com/dcarbone/terraform-plugin-framework-utils/acctest"
1011
)
1112

1213
func TestConfigValue_Defaults(t *testing.T) {
1314
type convTest struct {
14-
in interface{}
15-
out interface{}
15+
name string
16+
in interface{}
17+
out interface{}
1618
}
1719

1820
theTests := []convTest{
1921
{
20-
in: nil,
21-
out: "null",
22+
name: "nil",
23+
in: nil,
24+
out: "null",
2225
},
2326
{
24-
in: acctest.ConfigLiteral("local.whateverthing"),
25-
out: "local.whateverthing",
27+
name: "config-literal",
28+
in: acctest.ConfigLiteral("local.whateverthing"),
29+
out: "local.whateverthing",
2630
},
2731
{
28-
in: true,
29-
out: "true",
32+
name: "bool-true",
33+
in: true,
34+
out: "true",
3035
},
3136
{
32-
in: false,
33-
out: "false",
37+
name: "bool-false",
38+
in: false,
39+
out: "false",
3440
},
3541
{
36-
in: math.MaxInt16,
37-
out: fmt.Sprintf("%d", math.MaxInt16),
42+
name: "int-positive",
43+
in: math.MaxInt16,
44+
out: fmt.Sprintf("%d", math.MaxInt16),
3845
},
3946
{
40-
in: -5,
41-
out: "-5",
47+
name: "int-negative",
48+
in: -5,
49+
out: "-5",
4250
},
4351
{
44-
in: 0.5,
45-
out: fmt.Sprintf("%f", 0.5),
52+
name: "float-positive",
53+
in: 0.5,
54+
out: fmt.Sprintf("%f", 0.5),
4655
},
4756
{
48-
in: "single-line",
49-
out: `"single-line"`,
57+
name: "float-negative",
58+
in: -0.5,
59+
out: fmt.Sprintf("%f", -0.5),
5060
},
5161
{
62+
name: "string-literal",
63+
in: "single-line",
64+
out: `"single-line"`,
65+
},
66+
{
67+
name: "string-heredoc",
5268
in: `multi
5369
line`,
5470
out: `<<EOD
@@ -57,28 +73,37 @@ line
5773
EOD`,
5874
},
5975
{
60-
in: []interface{}{"hello", 5},
76+
name: "duration-to-string",
77+
in: time.Nanosecond,
78+
out: `"1ns"`,
79+
},
80+
{
81+
name: "slice-interface",
82+
in: []interface{}{"hello", 5},
6183
out: `[
6284
"hello",
6385
5
6486
]`,
6587
},
6688
{
67-
in: []string{"hello", "there"},
89+
name: "slice-string",
90+
in: []string{"hello", "there"},
6891
out: `[
6992
"hello",
7093
"there"
7194
]`,
7295
},
7396
{
74-
in: []int{1, 5},
97+
name: "slice-int",
98+
in: []int{1, 5},
7599
out: `[
76100
1,
77101
5
78102
]`,
79103
},
80104
{
81-
in: []float64{1, 5},
105+
name: "slice-float64",
106+
in: []float64{1, 5},
82107
out: `[
83108
` + fmt.Sprintf("%f", float64(1)) + `,
84109
` + fmt.Sprintf("%f", float64(5)) + `
@@ -89,14 +114,16 @@ EOD`,
89114
// todo: use biggerer brain to figure out how to test map -> string verification
90115

91116
for _, theT := range theTests {
92-
out := acctest.ConfigValue(theT.in)
93-
if !reflect.DeepEqual(out, theT.out) {
94-
t.Log("Output does not match expected")
95-
t.Logf("Input: %v", theT.in)
96-
t.Logf("Expected: %v", theT.out)
97-
t.Logf("Actual: %v", out)
98-
t.Fail()
99-
}
117+
t.Run(theT.name, func(t *testing.T) {
118+
out := acctest.ConfigValue(theT.in)
119+
if !reflect.DeepEqual(out, theT.out) {
120+
t.Log("Output does not match expected")
121+
t.Logf("Input: %v", theT.in)
122+
t.Logf("Expected: %v", theT.out)
123+
t.Logf("Actual: %v", out)
124+
t.Fail()
125+
}
126+
})
100127
}
101128
}
102129

0 commit comments

Comments
 (0)