array_go118.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright (c) 2022 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. //go:build go1.18
  21. // +build go1.18
  22. package zap
  23. import (
  24. "fmt"
  25. "go.uber.org/zap/zapcore"
  26. )
  27. // Objects constructs a field with the given key, holding a list of the
  28. // provided objects that can be marshaled by Zap.
  29. //
  30. // Note that these objects must implement zapcore.ObjectMarshaler directly.
  31. // That is, if you're trying to marshal a []Request, the MarshalLogObject
  32. // method must be declared on the Request type, not its pointer (*Request).
  33. // If it's on the pointer, use ObjectValues.
  34. //
  35. // Given an object that implements MarshalLogObject on the value receiver, you
  36. // can log a slice of those objects with Objects like so:
  37. //
  38. // type Author struct{ ... }
  39. // func (a Author) MarshalLogObject(enc zapcore.ObjectEncoder) error
  40. //
  41. // var authors []Author = ...
  42. // logger.Info("loading article", zap.Objects("authors", authors))
  43. //
  44. // Similarly, given a type that implements MarshalLogObject on its pointer
  45. // receiver, you can log a slice of pointers to that object with Objects like
  46. // so:
  47. //
  48. // type Request struct{ ... }
  49. // func (r *Request) MarshalLogObject(enc zapcore.ObjectEncoder) error
  50. //
  51. // var requests []*Request = ...
  52. // logger.Info("sending requests", zap.Objects("requests", requests))
  53. //
  54. // If instead, you have a slice of values of such an object, use the
  55. // ObjectValues constructor.
  56. //
  57. // var requests []Request = ...
  58. // logger.Info("sending requests", zap.ObjectValues("requests", requests))
  59. func Objects[T zapcore.ObjectMarshaler](key string, values []T) Field {
  60. return Array(key, objects[T](values))
  61. }
  62. type objects[T zapcore.ObjectMarshaler] []T
  63. func (os objects[T]) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  64. for _, o := range os {
  65. if err := arr.AppendObject(o); err != nil {
  66. return err
  67. }
  68. }
  69. return nil
  70. }
  71. // ObjectMarshalerPtr is a constraint that specifies that the given type
  72. // implements zapcore.ObjectMarshaler on a pointer receiver.
  73. type ObjectMarshalerPtr[T any] interface {
  74. *T
  75. zapcore.ObjectMarshaler
  76. }
  77. // ObjectValues constructs a field with the given key, holding a list of the
  78. // provided objects, where pointers to these objects can be marshaled by Zap.
  79. //
  80. // Note that pointers to these objects must implement zapcore.ObjectMarshaler.
  81. // That is, if you're trying to marshal a []Request, the MarshalLogObject
  82. // method must be declared on the *Request type, not the value (Request).
  83. // If it's on the value, use Objects.
  84. //
  85. // Given an object that implements MarshalLogObject on the pointer receiver,
  86. // you can log a slice of those objects with ObjectValues like so:
  87. //
  88. // type Request struct{ ... }
  89. // func (r *Request) MarshalLogObject(enc zapcore.ObjectEncoder) error
  90. //
  91. // var requests []Request = ...
  92. // logger.Info("sending requests", zap.ObjectValues("requests", requests))
  93. //
  94. // If instead, you have a slice of pointers of such an object, use the Objects
  95. // field constructor.
  96. //
  97. // var requests []*Request = ...
  98. // logger.Info("sending requests", zap.Objects("requests", requests))
  99. func ObjectValues[T any, P ObjectMarshalerPtr[T]](key string, values []T) Field {
  100. return Array(key, objectValues[T, P](values))
  101. }
  102. type objectValues[T any, P ObjectMarshalerPtr[T]] []T
  103. func (os objectValues[T, P]) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  104. for i := range os {
  105. // It is necessary for us to explicitly reference the "P" type.
  106. // We cannot simply pass "&os[i]" to AppendObject because its type
  107. // is "*T", which the type system does not consider as
  108. // implementing ObjectMarshaler.
  109. // Only the type "P" satisfies ObjectMarshaler, which we have
  110. // to convert "*T" to explicitly.
  111. var p P = &os[i]
  112. if err := arr.AppendObject(p); err != nil {
  113. return err
  114. }
  115. }
  116. return nil
  117. }
  118. // Stringers constructs a field with the given key, holding a list of the
  119. // output provided by the value's String method
  120. //
  121. // Given an object that implements String on the value receiver, you
  122. // can log a slice of those objects with Objects like so:
  123. //
  124. // type Request struct{ ... }
  125. // func (a Request) String() string
  126. //
  127. // var requests []Request = ...
  128. // logger.Info("sending requests", zap.Stringers("requests", requests))
  129. //
  130. // Note that these objects must implement fmt.Stringer directly.
  131. // That is, if you're trying to marshal a []Request, the String method
  132. // must be declared on the Request type, not its pointer (*Request).
  133. func Stringers[T fmt.Stringer](key string, values []T) Field {
  134. return Array(key, stringers[T](values))
  135. }
  136. type stringers[T fmt.Stringer] []T
  137. func (os stringers[T]) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  138. for _, o := range os {
  139. arr.AppendString(o.String())
  140. }
  141. return nil
  142. }