dump.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. Copyright 2021 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package dump
  14. import (
  15. "github.com/davecgh/go-spew/spew"
  16. )
  17. var prettyPrintConfig = &spew.ConfigState{
  18. Indent: " ",
  19. DisableMethods: true,
  20. DisablePointerAddresses: true,
  21. DisableCapacities: true,
  22. }
  23. // The config MUST NOT be changed because that could change the result of a hash operation
  24. var prettyPrintConfigForHash = &spew.ConfigState{
  25. Indent: " ",
  26. SortKeys: true,
  27. DisableMethods: true,
  28. SpewKeys: true,
  29. DisablePointerAddresses: true,
  30. DisableCapacities: true,
  31. }
  32. // Pretty wrap the spew.Sdump with Indent, and disabled methods like error() and String()
  33. // The output may change over time, so for guaranteed output please take more direct control
  34. func Pretty(a interface{}) string {
  35. return prettyPrintConfig.Sdump(a)
  36. }
  37. // ForHash keeps the original Spew.Sprintf format to ensure the same checksum
  38. func ForHash(a interface{}) string {
  39. return prettyPrintConfigForHash.Sprintf("%#v", a)
  40. }
  41. // OneLine outputs the object in one line
  42. func OneLine(a interface{}) string {
  43. return prettyPrintConfig.Sprintf("%#v", a)
  44. }