utils.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package utils
  13. import (
  14. // nolint:gosec
  15. "crypto/md5"
  16. "fmt"
  17. "reflect"
  18. "runtime"
  19. "strings"
  20. )
  21. const (
  22. notImplemented = "not implemented: %s"
  23. )
  24. // MergeByteMap merges map of byte slices.
  25. func MergeByteMap(dst, src map[string][]byte) map[string][]byte {
  26. for k, v := range src {
  27. dst[k] = v
  28. }
  29. return dst
  30. }
  31. // MergeStringMap performs a deep clone from src to dest.
  32. func MergeStringMap(dest, src map[string]string) {
  33. for k, v := range src {
  34. dest[k] = v
  35. }
  36. }
  37. // IsNil checks if an Interface is nil.
  38. func IsNil(i interface{}) bool {
  39. if i == nil {
  40. return true
  41. }
  42. value := reflect.ValueOf(i)
  43. if value.Type().Kind() == reflect.Ptr {
  44. return value.IsNil()
  45. }
  46. return false
  47. }
  48. // ObjectHash calculates md5 sum of the data contained in the secret.
  49. // nolint:gosec
  50. func ObjectHash(object interface{}) string {
  51. textualVersion := fmt.Sprintf("%+v", object)
  52. return fmt.Sprintf("%x", md5.Sum([]byte(textualVersion)))
  53. }
  54. func ErrorContains(out error, want string) bool {
  55. if out == nil {
  56. return want == ""
  57. }
  58. if want == "" {
  59. return false
  60. }
  61. return strings.Contains(out.Error(), want)
  62. }
  63. func ThrowNotImplemented() error {
  64. pc := make([]uintptr, 10) // at least 1 entry needed
  65. runtime.Callers(2, pc)
  66. f := runtime.FuncForPC(pc[0])
  67. return fmt.Errorf(notImplemented, f.Name())
  68. }