payload.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package fire
  2. import (
  3. "errors"
  4. "git.mokkon.com/sainw/server_helper.git/util"
  5. )
  6. // Payload defines the payload
  7. type Payload struct {
  8. // document data
  9. data map[string]interface{}
  10. }
  11. func NewPayload(data map[string]interface{}) *Payload {
  12. return &Payload{data: data}
  13. }
  14. func (p Payload) getID() (string, error) {
  15. id := p.String("id")
  16. if id == "" {
  17. return "", errors.New("ID not found")
  18. }
  19. return id, nil
  20. }
  21. // Add adds data to existing data
  22. func (p *Payload) Add(data map[string]interface{}) {
  23. for k, v := range data {
  24. p.data[k] = v
  25. }
  26. }
  27. // String returns string value of the field
  28. // if the value type is not string, return blank string
  29. func (pl *Payload) String(field string) string {
  30. value, ok := pl.data[field]
  31. if ok {
  32. str, isString := value.(string)
  33. if isString {
  34. return str
  35. }
  36. }
  37. return ""
  38. }
  39. // String returns string value of the field from payload
  40. func (pl *Payload) Strings(field string) []string {
  41. value, ok := pl.data[field]
  42. if ok {
  43. return util.ToStrings(value)
  44. }
  45. return []string{}
  46. }
  47. // Value returns interface value of the field from payload
  48. func (pl *Payload) Value(field string) interface{} {
  49. value, ok := pl.data[field]
  50. if ok {
  51. return value
  52. }
  53. return nil
  54. }