MCPcopy
hub / github.com/purpleidea/mgmt / stringResFields

Function stringResFields

lang/interpret_test.go:2615–2650  ·  view source on GitHub ↗

stringResFields is a helper function to store a resource graph as a text format for test comparisons. This also adds a line for each vertex as well!

(res engine.Res)

Source from the content-addressed store, hash-verified

2613// stringResFields is a helper function to store a resource graph as a text
2614// format for test comparisons. This also adds a line for each vertex as well!
2615func stringResFields(res engine.Res) (string, error) {
2616 m, err := engineUtil.ResToParamValues(res)
2617 if err != nil {
2618 return "", errwrap.Wrapf(err, "can't read resource %s", res)
2619 }
2620 str := ""
2621 keys := []string{}
2622 for k := range m {
2623 keys = append(keys, k)
2624 }
2625 sort.Strings(keys) // sort for determinism
2626 for _, field := range keys {
2627 v := m[field]
2628 str += fmt.Sprintf("Field: %s[%s].%s = %s\n", res.Kind(), res.Name(), field, v)
2629 }
2630
2631 groupableRes, ok := res.(engine.GroupableRes)
2632 if !ok {
2633 return str, nil
2634 }
2635 for _, x := range groupableRes.GetGroup() { // grouped elements
2636 s, err := stringResFields(x) // recurse
2637 if err != nil {
2638 return "", err
2639 }
2640 s += fmt.Sprintf("Vertex: %s\n", x) // add one for the res itself!
2641
2642 // add a prefix to each line?
2643 s = strings.Trim(s, "\n") // trim trailing newlines
2644 for _, f := range strings.Split(s, "\n") {
2645 str += fmt.Sprintf("Group: %s: ", res) + f + "\n"
2646 }
2647 //str += s
2648 }
2649 return str, nil
2650}

Callers 1

TestAstFunc3Function · 0.85

Calls 4

WrapfFunction · 0.92
KindMethod · 0.65
NameMethod · 0.65
GetGroupMethod · 0.65

Tested by

no test coverage detected