-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
138 lines (111 loc) · 2.33 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"fmt"
"github.com/hashicorp/hcl"
gohcl2 "github.com/hashicorp/hcl2/gohcl"
hcl2parse "github.com/hashicorp/hcl2/hclparse"
"github.com/y0ssar1an/q"
)
var buf []byte = []byte(`
suitename = "suite1"
testcase {
casename = "case1"
step {
stepname = "step1"
}
step {
stepname = "step2"
}
}
testcase {
casename = "case2"
}
`)
func main() {
simpleStructTest()
hclTest()
hcl2Test()
hcl2TestSchema()
}
func simpleStructTest() {
foo := []map[string]interface{}{}
if err := hcl.Unmarshal(buf, &foo); err != nil {
panic(fmt.Sprintf("bad: %v", err))
}
q.Q(foo)
}
func hclTest() {
type TestStep struct {
Name string `hcl:"stepname"`
}
type TestCase struct {
Stepname string `hcl:"name",key=casename`
TestSteps []TestStep `hcl:"step",squash,key=stepname`
}
type TestSuite struct {
Name string `hcl:"suitename"`
TestCases []TestCase `hcl:"testcase"`
}
{
ts := TestSuite{}
if err := hcl.Unmarshal(buf, &ts); err != nil {
panic(fmt.Sprintf("bad: %v", err))
}
q.Q(ts)
}
{
tc := []TestCase{}
if err := hcl.Unmarshal(buf, &tc); err != nil {
panic(fmt.Sprintf("bad: %v", err))
}
q.Q(tc)
}
}
func hcl2Test() {
type TestStep struct {
Name string `hcl:"stepname"`
}
type TestCase struct {
Stepname string `hcl:"casename"`
TestSteps []TestStep `hcl:"step,block"`
}
type TestSuite struct {
Name string `hcl:"suitename"`
TestCases []TestCase `hcl:"testcase,block"`
}
p := hcl2parse.NewParser()
f, err := p.ParseHCL(buf, "stdin.hcl")
if err != nil {
panic(fmt.Sprintf("bad parse: %v", err))
}
ts := TestSuite{}
diags := gohcl2.DecodeBody(f.Body, nil, &ts)
if diags.HasErrors() {
panic(fmt.Sprintf("bad decode: %v\n%v", diags, ts))
}
q.Q(ts)
}
func hcl2TestSchema() {
type TestStep struct {
Name string `hcl:"stepname"`
}
type TestCase struct {
Stepname string `hcl:"casename"`
TestSteps []TestStep `hcl:"step,block"`
}
type TestSuite struct {
Name string `hcl:"suitename"`
TestCases []TestCase `hcl:"testcase,block"`
}
p := hcl2parse.NewParser()
f, err := p.ParseHCL(buf, "stdin.hcl")
if err != nil {
panic(fmt.Sprintf("bad parse: %v", err))
}
ts := TestSuite{}
diags := gohcl2.DecodeBody(f.Body, nil, &ts)
if diags.HasErrors() {
panic(fmt.Sprintf("bad decode: %v\n%v", diags, ts))
}
q.Q(ts)
}