stringer.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. package dig
  21. import (
  22. "bytes"
  23. "fmt"
  24. "strings"
  25. )
  26. // String representation of the entire Container
  27. func (c *Container) String() string {
  28. b := &bytes.Buffer{}
  29. fmt.Fprintln(b, "nodes: {")
  30. for k, vs := range c.providers {
  31. for _, v := range vs {
  32. fmt.Fprintln(b, "\t", k, "->", v)
  33. }
  34. }
  35. fmt.Fprintln(b, "}")
  36. fmt.Fprintln(b, "values: {")
  37. for k, v := range c.values {
  38. fmt.Fprintln(b, "\t", k, "=>", v)
  39. }
  40. for k, vs := range c.groups {
  41. for _, v := range vs {
  42. fmt.Fprintln(b, "\t", k, "=>", v)
  43. }
  44. }
  45. fmt.Fprintln(b, "}")
  46. return b.String()
  47. }
  48. func (n *node) String() string {
  49. return fmt.Sprintf("deps: %v, ctor: %v", n.paramList, n.ctype)
  50. }
  51. func (k key) String() string {
  52. if k.name != "" {
  53. return fmt.Sprintf("%v[name=%q]", k.t, k.name)
  54. }
  55. if k.group != "" {
  56. return fmt.Sprintf("%v[group=%q]", k.t, k.group)
  57. }
  58. return k.t.String()
  59. }
  60. func (pl paramList) String() string {
  61. args := make([]string, len(pl.Params))
  62. for i, p := range pl.Params {
  63. args[i] = p.String()
  64. }
  65. return fmt.Sprint(args)
  66. }
  67. func (sp paramSingle) String() string {
  68. // tally.Scope[optional] means optional
  69. // tally.Scope[optional, name="foo"] means named optional
  70. var opts []string
  71. if sp.Optional {
  72. opts = append(opts, "optional")
  73. }
  74. if sp.Name != "" {
  75. opts = append(opts, fmt.Sprintf("name=%q", sp.Name))
  76. }
  77. if len(opts) == 0 {
  78. return fmt.Sprint(sp.Type)
  79. }
  80. return fmt.Sprintf("%v[%v]", sp.Type, strings.Join(opts, ", "))
  81. }
  82. func (op paramObject) String() string {
  83. fields := make([]string, len(op.Fields))
  84. for i, f := range op.Fields {
  85. fields[i] = f.Param.String()
  86. }
  87. return strings.Join(fields, " ")
  88. }
  89. func (pt paramGroupedSlice) String() string {
  90. // io.Reader[group="foo"] refers to a group of io.Readers called 'foo'
  91. return fmt.Sprintf("%v[group=%q]", pt.Type.Elem(), pt.Group)
  92. }