pretty.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. *
  3. * Copyright 2021 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. // Package pretty defines helper functions to pretty-print structs for logging.
  19. package pretty
  20. import (
  21. "bytes"
  22. "encoding/json"
  23. "fmt"
  24. protov1 "github.com/golang/protobuf/proto"
  25. "google.golang.org/protobuf/encoding/protojson"
  26. protov2 "google.golang.org/protobuf/proto"
  27. )
  28. const jsonIndent = " "
  29. // ToJSON marshals the input into a json string.
  30. //
  31. // If marshal fails, it falls back to fmt.Sprintf("%+v").
  32. func ToJSON(e any) string {
  33. switch ee := e.(type) {
  34. case protov1.Message:
  35. mm := protojson.MarshalOptions{Indent: jsonIndent}
  36. ret, err := mm.Marshal(protov1.MessageV2(ee))
  37. if err != nil {
  38. // This may fail for proto.Anys, e.g. for xDS v2, LDS, the v2
  39. // messages are not imported, and this will fail because the message
  40. // is not found.
  41. return fmt.Sprintf("%+v", ee)
  42. }
  43. return string(ret)
  44. case protov2.Message:
  45. mm := protojson.MarshalOptions{
  46. Multiline: true,
  47. Indent: jsonIndent,
  48. }
  49. ret, err := mm.Marshal(ee)
  50. if err != nil {
  51. // This may fail for proto.Anys, e.g. for xDS v2, LDS, the v2
  52. // messages are not imported, and this will fail because the message
  53. // is not found.
  54. return fmt.Sprintf("%+v", ee)
  55. }
  56. return string(ret)
  57. default:
  58. ret, err := json.MarshalIndent(ee, "", jsonIndent)
  59. if err != nil {
  60. return fmt.Sprintf("%+v", ee)
  61. }
  62. return string(ret)
  63. }
  64. }
  65. // FormatJSON formats the input json bytes with indentation.
  66. //
  67. // If Indent fails, it returns the unchanged input as string.
  68. func FormatJSON(b []byte) string {
  69. var out bytes.Buffer
  70. err := json.Indent(&out, b, "", jsonIndent)
  71. if err != nil {
  72. return string(b)
  73. }
  74. return out.String()
  75. }