Skip to content

Commit

Permalink
Losen ASL parser rules to accept different resource names
Browse files Browse the repository at this point in the history
  • Loading branch information
enginyoyen committed May 10, 2020
1 parent ee1f7b7 commit ea60ee5
Show file tree
Hide file tree
Showing 9 changed files with 959 additions and 255 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,20 @@ go get github.com/enginyoyen/aslparser
```

## Usage
First argument is the file content to be validated, while the second argument is whether it should use strict mode for validation
```
stateMachine, err := aslparser.Parse(filePath)
stateMachine, err := aslparser.Parse(filePath, true)
if !stateMachine.Valid() {
for _, e := range stateMachine.Errors(){
fmt.Print(e.Description())
}
}
```

Alternatively, `ParseFile` method uses file path to load and validate

```
stateMachine, err := aslparser.ParseFile(filePath, true)
if !stateMachine.Valid() {
for _, e := range stateMachine.Errors(){
fmt.Print(e.Description())
Expand All @@ -23,7 +35,8 @@ if !stateMachine.Valid() {
## Converting JSON Schema to a static file
JSON schema is converted to an static go file to be included as an executable.
```
go-bindata -o state_machine_bin.go schemas/state-machine.json
go-bindata -o state_machine_bin.go schemas/state-machine.json schemas/state-machine-strict-arn.json
```

## JSON Schema
Expand Down
13 changes: 10 additions & 3 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,31 @@ type StateMachine struct {
}

// Given the file path validates and returns the StateMachine
func Parse(filepath string) (*StateMachine, error) {
// 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(payload)
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(payload, &stateMachine)
unmarshalErr := json.Unmarshal(content, &stateMachine)
if unmarshalErr != nil {
return &stateMachine, unmarshalErr
}
Expand Down
2 changes: 1 addition & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestParse(t *testing.T) {
}

func runInvalidParseCase(t *testing.T, path string, name string) {
stateMachine, _ := Parse(path)
stateMachine, _ := ParseFile(path, true)
if stateMachine.Valid() {
t.Errorf("Validation passed, where as suppose to fail for input %s", name)
}
Expand Down
Loading

0 comments on commit ea60ee5

Please sign in to comment.