decode.go 532 B

12345678910111213141516171819202122232425262728
  1. package message
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. )
  7. const (
  8. FormatBinary = "binary"
  9. FormatJson = "json"
  10. DefaultField = "self"
  11. MetaKey = "__meta"
  12. )
  13. func Decode(payload []byte, format string) (map[string]interface{}, error) {
  14. result := make(map[string]interface{})
  15. switch strings.ToLower(format) {
  16. case FormatJson:
  17. e := json.Unmarshal(payload, &result)
  18. return result, e
  19. case FormatBinary:
  20. result[DefaultField] = payload
  21. return result, nil
  22. }
  23. return nil, fmt.Errorf("invalid format %s", format)
  24. }