-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparser.go
152 lines (132 loc) · 3.81 KB
/
parser.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Copyright (c) 2020 Engin Yöyen.
* Use of this source code is governed by an MIT
* license that can be found in the LICENSE file.
*/
package aslparser
import (
"encoding/json"
"github.com/xeipuuv/gojsonschema"
"io/ioutil"
)
type Retry struct {
ErrorEquals []string
IntervalSeconds int
BackoffRate int
MaxAttempts int
}
type Catch struct {
ErrorEquals []string
ResultPath string
Next string
}
type State struct {
Comment string
Type string
Next string
Default string
Resource string
End bool
Parameters map[string]interface{}
Retry []Retry
Catch []Catch
TimeoutSeconds int
HeartbeatSeconds int
}
type StateMachine struct {
Comment string
StartAt string
Version string
States map[string]State
validationResult *gojsonschema.Result
}
// Given the file path validates and returns the StateMachine
// strict argument defines whether Resource name must be AWS ARN pattern or not
func ParseFile(filepath string, strict bool) (*StateMachine, error) {
//load file
payload, fileErr := ioutil.ReadFile(filepath)
if fileErr != nil {
return nil, fileErr
}
return Parse(payload, strict)
}
// Given the file content validates and returns the StateMachine
// strict argument defines whether Resource name must be AWS ARN pattern or not
func Parse(content []byte, strict bool) (*StateMachine, error) {
// validate it, if there is an error or document is not Valid
// return the result without further analysis
var stateMachine StateMachine
validationResult, valErr := Validate(content, strict)
stateMachine.validationResult = validationResult
if valErr != nil || !validationResult.Valid() {
return &stateMachine, valErr
}
// given state-machine payload is valid, unmarshal the json file
unmarshalErr := json.Unmarshal(content, &stateMachine)
if unmarshalErr != nil {
return &stateMachine, unmarshalErr
}
// find and register non-semantic errors
stateMachine.findAndRegisterSchemaErrors()
return &stateMachine, nil
}
// Valid returns true if no errors were found
func (s *StateMachine) Valid() bool {
return s.validationResult.Valid()
}
// Errors returns the errors that were found
func (s *StateMachine) Errors() []gojsonschema.ResultError {
return s.validationResult.Errors()
}
func (s *StateMachine) findAndRegisterSchemaErrors() {
missingStates := *s.findNonSchemaErrors()
for k, v := range missingStates {
err := new(gojsonschema.MissingDependencyError)
err.SetType("missing_dependency")
err.SetDescriptionFormat(gojsonschema.Locale.MissingDependency())
details := gojsonschema.ErrorDetails{
"dependency": k + ". " + v,
}
err.SetDetails(details)
s.validationResult.AddError(err, details)
}
}
func (s *StateMachine) findNonSchemaErrors() *map[string]string {
var errors = make(map[string]string)
if !s.statePresent(s.StartAt) {
errors[s.StartAt] = "Missing 'StartAt' transition target. Could not locate " + s.StartAt
}
for k, v := range s.States {
if !s.targetStateRegistered(k) {
errors[k] = k + " is defined but is not reachable."
}
if len(v.Next) > 0 && !s.statePresent(v.Next) {
errors[k] = v.Next + " as Next,defined in " + k + ", but not reachable"
}
if len(v.Default) > 0 && !s.statePresent(v.Default) {
errors[k] = v.Default + " as Default, defined in " + k + ", but not reachable"
}
}
return &errors
}
func (s *StateMachine) statePresent(state string) bool {
_, present := s.States[state]
return present
}
func (s *StateMachine) targetStateRegistered(state string) bool {
if s.StartAt == state {
return true
}
match := false
for _, v := range s.States {
if len(v.Next) > 0 && v.Next == state {
match = true
break
}
if len(v.Default) > 0 && v.Next == state {
match = true
break
}
}
return match
}