yamlgraph: Refactor parsing for dynamic resource registration

Avoid use of the reflect package, and use an extensible list of registred
resource kinds. This also has the benefit of removing the empty VirtRes and
AugeasRes struct types when compiling without libvirt and libaugeas.
This commit is contained in:
Mildred Ki'Lya
2017-03-24 22:38:06 +01:00
parent 64dc47d7e9
commit 525a1e8140
17 changed files with 474 additions and 0 deletions

View File

@@ -38,6 +38,24 @@ import (
"golang.org/x/time/rate"
)
var registeredResources = map[string]func() Res{}
// RegisterResource registers a new resource by providing a constructor
// function that returns a resource object ready to be unmarshalled from YAML.
func RegisterResource(name string, creator func() Res) {
registeredResources[name] = creator
}
// NewEmptyNamedResource returns an empty resource object from a registered
// type, ready to be unmarshalled.
func NewEmptyNamedResource(name string) (Res, error) {
fn, ok := registeredResources[name]
if !ok {
return nil, fmt.Errorf("no resource named %s available", name)
}
return fn(), nil
}
//go:generate stringer -type=ResState -output=resstate_stringer.go
// The ResState type represents the current activity state of each resource.