This repository has been archived by the owner on Jan 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
func.go
112 lines (103 loc) · 2.63 KB
/
func.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
package cloudformation
import (
"encoding/json"
"fmt"
)
// Func is an interface provided by objects that represent Cloudformation
// function calls.
type Func interface {
}
// BoolFunc is an interface provided by objects that represent Cloudformation
// function that can return a boolean value.
type BoolFunc interface {
Func
Bool() *BoolExpr
}
// IntegerFunc is an interface provided by objects that represent Cloudformation
// function that can return an integer value.
type IntegerFunc interface {
Func
Integer() *IntegerExpr
}
// StringFunc is an interface provided by objects that represent Cloudformation
// function that can return a string value.
type StringFunc interface {
Func
String() *StringExpr
}
// StringListFunc is an interface provided by objects that represent Cloudformation
// function that can return a list of strings.
type StringListFunc interface {
Func
StringList() *StringListExpr
}
// UnknownFunctionError is returned by various UnmarshalJSON
// functions when they encounter a function that is not
// implemented.
type UnknownFunctionError struct {
Name string
}
func (ufe UnknownFunctionError) Error() string {
return fmt.Sprintf("unknown function %s", ufe.Name)
}
// unmarshalFunc unmarshals data into a Func, or returns an error
// if the function call is invalid.
func unmarshalFunc(data []byte) (Func, error) {
rawDecode := map[string]json.RawMessage{}
err := json.Unmarshal(data, &rawDecode)
if err != nil {
return nil, err
}
for key := range rawDecode {
switch key {
case "Ref":
f := RefFunc{}
if err := json.Unmarshal(data, &f); err == nil {
return f, nil
}
case "Fn::Join":
f := JoinFunc{}
if err := json.Unmarshal(data, &f); err == nil {
return f, nil
}
case "Fn::Select":
f := SelectFunc{}
if err := json.Unmarshal(data, &f); err == nil {
return f, nil
}
case "Fn::GetAtt":
f := GetAttFunc{}
if err := json.Unmarshal(data, &f); err == nil {
return f, nil
}
case "Fn::FindInMap":
f := FindInMapFunc{}
if err := json.Unmarshal(data, &f); err == nil {
return f, nil
}
case "Fn::Base64":
f := Base64Func{}
if err := json.Unmarshal(data, &f); err == nil {
return f, nil
}
case "Fn::GetAZs":
f := GetAZsFunc{}
if err := json.Unmarshal(data, &f); err == nil {
return f, nil
}
case "Fn::If":
f := IfFunc{}
if err := json.Unmarshal(data, &f); err == nil {
return f, nil
}
case "Fn::ImportValue":
f := ImportValueFunc{}
if err := json.Unmarshal(data, &f); err == nil {
return f, nil
}
default:
return nil, UnknownFunctionError{Name: key}
}
}
return nil, fmt.Errorf("cannot decode function")
}