Skip to content

Commit

Permalink
adding support for resource decorator (#19)
Browse files Browse the repository at this point in the history
* sending the entire resource
  • Loading branch information
sumeet rohatgi authored Oct 25, 2019
1 parent f0dcd44 commit 7ec40a5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
8 changes: 4 additions & 4 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (lib *Lib) createSync(ctxt context.Context, resources []Resource, g *graph)

go func(b Resource, c chan builderOutput) {
defer wg.Done()
c <- execute(ctxt, b, buildCache)
c <- lib.execute(ctxt, b, buildCache)
}(resources[i], output[i])
}

Expand Down Expand Up @@ -266,12 +266,12 @@ func (lib *Lib) createSync(ctxt context.Context, resources []Resource, g *graph)
return status, err
}

func execute(ctxt context.Context, r Resource, cache map[string]Resource) builderOutput {
func (lib *Lib) execute(ctxt context.Context, r Resource, cache map[string]Resource) builderOutput {
for _, dep := range r.ResourceDependencies() {
copyValue(r, dep.ToField, cache[dep.FromResource], dep.FromField)
}

out, err := r.Update(ctxt)
out, err := lib.decorator(r).Update(ctxt)
return builderOutput{out, err}
}

Expand All @@ -291,7 +291,7 @@ func (lib *Lib) deleteSync(ctxt context.Context, resources []Resource, g *graph)
var err error

for _, i := range order {
err = resources[i].Delete(ctxt)
err = lib.decorator(resources[i]).Delete(ctxt)
if err != nil {
err = errorMap{resources[i].ResourceName(): err}
break
Expand Down
11 changes: 9 additions & 2 deletions graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,30 @@ import (
// Opts captures customizable functionality like logging
type Opts struct {
CustomLogger func(args ...interface{})
Decorator func(r Resource) Resource
}

// New creates an instance object
func New(opts *Opts) *Lib {
lib := &Lib{
logger: func(args ...interface{}) {},
logger: func(args ...interface{}) {},
decorator: func(r Resource) Resource { return r },
}
if opts != nil && opts.CustomLogger != nil {
lib.logger = opts.CustomLogger
}

if opts != nil && opts.Decorator != nil {
lib.decorator = opts.Decorator
}

return lib
}

// Lib object is required for using the library
type Lib struct {
logger func(args ...interface{})
logger func(args ...interface{})
decorator func(r Resource) Resource
}

// graph data type
Expand Down

0 comments on commit 7ec40a5

Please sign in to comment.