conversions.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package objx
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "net/url"
  9. )
  10. // JSON converts the contained object to a JSON string
  11. // representation
  12. func (m Map) JSON() (string, error) {
  13. result, err := json.Marshal(m)
  14. if err != nil {
  15. err = errors.New("objx: JSON encode failed with: " + err.Error())
  16. }
  17. return string(result), err
  18. }
  19. // MustJSON converts the contained object to a JSON string
  20. // representation and panics if there is an error
  21. func (m Map) MustJSON() string {
  22. result, err := m.JSON()
  23. if err != nil {
  24. panic(err.Error())
  25. }
  26. return result
  27. }
  28. // Base64 converts the contained object to a Base64 string
  29. // representation of the JSON string representation
  30. func (m Map) Base64() (string, error) {
  31. var buf bytes.Buffer
  32. jsonData, err := m.JSON()
  33. if err != nil {
  34. return "", err
  35. }
  36. encoder := base64.NewEncoder(base64.StdEncoding, &buf)
  37. _, err = encoder.Write([]byte(jsonData))
  38. if err != nil {
  39. return "", err
  40. }
  41. _ = encoder.Close()
  42. return buf.String(), nil
  43. }
  44. // MustBase64 converts the contained object to a Base64 string
  45. // representation of the JSON string representation and panics
  46. // if there is an error
  47. func (m Map) MustBase64() string {
  48. result, err := m.Base64()
  49. if err != nil {
  50. panic(err.Error())
  51. }
  52. return result
  53. }
  54. // SignedBase64 converts the contained object to a Base64 string
  55. // representation of the JSON string representation and signs it
  56. // using the provided key.
  57. func (m Map) SignedBase64(key string) (string, error) {
  58. base64, err := m.Base64()
  59. if err != nil {
  60. return "", err
  61. }
  62. sig := HashWithKey(base64, key)
  63. return base64 + SignatureSeparator + sig, nil
  64. }
  65. // MustSignedBase64 converts the contained object to a Base64 string
  66. // representation of the JSON string representation and signs it
  67. // using the provided key and panics if there is an error
  68. func (m Map) MustSignedBase64(key string) string {
  69. result, err := m.SignedBase64(key)
  70. if err != nil {
  71. panic(err.Error())
  72. }
  73. return result
  74. }
  75. /*
  76. URL Query
  77. ------------------------------------------------
  78. */
  79. // URLValues creates a url.Values object from an Obj. This
  80. // function requires that the wrapped object be a map[string]interface{}
  81. func (m Map) URLValues() url.Values {
  82. vals := make(url.Values)
  83. for k, v := range m {
  84. //TODO: can this be done without sprintf?
  85. vals.Set(k, fmt.Sprintf("%v", v))
  86. }
  87. return vals
  88. }
  89. // URLQuery gets an encoded URL query representing the given
  90. // Obj. This function requires that the wrapped object be a
  91. // map[string]interface{}
  92. func (m Map) URLQuery() (string, error) {
  93. return m.URLValues().Encode(), nil
  94. }