resources: Add NewNamedResource helper

This makes the common pattern of NewResource, SetName, easier. It also
makes it less likely for you to forget to use SetName.
This commit is contained in:
James Shubin
2017-06-17 17:32:52 -04:00
parent e341256627
commit 0dadf3d78a
8 changed files with 41 additions and 36 deletions

View File

@@ -57,15 +57,13 @@ func (obj *MyGAPI) Graph() (*pgraph.Graph, error) {
return nil, fmt.Errorf("libmgmt: MyGAPI is not initialized")
}
// TODO: this method of instantiation is deprecated, use: NewResource
n1 := &resources.NoopRes{
BaseRes: resources.BaseRes{
Name: "noop1",
Kind: "noop",
MetaParams: resources.DefaultMetaParams,
},
n1, err := resources.NewNamedResource("noop", "noop1")
if err != nil {
return nil, err
}
// NOTE: This is considered the legacy method to build graphs. Avoid
// importing the legacy `yamlgraph` lib if possible for custom graphs.
// we can still build a graph via the yaml method
gc := &yamlgraph.GraphConfig{
Graph: obj.Name,
@@ -74,7 +72,7 @@ func (obj *MyGAPI) Graph() (*pgraph.Graph, error) {
Exec: []*resources.ExecRes{},
File: []*resources.FileRes{},
Msg: []*resources.MsgRes{},
Noop: []*resources.NoopRes{n1},
Noop: []*resources.NoopRes{n1.(*resources.NoopRes)},
Pkg: []*resources.PkgRes{},
Svc: []*resources.SvcRes{},
Timer: []*resources.TimerRes{},

View File

@@ -65,11 +65,10 @@ func (obj *MyGAPI) Graph() (*pgraph.Graph, error) {
}
var vertex pgraph.Vertex
for i := uint(0); i < obj.Count; i++ {
n, err := resources.NewResource("noop")
n, err := resources.NewNamedResource("noop", fmt.Sprintf("noop%d", i))
if err != nil {
return nil, err
}
n.SetName(fmt.Sprintf("noop%d", i))
g.AddVertex(n)
if i > 0 {
g.AddEdge(vertex, n, &resources.Edge{Name: fmt.Sprintf("e%d", i)})