Skip to content
This repository has been archived by the owner on Sep 29, 2021. It is now read-only.

Commit

Permalink
refactor: remove Task.CompiledItems
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Oct 17, 2020
1 parent 6424ec4 commit 48fd22b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 47 deletions.
31 changes: 30 additions & 1 deletion pkg/config/item.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package config

import "github.com/suzuki-shunsuke/buildflow/pkg/expr"
import (
"github.com/suzuki-shunsuke/buildflow/pkg/expr"
"github.com/suzuki-shunsuke/go-convmap/convmap"
)

type Items struct {
Items interface{}
Expand All @@ -19,3 +22,29 @@ func (items Items) Run(params map[string]interface{}) (interface{}, error) {
}
return a, nil
}

func (items *Items) UnmarshalYAML(unmarshal func(interface{}) error) error {
var src interface{}
if err := unmarshal(&src); err != nil {
return err
}
switch t := src.(type) {
case string:
prog, err := expr.New(t)
if err != nil {
return err
}
items.Program = prog
return nil
default:
if t == nil {
return nil
}
a, err := convmap.Convert(t)
if err != nil {
return err
}
items.Items = a
return nil
}
}
55 changes: 18 additions & 37 deletions pkg/config/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,27 @@ import (

"github.com/suzuki-shunsuke/buildflow/pkg/constant"
"github.com/suzuki-shunsuke/buildflow/pkg/execute"
"github.com/suzuki-shunsuke/buildflow/pkg/expr"
"github.com/suzuki-shunsuke/go-convmap/convmap"
)

type Task struct {
Name Template
Type string `yaml:"-"`
When Bool
WhenFile string `yaml:"when_file"`
Dependency Dependency
Command Command
ReadFile ReadFile `yaml:"read_file"`
WriteFile WriteFile `yaml:"write_file"`
HTTP HTTP
Timeout execute.Timeout
Items interface{}
Item Item `yaml:"-"`
CompiledItems Items
Meta map[string]interface{}
Output Script
Input Script
InputFile string `yaml:"input_file"`
OutputFile string `yaml:"output_file"`
Import string
Name Template
Type string `yaml:"-"`
When Bool
WhenFile string `yaml:"when_file"`
Dependency Dependency
Command Command
ReadFile ReadFile `yaml:"read_file"`
WriteFile WriteFile `yaml:"write_file"`
HTTP HTTP
Timeout execute.Timeout
Items Items
Item Item `yaml:"-"`
Meta map[string]interface{}
Output Script
Input Script
InputFile string `yaml:"input_file"`
OutputFile string `yaml:"output_file"`
Import string
}

type WriteFile struct {
Expand All @@ -42,22 +39,6 @@ func (task *Task) Set() error {
return err
}

if s, ok := task.Items.(string); ok {
prog, err := expr.New(s)
if err != nil {
return err
}
task.CompiledItems = Items{
Program: prog,
}
} else if task.Items != nil {
a, err := convmap.Convert(task.Items)
if err != nil {
return err
}
task.Items = a
}

if err := convertMeta(task.Meta); err != nil {
return err
}
Expand Down
18 changes: 9 additions & 9 deletions pkg/controller/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,14 @@ func expandItems(task config.Task, items interface{}, templateParams Params) ([]
}

func Expand(task config.Task, params Params) ([]config.Task, error) {
if task.Items == nil {
if task.Items.Items != nil {
return expandItems(task, task.Items.Items, params)
}
items, err := task.Items.Run(params.ToExpr())
if err != nil {
return nil, err
}
if items == nil {
name, err := task.Name.New(params.ToTemplate())
if err != nil {
return nil, err
Expand All @@ -82,12 +89,5 @@ func Expand(task config.Task, params Params) ([]config.Task, error) {
t.Name = name
return []config.Task{t}, nil
}
if _, ok := task.Items.(string); ok {
items, err := task.CompiledItems.Run(params.ToExpr())
if err != nil {
return nil, err
}
return expandItems(task, items, params)
}
return expandItems(task, task.Items, params)
return expandItems(task, items, params)
}

0 comments on commit 48fd22b

Please sign in to comment.