123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- package fire
- import (
- "errors"
- "git.mokkon.com/sainw/server_helper.git/util"
- )
- // Payload defines the payload
- type Payload struct {
- // document data
- data map[string]interface{}
- }
- func NewPayload(data map[string]interface{}) *Payload {
- return &Payload{data: data}
- }
- func (p Payload) getID() (string, error) {
- id := p.String("id")
- if id == "" {
- return "", errors.New("ID not found")
- }
- return id, nil
- }
- // Add adds data to existing data
- func (p *Payload) Add(data map[string]interface{}) {
- for k, v := range data {
- p.data[k] = v
- }
- }
- // String returns string value of the field
- // if the value type is not string, return blank string
- func (pl *Payload) String(field string) string {
- value, ok := pl.data[field]
- if ok {
- str, isString := value.(string)
- if isString {
- return str
- }
- }
- return ""
- }
- // String returns string value of the field from payload
- func (pl *Payload) Strings(field string) []string {
- value, ok := pl.data[field]
- if ok {
- return util.ToStrings(value)
- }
- return []string{}
- }
- // Value returns interface value of the field from payload
- func (pl *Payload) Value(field string) interface{} {
- value, ok := pl.data[field]
- if ok {
- return value
- }
- return nil
- }
|