func listToMap(list []yaml.Node) map[string]yaml.Node { toMap := make(map[string]yaml.Node) for _, val := range list { name, ok := yaml.FindString(val, "name") if !ok { return nil } asMap, ok := val.(map[string]yaml.Node) if !ok { return nil } newMap := make(map[string]yaml.Node) for key, val := range asMap { if key != "name" { newMap[key] = val } } toMap[name] = newMap } return toMap }
func stepName(index int, value yaml.Node) string { name, ok := yaml.FindString(value, "name") if ok { return name } return fmt.Sprintf("[%d]", index) }
func findByNameOrIndex(node yaml.Node, others []yaml.Node, index int) (string, yaml.Node, bool) { name, ok := yaml.FindString(node, "name") if !ok { return findByIndex(others, index) } key, node, found := findByName(name, others) if !found { return findByIndex(others, index) } return key, node, true }
func findByName(name string, nodes []yaml.Node) (string, yaml.Node, bool) { for _, node := range nodes { otherName, ok := yaml.FindString(node, "name") if !ok { continue } if otherName == name { return name, node, true } } return "", nil, false }
func jobMap(jobs []yaml.Node) map[string]yaml.Node { byName := make(map[string]yaml.Node) for index, job := range jobs { attrs, ok := job.(map[string]yaml.Node) attrs["index"] = index name, ok := yaml.FindString(job, "name") if !ok { panic("job without string name") } byName[name] = attrs } return byName }
func (e AutoExpr) Evaluate(binding Binding) (yaml.Node, bool) { if len(e.Path) == 3 && e.Path[0] == "resource_pools" && e.Path[2] == "size" { jobs, found := binding.FindFromRoot([]string{"jobs"}) if !found { return nil, false } jobsList, ok := jobs.([]yaml.Node) if !ok { return nil, false } size := 0 for _, job := range jobsList { poolName, ok := yaml.FindString(job, "resource_pool") if !ok { continue } if poolName != e.Path[1] { continue } instances, ok := yaml.FindInt(job, "instances") if !ok { return nil, false } size += instances } return size, true } return nil, false }