-
Notifications
You must be signed in to change notification settings - Fork 11
/
hcl.go
123 lines (102 loc) · 3.47 KB
/
hcl.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
package hcl
import (
"bytes"
"io/ioutil"
"reflect"
"github.com/coveo/gotemplate/collections"
"github.com/coveo/gotemplate/collections/implementation"
"github.com/coveo/gotemplate/errors"
"github.com/hashicorp/hcl"
)
// Expose hcl public objects
var (
Decode = hcl.Decode
DecodeObject = hcl.DecodeObject
Parse = hcl.Parse
ParseBytes = hcl.ParseBytes
ParseString = hcl.ParseString
)
func (l hclList) String() string {
result, err := MarshalInternal(l.AsArray())
if err != nil {
panic(err)
}
return string(result)
}
func (d hclDict) String() string {
result, err := Marshal(d.AsMap())
if err != nil {
panic(err)
}
return string(result)
}
func (l hclList) PrettyPrint() string {
result, _ := MarshalIndent(l.AsArray(), "", " ")
return string(result)
}
func (d hclDict) PrettyPrint() string {
result, _ := MarshalIndent(d.AsMap(), "", " ")
return string(result)
}
var _ = func() int {
collections.TypeConverters["hcl"] = Unmarshal
return 0
}()
// Unmarshal adds support to single array and struct representation
func Unmarshal(bs []byte, out interface{}) (err error) {
defer func() { err = errors.Trap(err, recover()) }()
bs = bytes.TrimSpace(bs)
if err = hcl.Unmarshal(bs, out); err != nil {
bs = append([]byte("_="), bs...)
var temp hclDict
if errInternalHcl := hcl.Unmarshal(bs, &temp); errInternalHcl != nil {
return err
}
err = nil
reflect.ValueOf(out).Elem().Set(reflect.ValueOf(temp["_"]))
}
transform(out)
return
}
// Load loads hcl file into variable
func Load(filename string) (result interface{}, err error) {
var content []byte
if content, err = ioutil.ReadFile(filename); err == nil {
err = Unmarshal(content, &result)
}
return
}
// Marshal serialize values to hcl format
func Marshal(value interface{}) ([]byte, error) { return MarshalIndent(value, "", "") }
// MarshalIndent serialize values to hcl format with indentation
func MarshalIndent(value interface{}, prefix, indent string) ([]byte, error) {
result, err := marshalHCL(collections.ToNativeRepresentation(value), true, true, prefix, indent)
return []byte(result), err
}
// MarshalInternal serialize values to hcl format for result used in outer hcl struct
func MarshalInternal(value interface{}) ([]byte, error) {
result, err := marshalHCL(collections.ToNativeRepresentation(value), false, false, "", "")
return []byte(result), err
}
// MarshalTFVars serialize values to hcl format (without hcl map format)
func MarshalTFVars(value interface{}) ([]byte, error) { return MarshalTFVarsIndent(value, "", "") }
// MarshalTFVarsIndent serialize values to hcl format with indentation (without hcl map format)
func MarshalTFVarsIndent(value interface{}, prefix, indent string) ([]byte, error) {
result, err := marshalHCL(collections.ToNativeRepresentation(value), false, true, prefix, indent)
return []byte(result), err
}
// SingleContext converts array of 1 to single object otherwise, let the context unchanged
func SingleContext(context ...interface{}) interface{} {
if len(context) == 1 {
return context[0]
}
return context
}
type (
helperBase = implementation.BaseHelper
helperList = implementation.ListHelper
helperDict = implementation.DictHelper
)
var needConversionImpl = implementation.NeedConversion
//go:generate genny -pkg=hcl -in=../collections/implementation/generic.go -out=generated_impl.go gen "ListTypeName=List DictTypeName=Dictionary base=hcl"
//go:generate genny -pkg=hcl -in=../collections/implementation/generic_test.go -out=generated_test.go gen "base=hcl"