stringify.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright (c) 2015 Arista Networks, Inc.
  2. // Use of this source code is governed by the Apache License 2.0
  3. // that can be found in the COPYING file.
  4. package key
  5. import (
  6. "encoding/base64"
  7. "errors"
  8. "fmt"
  9. "math"
  10. "strconv"
  11. "strings"
  12. "unicode/utf8"
  13. "notabug.org/themusicgod1/goarista/value"
  14. )
  15. // StringifyInterface transforms an arbitrary interface into its string
  16. // representation. We need to do this because some entities use the string
  17. // representation of their keys as their names.
  18. // Note: this API is deprecated and will be removed.
  19. func StringifyInterface(key interface{}) (string, error) {
  20. if key == nil {
  21. return "", errors.New("Unable to stringify nil")
  22. }
  23. var str string
  24. switch key := key.(type) {
  25. case bool:
  26. str = strconv.FormatBool(key)
  27. case uint8:
  28. str = strconv.FormatUint(uint64(key), 10)
  29. case uint16:
  30. str = strconv.FormatUint(uint64(key), 10)
  31. case uint32:
  32. str = strconv.FormatUint(uint64(key), 10)
  33. case uint64:
  34. str = strconv.FormatUint(key, 10)
  35. case int8:
  36. str = strconv.FormatInt(int64(key), 10)
  37. case int16:
  38. str = strconv.FormatInt(int64(key), 10)
  39. case int32:
  40. str = strconv.FormatInt(int64(key), 10)
  41. case int64:
  42. str = strconv.FormatInt(key, 10)
  43. case float32:
  44. str = "f" + strconv.FormatInt(int64(math.Float32bits(key)), 10)
  45. case float64:
  46. str = "f" + strconv.FormatInt(int64(math.Float64bits(key)), 10)
  47. case string:
  48. str = escape(key)
  49. case map[string]interface{}:
  50. keys := SortedKeys(key)
  51. for i, k := range keys {
  52. v := key[k]
  53. keys[i] = stringify(v)
  54. }
  55. str = strings.Join(keys, "_")
  56. case *map[string]interface{}:
  57. return StringifyInterface(*key)
  58. case map[Key]interface{}:
  59. m := make(map[string]interface{}, len(key))
  60. for k, v := range key {
  61. m[k.String()] = v
  62. }
  63. keys := SortedKeys(m)
  64. for i, k := range keys {
  65. keys[i] = stringify(k) + "=" + stringify(m[k])
  66. }
  67. str = strings.Join(keys, "_")
  68. case []interface{}:
  69. elements := make([]string, len(key))
  70. for i, element := range key {
  71. elements[i] = stringify(element)
  72. }
  73. str = strings.Join(elements, ",")
  74. case value.Value:
  75. return key.String(), nil
  76. default:
  77. panic(fmt.Errorf("Unable to stringify type %T: %#v", key, key))
  78. }
  79. return str, nil
  80. }
  81. // escape checks if the string is a valid utf-8 string.
  82. // If it is, it will return the string as is.
  83. // If it is not, it will return the base64 representation of the byte array string
  84. func escape(str string) string {
  85. if utf8.ValidString(str) {
  86. return str
  87. }
  88. return base64.StdEncoding.EncodeToString([]byte(str))
  89. }
  90. func stringify(key interface{}) string {
  91. s, err := StringifyInterface(key)
  92. if err != nil {
  93. panic(err)
  94. }
  95. return s
  96. }