message_util.go 519 B

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