-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
includes parsing functionality for name/description, inputs and outputs as well as relevant tests
- Loading branch information
1 parent
218ca7c
commit 84b8d9c
Showing
10 changed files
with
220 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
Copyright © 2021 Matt Rose <[email protected]> | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
*/ | ||
package parser | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
|
||
"github.com/matty-rose/gha-docs/pkg/types" | ||
"github.com/mitchellh/mapstructure" | ||
"github.com/pkg/errors" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func Parse(filename string) (*types.CompositeAction, error) { | ||
file, err := ioutil.ReadFile(filename) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "couldn't read given yaml file") | ||
} | ||
|
||
data := make(map[interface{}]interface{}) | ||
|
||
err = yaml.Unmarshal(file, &data) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed unmarshalling yaml data") | ||
} | ||
|
||
var action types.CompositeAction | ||
|
||
parseMetadata(&action, data) | ||
|
||
err = parseInputs(&action, data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = parseOutputs(&action, data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &action, nil | ||
} | ||
|
||
func parseMetadata(action *types.CompositeAction, data map[interface{}]interface{}) { | ||
action.SetName(data["name"].(string)) | ||
action.SetDescription(data["description"].(string)) | ||
} | ||
|
||
func parseInputs(action *types.CompositeAction, data map[interface{}]interface{}) error { | ||
inputs, ok := data["inputs"].(map[string]interface{}) | ||
if !ok { | ||
// TODO: Replace with logrus | ||
fmt.Println("no inputs found") | ||
} | ||
|
||
for name, input := range inputs { | ||
inp := types.Input{Name: name} | ||
|
||
err := mapstructure.Decode(input, &inp) | ||
if err != nil { | ||
return errors.Wrap(err, "failed parsing action input into struct") | ||
} | ||
|
||
action.AddInput(inp) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func parseOutputs(action *types.CompositeAction, data map[interface{}]interface{}) error { | ||
outputs, ok := data["outputs"].(map[string]interface{}) | ||
if !ok { | ||
// TODO: Replace with logrus | ||
fmt.Println("no outputs found") | ||
} | ||
|
||
for name, output := range outputs { | ||
out := types.Output{Name: name} | ||
|
||
err := mapstructure.Decode(output, &out) | ||
if err != nil { | ||
return errors.Wrap(err, "failed parsing action output into struct") | ||
} | ||
|
||
action.AddOutput(out) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
Copyright © 2021 Matt Rose <[email protected]> | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
*/ | ||
package parser_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/matty-rose/gha-docs/pkg/parser" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestParseNameDescription(t *testing.T) { | ||
assertion := assert.New(t) | ||
|
||
action, err := parser.Parse("./testdata/name_description.yaml") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
assertion.Equal("test", action.Name) | ||
assertion.Equal("test", action.Description) | ||
} | ||
|
||
func TestParseInputs(t *testing.T) { | ||
assertion := assert.New(t) | ||
|
||
action, err := parser.Parse("./testdata/inputs.yaml") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
assertion.Len(action.Inputs, 2) | ||
} | ||
|
||
func TestParseOutputs(t *testing.T) { | ||
assertion := assert.New(t) | ||
|
||
action, err := parser.Parse("./testdata/outputs.yaml") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
assertion.Len(action.Outputs, 1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
name: "test" | ||
description: "test" | ||
|
||
inputs: | ||
input-a: | ||
description: "hello" | ||
required: false | ||
default: "a" | ||
input-b: | ||
description: "hello" | ||
required: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
name: "test" | ||
description: "test" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
name: "test" | ||
description: "test" | ||
|
||
outputs: | ||
output-a: | ||
description: "an output" | ||
value: "b" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters