From e06c4a873d01bb53feca94e27e54e474377d9d32 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Sun, 23 Oct 2016 01:14:02 -0400 Subject: [PATCH] resources: Set the defaults for metaparameters This now lets us have defaults for metaparameters that aren't the zero value for that type. --- resources/resources.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/resources/resources.go b/resources/resources.go index dc5a73dc..847726ef 100644 --- a/resources/resources.go +++ b/resources/resources.go @@ -70,8 +70,8 @@ type AutoEdge interface { // MetaParams is a struct will all params that apply to every resource. type MetaParams struct { - AutoEdge bool `yaml:"autoedge"` // metaparam, should we generate auto edges? // XXX: should default to true - AutoGroup bool `yaml:"autogroup"` // metaparam, should we auto group? // XXX: should default to true + AutoEdge bool `yaml:"autoedge"` // metaparam, should we generate auto edges? + AutoGroup bool `yaml:"autogroup"` // metaparam, should we auto group? Noop bool `yaml:"noop"` // NOTE: there are separate Watch and CheckApply retry and delay values, // but I've decided to use the same ones for both until there's a proper @@ -80,6 +80,29 @@ type MetaParams struct { Delay uint64 `yaml:"delay"` // metaparam, number of milliseconds to wait between retries } +// UnmarshalYAML is the custom unmarshal handler for the MetaParams struct. It +// is primarily useful for setting the defaults. +func (obj *MetaParams) UnmarshalYAML(unmarshal func(interface{}) error) error { + type rawMetaParams MetaParams // indirection to avoid infinite recursion + raw := rawMetaParams(DefaultMetaParams) // convert; the defaults go here + + if err := unmarshal(&raw); err != nil { + return err + } + + *obj = MetaParams(raw) // restore from indirection with type conversion! + return nil +} + +// DefaultMetaParams are the defaults to be used for undefined metaparams. +var DefaultMetaParams = MetaParams{ + AutoEdge: true, + AutoGroup: true, + Noop: false, + Retry: 0, // TODO: is this a good default? + Delay: 0, // TODO: is this a good default? +} + // The Base interface is everything that is common to all resources. // Everything here only needs to be implemented once, in the BaseRes. type Base interface {