Skip to content

Commit

Permalink
Simplified the 'WorkflowMap' type alias and unmarshalling.
Browse files Browse the repository at this point in the history
  • Loading branch information
wbreza committed Jan 18, 2024
1 parent 2aa20eb commit 788af17
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 51 deletions.
2 changes: 1 addition & 1 deletion cli/azd/cmd/up.go
Expand Up @@ -105,7 +105,7 @@ func (u *upAction) Run(ctx context.Context) (*actions.ActionResult, error) {

startTime := time.Now()

upWorkflow, has := u.projectConfig.Workflows.Get("up")
upWorkflow, has := u.projectConfig.Workflows["up"]
if !has {
upWorkflow = defaultUpWorkflow
} else {
Expand Down
38 changes: 3 additions & 35 deletions cli/azd/pkg/workflow/config.go
@@ -1,40 +1,7 @@
package workflow

// Stores a map of workflows configured for an azd project
type WorkflowMap struct {
inner map[string]*Workflow
}

// NewWorkflowMap creates a new WorkflowMap.
func NewWorkflowMap() *WorkflowMap {
return &WorkflowMap{
inner: map[string]*Workflow{},
}
}

// Set adds or updates a key-value pair in the map.
func (wm *WorkflowMap) Set(key string, value *Workflow) {
if wm.inner == nil {
wm.inner = map[string]*Workflow{}
}

wm.inner[key] = value
}

// Get retrieves the value for a given key from the map.
func (wm *WorkflowMap) Get(key string) (*Workflow, bool) {
if wm.inner == nil {
wm.inner = map[string]*Workflow{}
}

val, ok := wm.inner[key]
return val, ok
}

// MarshalYAML marshals the WorkflowMap into YAML.
func (wm *WorkflowMap) MarshalYAML() (interface{}, error) {
return wm.inner, nil
}
type WorkflowMap map[string]*Workflow

// UnmarshalYAML will unmarshal the WorkflowMap from YAML.
// The unmarshalling will marshall the YAML like a standard Go map
Expand All @@ -49,6 +16,7 @@ func (wm *WorkflowMap) UnmarshalYAML(unmarshal func(interface{}) error) error {
workflow.Name = key
}

wm.inner = m
*wm = m

return nil
}
30 changes: 15 additions & 15 deletions cli/azd/pkg/workflow/config_test.go
Expand Up @@ -18,14 +18,14 @@ var testWorkflow = &Workflow{
}

func Test_WorkflowMap_MarshalYAML(t *testing.T) {
m := NewWorkflowMap()
m.Set("up", testWorkflow)
m.Set("down", testWorkflow)
workflowMap := WorkflowMap{}
workflowMap["up"] = testWorkflow
workflowMap["down"] = testWorkflow

expected, err := yaml.Marshal(m.inner)
expected, err := yaml.Marshal(workflowMap)
require.NoError(t, err)

actual, err := yaml.Marshal(m)
actual, err := yaml.Marshal(workflowMap)
require.NoError(t, err)

require.NoError(t, err)
Expand All @@ -34,7 +34,7 @@ func Test_WorkflowMap_MarshalYAML(t *testing.T) {

func Test_WorkflowMap_UnmarshallYAML(t *testing.T) {
t.Run("array style", func(t *testing.T) {
var wm *WorkflowMap
var wm WorkflowMap
yamlString := heredoc.Doc(`
up:
- azd: package --all
Expand All @@ -45,14 +45,14 @@ func Test_WorkflowMap_UnmarshallYAML(t *testing.T) {
err := yaml.Unmarshal([]byte(yamlString), &wm)
require.NoError(t, err)

upWorkflow, ok := wm.Get("up")
upWorkflow, ok := wm["up"]
require.True(t, ok)

assertWorkflow(t, upWorkflow)
})

t.Run("map style", func(t *testing.T) {
var wm *WorkflowMap
var workflowMap WorkflowMap
yamlString := heredoc.Doc(`
up:
steps:
Expand All @@ -61,17 +61,17 @@ func Test_WorkflowMap_UnmarshallYAML(t *testing.T) {
- azd: deploy --all
`)

err := yaml.Unmarshal([]byte(yamlString), &wm)
err := yaml.Unmarshal([]byte(yamlString), &workflowMap)
require.NoError(t, err)

upWorkflow, ok := wm.Get("up")
upWorkflow, ok := workflowMap["up"]
require.True(t, ok)

assertWorkflow(t, upWorkflow)
})

t.Run("verbose style", func(t *testing.T) {
var wm *WorkflowMap
var workflowMap WorkflowMap
yamlString := heredoc.Doc(`
up:
steps:
Expand All @@ -87,22 +87,22 @@ func Test_WorkflowMap_UnmarshallYAML(t *testing.T) {
- --all
`)

err := yaml.Unmarshal([]byte(yamlString), &wm)
err := yaml.Unmarshal([]byte(yamlString), &workflowMap)
require.NoError(t, err)

upWorkflow, ok := wm.Get("up")
upWorkflow, ok := workflowMap["up"]
require.True(t, ok)

assertWorkflow(t, upWorkflow)
})

t.Run("invalid workflow", func(t *testing.T) {
var wm *WorkflowMap
var workflowMap WorkflowMap
yamlString := heredoc.Doc(`
up: provision && deploy --all && package --all
`)

err := yaml.Unmarshal([]byte(yamlString), &wm)
err := yaml.Unmarshal([]byte(yamlString), &workflowMap)
require.Error(t, err)
})
}
Expand Down

0 comments on commit 788af17

Please sign in to comment.