uber-go / dig
1
// Copyright (c) 2019 Uber Technologies, Inc.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a copy
4
// of this software and associated documentation files (the "Software"), to deal
5
// in the Software without restriction, including without limitation the rights
6
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
// copies of the Software, and to permit persons to whom the Software is
8
// furnished to do so, subject to the following conditions:
9
//
10
// The above copyright notice and this permission notice shall be included in
11
// all copies or substantial portions of the Software.
12
//
13
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
// THE SOFTWARE.
20

21
package dig
22

23
import (
24
	"bytes"
25
	"fmt"
26
	"strings"
27
)
28

29
// String representation of the entire Container
30
func (c *Container) String() string {
31 2
	b := &bytes.Buffer{}
32 2
	fmt.Fprintln(b, "nodes: {")
33
	for k, vs := range c.providers {
34
		for _, v := range vs {
35 2
			fmt.Fprintln(b, "\t", k, "->", v)
36
		}
37
	}
38 2
	fmt.Fprintln(b, "}")
39

40 2
	fmt.Fprintln(b, "values: {")
41
	for k, v := range c.values {
42 2
		fmt.Fprintln(b, "\t", k, "=>", v)
43
	}
44
	for k, vs := range c.groups {
45
		for _, v := range vs {
46 2
			fmt.Fprintln(b, "\t", k, "=>", v)
47
		}
48
	}
49 2
	fmt.Fprintln(b, "}")
50

51 2
	return b.String()
52
}
53

54
func (n *node) String() string {
55 2
	return fmt.Sprintf("deps: %v, ctor: %v", n.paramList, n.ctype)
56
}
57

58
func (k key) String() string {
59 2
	if k.name != "" {
60 2
		return fmt.Sprintf("%v[name=%q]", k.t, k.name)
61
	}
62 2
	if k.group != "" {
63 2
		return fmt.Sprintf("%v[group=%q]", k.t, k.group)
64
	}
65 2
	return k.t.String()
66
}
67

68
func (pl paramList) String() string {
69 2
	args := make([]string, len(pl.Params))
70
	for i, p := range pl.Params {
71 2
		args[i] = p.String()
72
	}
73 2
	return fmt.Sprint(args)
74
}
75

76
func (sp paramSingle) String() string {
77
	// tally.Scope[optional] means optional
78
	// tally.Scope[optional, name="foo"] means named optional
79

80 2
	var opts []string
81 2
	if sp.Optional {
82 2
		opts = append(opts, "optional")
83
	}
84 2
	if sp.Name != "" {
85 2
		opts = append(opts, fmt.Sprintf("name=%q", sp.Name))
86
	}
87

88 2
	if len(opts) == 0 {
89 2
		return fmt.Sprint(sp.Type)
90
	}
91

92 2
	return fmt.Sprintf("%v[%v]", sp.Type, strings.Join(opts, ", "))
93
}
94

95
func (op paramObject) String() string {
96 2
	fields := make([]string, len(op.Fields))
97
	for i, f := range op.Fields {
98 2
		fields[i] = f.Param.String()
99
	}
100 2
	return strings.Join(fields, " ")
101
}
102

103
func (pt paramGroupedSlice) String() string {
104
	// io.Reader[group="foo"] refers to a group of io.Readers called 'foo'
105 2
	return fmt.Sprintf("%v[group=%q]", pt.Type.Elem(), pt.Group)
106
}

Read our documentation on viewing source code .

Loading