conversions.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. encoder.Write([]byte(jsonData))
  38. encoder.Close()
  39. return buf.String(), nil
  40. }
  41. // MustBase64 converts the contained object to a Base64 string
  42. // representation of the JSON string representation and panics
  43. // if there is an error
  44. func (m Map) MustBase64() string {
  45. result, err := m.Base64()
  46. if err != nil {
  47. panic(err.Error())
  48. }
  49. return result
  50. }
  51. // SignedBase64 converts the contained object to a Base64 string
  52. // representation of the JSON string representation and signs it
  53. // using the provided key.
  54. func (m Map) SignedBase64(key string) (string, error) {
  55. base64, err := m.Base64()
  56. if err != nil {
  57. return "", err
  58. }
  59. sig := HashWithKey(base64, key)
  60. return base64 + SignatureSeparator + sig, nil
  61. }
  62. // MustSignedBase64 converts the contained object to a Base64 string
  63. // representation of the JSON string representation and signs it
  64. // using the provided key and panics if there is an error
  65. func (m Map) MustSignedBase64(key string) string {
  66. result, err := m.SignedBase64(key)
  67. if err != nil {
  68. panic(err.Error())
  69. }
  70. return result
  71. }
  72. /*
  73. URL Query
  74. ------------------------------------------------
  75. */
  76. // URLValues creates a url.Values object from an Obj. This
  77. // function requires that the wrapped object be a map[string]interface{}
  78. func (m Map) URLValues() url.Values {
  79. vals := make(url.Values)
  80. for k, v := range m {
  81. //TODO: can this be done without sprintf?
  82. vals.Set(k, fmt.Sprintf("%v", v))
  83. }
  84. return vals
  85. }
  86. // URLQuery gets an encoded URL query representing the given
  87. // Obj. This function requires that the wrapped object be a
  88. // map[string]interface{}
  89. func (m Map) URLQuery() (string, error) {
  90. return m.URLValues().Encode(), nil
  91. }