switch around the WithCheck function
It should be better with an interface. That way it can be written as ensure.WithCheck(resource)
This commit is contained in:
parent
5e49842e6f
commit
ef72359b2d
15
main.go
15
main.go
|
@ -17,22 +17,27 @@ type (
|
||||||
Ensure() error
|
Ensure() error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CheckEnsurer is an etension of ensurer, which also provides an Is function
|
||||||
|
// to check the state of the resource.
|
||||||
|
CheckEnsurer interface {
|
||||||
|
Is() bool
|
||||||
|
Ensure() error
|
||||||
|
}
|
||||||
|
|
||||||
checkEnsurer struct {
|
checkEnsurer struct {
|
||||||
Is Enforced
|
State CheckEnsurer
|
||||||
State Ensurer
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// WithCheck creates an Ensurer instance that runs is before Ensure.
|
// WithCheck creates an Ensurer instance that runs is before Ensure.
|
||||||
func WithCheck(is Enforced, en Ensurer) Ensurer {
|
func WithCheck(en CheckEnsurer) Ensurer {
|
||||||
return &checkEnsurer{
|
return &checkEnsurer{
|
||||||
Is: is,
|
|
||||||
State: en,
|
State: en,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *checkEnsurer) Ensure() error {
|
func (c *checkEnsurer) Ensure() error {
|
||||||
if !c.Is() {
|
if !c.State.Is() {
|
||||||
return c.State.Ensure()
|
return c.State.Ensure()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in New Issue