forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.kv.go
71 lines (58 loc) · 1.2 KB
/
types.kv.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
//go:generate mapstructure-to-hcl2 -type KeyValue,KeyValues,KeyValueFilter,NameValue,NameValues,NameValueFilter
package hcl2template
type KeyValue struct {
Key string
Value string
}
type KeyValues []KeyValue
func (kvs KeyValues) CopyOn(to *map[string]string) []error {
if len(kvs) == 0 {
return nil
}
if *to == nil {
*to = map[string]string{}
}
for _, kv := range kvs {
(*to)[kv.Key] = kv.Value
}
return nil
}
type KeyValueFilter struct {
Filters map[string]string
Filter KeyValues
}
func (kvf *KeyValueFilter) Prepare() []error {
kvf.Filter.CopyOn(&kvf.Filters)
return nil
}
func (kvf *KeyValueFilter) Empty() bool {
return len(kvf.Filters) == 0
}
type NameValue struct {
Name string
Value string
}
type NameValues []NameValue
func (nvs NameValues) CopyOn(to *map[string]string) []error {
if len(nvs) == 0 {
return nil
}
if *to == nil {
*to = map[string]string{}
}
for _, kv := range nvs {
(*to)[kv.Name] = kv.Value
}
return nil
}
type NameValueFilter struct {
Filters map[string]string
Filter NameValues
}
func (nvf *NameValueFilter) Prepare() []error {
nvf.Filter.CopyOn(&nvf.Filters)
return nil
}
func (nvf *NameValueFilter) Empty() bool {
return len(nvf.Filters) == 0
}