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