Skip to content

Commit

Permalink
feat: add initial parser package
Browse files Browse the repository at this point in the history
includes parsing functionality for name/description, inputs and outputs
as well as relevant tests
  • Loading branch information
matty-rose committed Oct 9, 2021
1 parent 218ca7c commit 84b8d9c
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 6 deletions.
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ module github.com/matty-rose/gha-docs
go 1.17

require (
github.com/mitchellh/mapstructure v1.4.2
github.com/pkg/errors v0.8.1
github.com/spf13/cobra v1.2.1
github.com/spf13/viper v1.9.0
github.com/stretchr/testify v1.7.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mitchellh/mapstructure v1.4.2 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FI
github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM=
github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
109 changes: 109 additions & 0 deletions pkg/parser/parser.go
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
}
63 changes: 63 additions & 0 deletions pkg/parser/parser_test.go
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)
}
11 changes: 11 additions & 0 deletions pkg/parser/testdata/inputs.yaml
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
2 changes: 2 additions & 0 deletions pkg/parser/testdata/name_description.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: "test"
description: "test"
7 changes: 7 additions & 0 deletions pkg/parser/testdata/outputs.yaml
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"
16 changes: 16 additions & 0 deletions pkg/types/composite_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,19 @@ type CompositeAction struct {
Outputs []Output
ThirdPartyActions []ThirdPartyAction
}

func (c *CompositeAction) SetName(name string) {
c.Name = name
}

func (c *CompositeAction) SetDescription(description string) {
c.Description = description
}

func (c *CompositeAction) AddInput(input Input) {
c.Inputs = append(c.Inputs, input)
}

func (c *CompositeAction) AddOutput(output Output) {
c.Outputs = append(c.Outputs, output)
}
6 changes: 3 additions & 3 deletions pkg/types/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ package types
// Input represents a single input to a composite action.
type Input struct {
Name string
Description string
Required bool
Default string
Description string `mapstructure:"description"`
Required bool `mapstructure:"required"`
Default string `mapstructure:"default"`
}
4 changes: 2 additions & 2 deletions pkg/types/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ package types
// Output represents a single output of a composite action.
type Output struct {
Name string
Description string
Value string
Description string `mapstructure:"description"`
Value string `mapstructure:"value"`
}

0 comments on commit 84b8d9c

Please sign in to comment.