本文整理汇总了Golang中github.com/sarifsystems/sarif/sarif.Message.DecodePayload方法的典型用法代码示例。如果您正苦于以下问题:Golang Message.DecodePayload方法的具体用法?Golang Message.DecodePayload怎么用?Golang Message.DecodePayload使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/sarifsystems/sarif/sarif.Message
的用法示例。
在下文中一共展示了Message.DecodePayload方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: HandleQuery
func (s *Service) HandleQuery(msg sarif.Message) {
var f Fact
if err := msg.DecodePayload(&f); err != nil {
s.ReplyBadRequest(msg, err)
return
}
f, err := s.InterpretLiterals(f)
if err != nil {
s.ReplyInternalError(msg, err)
return
}
var facts []*Fact
if err := s.DB.Where(f).Limit(100).Find(&facts).Error; err != nil {
s.ReplyInternalError(msg, err)
return
}
if facts, err = s.AddLabelFacts(facts); err != nil {
s.ReplyInternalError(msg, err)
return
}
s.Reply(msg, sarif.CreateMessage("concepts/result", &resultPayload{
ToJsonLd(facts),
facts,
}))
}
示例2: HandleQueryExternal
func (s *Service) HandleQueryExternal(msg sarif.Message) {
var f Fact
if err := msg.DecodePayload(&f); err != nil {
s.ReplyBadRequest(msg, err)
return
}
facts := []*Fact{&f}
FillVariables(facts)
var r sparql.ResourceResponse
q := sparql.DBPedia.Query()
q = BuildQuery(q, facts)
if err := q.Exec(&r); err != nil {
s.ReplyInternalError(msg, err)
return
}
result := ApplyBindings(facts, r.Results.Bindings, sparql.CommonPrefixes)
s.Reply(msg, sarif.CreateMessage("concepts/result", &resultPayload{
ToJsonLd(result),
result,
}))
for _, f := range result {
if err := s.DB.FirstOrCreate(&f, &f).Error; err != nil {
s.Log("err", "[reasoner] error updating external fact: "+err.Error())
}
}
}
示例3: handleEventLast
func (s *Service) handleEventLast(msg sarif.Message) {
filter := make(map[string]interface{})
if err := msg.DecodePayload(&filter); err != nil {
s.ReplyBadRequest(msg, err)
return
}
s.Log("debug", "get last by filter:", filter)
var events []Event
err := s.Store.Scan("events", store.Scan{
Reverse: true,
Only: "values",
Filter: filter,
Limit: 1,
}, &events)
if err != nil {
s.ReplyInternalError(msg, err)
}
if len(events) == 0 {
s.Reply(msg, MessageEventNotFound)
return
}
s.Log("debug", "last - found", events)
reply := sarif.Message{Action: "event/found"}
if err := reply.EncodePayload(events[0]); err != nil {
s.ReplyInternalError(msg, err)
return
}
reply.Text = events[0].String()
s.Reply(msg, reply)
}
示例4: handleEventRecord
func (s *Service) handleEventRecord(msg sarif.Message) {
var p recordPayload
if err := msg.DecodePayload(&p); err != nil {
s.ReplyBadRequest(msg, err)
return
}
if p.Action == "" {
s.ReplyBadRequest(msg, errors.New("No action specified"))
return
}
var cfg Config
s.cfg.Get(&cfg)
if cfg.RecordedActions == nil {
cfg.RecordedActions = make(map[string]bool)
}
if enabled := cfg.RecordedActions[p.Action]; !enabled {
cfg.RecordedActions[p.Action] = true
s.cfg.Set(cfg)
s.Subscribe(p.Action, "", s.handleEventNew)
}
s.Log("debug", "recording action:", p.Action)
s.Reply(msg, sarif.CreateMessage("event/recording", p))
}
示例5: handleEventList
func (s *Service) handleEventList(msg sarif.Message) {
var filter map[string]interface{}
if err := msg.DecodePayload(&filter); err != nil {
s.ReplyBadRequest(msg, err)
return
}
if filter == nil {
filter = make(map[string]interface{})
}
reverse := false
if len(filter) == 0 {
filter["time >="] = time.Now().Add(-24 * time.Hour)
reverse = true
}
s.Log("debug", "list by filter:", filter)
var events []Event
err := s.Store.Scan("events", store.Scan{
Only: "values",
Filter: filter,
Reverse: reverse,
}, &events)
if err != nil {
s.ReplyInternalError(msg, err)
}
s.Log("debug", "list - found", len(events))
s.Reply(msg, sarif.CreateMessage("events/listed", &aggPayload{
Type: "list",
Filter: filter,
Events: events,
Value: float64(len(events)),
}))
}
示例6: handlePut
func (s *Service) handlePut(msg sarif.Message) {
collection, key := parseAction("store/put/", msg.Action)
if collection == "" {
s.ReplyBadRequest(msg, errors.New("No collection specified."))
return
}
if len(msg.Payload.Raw) == 0 && msg.Text != "" {
v, _ := json.Marshal(msg.Text)
msg.Payload.Raw = json.RawMessage(v)
}
var p interface{}
if err := msg.DecodePayload(&p); err != nil {
s.ReplyBadRequest(msg, err)
return
}
// TODO: maybe a JSON payload consistency check
doc, err := s.Store.Put(&Document{
Collection: collection,
Key: key,
Value: msg.Payload.Raw,
})
if err != nil {
s.ReplyInternalError(msg, err)
return
}
doc.Value = nil
reply := sarif.CreateMessage("store/updated/"+doc.Collection+"/"+doc.Key, doc)
s.Reply(msg, reply)
pub := sarif.CreateMessage("store/updated/"+doc.Collection+"/"+doc.Key, doc)
s.Publish(pub)
}
示例7: handleScan
func (s *Service) handleScan(msg sarif.Message) {
collection, prefix := parseAction("store/scan/", msg.Action)
if collection == "" {
s.ReplyBadRequest(msg, errors.New("No collection specified."))
return
}
var p scanMessage
if err := msg.DecodePayload(&p); err != nil {
s.ReplyBadRequest(msg, err)
return
}
if p.Start == "" && p.End == "" {
if p.Prefix == "" {
p.Prefix = prefix
}
}
got, err := s.doScan(collection, p)
if err != nil {
s.ReplyInternalError(msg, err)
return
}
s.Reply(msg, sarif.CreateMessage("store/scanned/"+collection, got))
}
示例8: handleLuaLoad
func (s *Service) handleLuaLoad(msg sarif.Message) {
gen := false
name := strings.TrimPrefix(strings.TrimPrefix(msg.Action, "lua/load"), "/")
if name == "" {
name, gen = sarif.GenerateId(), true
}
if _, ok := s.Machines[name]; ok {
s.destroyMachine(name)
}
m, err := s.createMachine(name)
if err != nil {
s.ReplyInternalError(msg, err)
return
}
var ctp ContentPayload
if err := msg.DecodePayload(&ctp); err != nil {
s.ReplyBadRequest(msg, err)
return
}
text := msg.Text
if ctp.Content.Url != "" {
ct, err := content.Get(ctp.Content)
if err != nil {
s.ReplyBadRequest(msg, err)
}
text = string(ct.Data)
}
var gp interface{}
msg.DecodePayload(&gp)
out, err, _ := m.Do(text, gp)
if err != nil {
s.ReplyBadRequest(msg, err)
s.destroyMachine(name)
return
}
if !gen {
f, err := os.Create(s.cfg.ScriptDir + "/" + name + ".lua")
if err == nil {
_, err = f.Write([]byte(text))
defer f.Close()
}
if err != nil {
s.ReplyInternalError(msg, err)
s.destroyMachine(name)
return
}
}
s.Reply(msg, sarif.CreateMessage("lua/loaded", &MsgMachineStatus{
name,
"up",
out,
}))
}
示例9: handle
func (s *Scheduler) handle(msg sarif.Message) {
var t ScheduleMessage
if err := msg.DecodePayload(&t); err != nil {
s.ReplyBadRequest(msg, err)
return
}
now := time.Now()
t.Task.Time = now
if t.Time != "" {
t.Task.Time = futureTime(util.ParseTime(t.Time, now))
}
if t.RandomAfter != "" && t.RandomBefore != "" {
after := futureTime(util.ParseTime(t.RandomAfter, t.Task.Time))
before := futureTime(util.ParseTime(t.RandomBefore, t.Task.Time))
if before.Before(after) {
after, before = before, after
}
maxDur := int64(before.Sub(after))
ranDur := time.Duration(rand.Int63n(maxDur))
t.Task.Time = after.Add(ranDur)
}
if t.Duration != "" {
dur, err := util.ParseDuration(t.Duration)
if err != nil {
s.ReplyBadRequest(msg, err)
return
}
t.Task.Time = t.Task.Time.Add(dur)
}
if t.Task.Reply.Action == "" {
text := msg.Text
if text == "" {
text = "Reminder from " + time.Now().Format(time.RFC3339) + " finished."
}
t.Task.Reply = sarif.Message{
Action: "schedule/finished",
Destination: msg.Source,
Text: text,
}
}
if t.Task.Reply.CorrId == "" {
t.Reply.CorrId = msg.Id
}
s.Log("info", "new task:", t)
if _, err := s.Store.Put(t.Task.Key(), &t.Task); err != nil {
s.ReplyInternalError(msg, err)
return
}
go s.recalculateTimer()
s.Reply(msg, sarif.CreateMessage("schedule/created", t.Task))
}
示例10: handleServingRecord
func (s *Service) handleServingRecord(msg sarif.Message) {
var sv Serving
if err := msg.DecodePayload(&sv); err != nil {
s.ReplyBadRequest(msg, err)
return
}
size, name := splitSizeName(msg.Text)
if sv.Size == 0 {
sv.Size = size
}
if sv.Name == "" {
sv.Name = name
}
if sv.Product == nil {
ps, err := s.findProduct(sv.Name)
if err != nil {
s.ReplyBadRequest(msg, err)
return
}
if len(ps) > 1 {
pList := ""
for _, p := range ps {
pList += "\n- " + p.Name
}
s.ReplyBadRequest(msg, fmt.Errorf("%d products named %s found.%s", len(ps), sv.Name, pList))
return
}
if len(ps) == 1 {
sv.Product = &ps[0]
}
}
if sv.AmountWeight <= 0 {
sv.AmountWeight = Weight(sv.Size) * sv.Product.ServingWeight
}
if sv.AmountVolume <= 0 {
sv.AmountVolume = Volume(sv.Size) * sv.Product.ServingVolume
}
if sv.AmountWeight <= 0 && sv.AmountVolume <= 0 {
s.ReplyBadRequest(msg, errors.New("No serving amount specified."))
return
}
if sv.Time.IsZero() {
sv.Time = time.Now()
}
if err := s.DB.Save(&sv).Error; err != nil {
s.ReplyInternalError(msg, err)
return
}
s.Reply(msg, sarif.CreateMessage("meal/serving/recorded", &sv))
}
示例11: AddMessage
func (s *MessageSchemaStore) AddMessage(msg *sarif.Message) {
schema := &MessageSchema{
Action: msg.Action,
Fields: make(map[string]string),
}
p := make(map[string]interface{})
msg.DecodePayload(&p)
for v, _ := range p {
schema.Fields[v] = "text"
}
s.Add(schema)
}
示例12: handleBatch
func (s *Service) handleBatch(msg sarif.Message) {
cmds := make([]BatchCommand, 0)
if err := msg.DecodePayload(&cmds); err != nil {
s.ReplyInternalError(msg, err)
return
}
results := make([]interface{}, len(cmds))
for i, cmd := range cmds {
collection, key := parseAction("", cmd.Key)
switch cmd.Type {
case "put":
doc, err := s.Store.Put(&Document{
Collection: collection,
Key: key,
Value: []byte(cmd.Value),
})
if err != nil {
s.ReplyInternalError(msg, err)
return
}
results[i] = doc
case "get":
doc, err := s.Store.Get(collection, key)
if err != nil {
s.ReplyInternalError(msg, err)
return
}
results[i] = doc
case "del":
if err := s.Store.Del(collection, key); err != nil {
s.ReplyInternalError(msg, err)
return
}
results[i] = true
case "scan":
var p scanMessage
if err := json.Unmarshal(cmd.Value, &p); err != nil {
s.ReplyInternalError(msg, err)
return
}
got, err := s.doScan(collection, p)
if err != nil {
s.ReplyInternalError(msg, err)
return
}
results[i] = got
}
}
s.Reply(msg, sarif.CreateMessage("store/batched/", results))
}
示例13: handleLocationUpdate
func (s *Service) handleLocationUpdate(msg sarif.Message) {
loc := Location{}
if err := msg.DecodePayload(&loc); err != nil {
s.ReplyBadRequest(msg, err)
return
}
if loc.Time.IsZero() {
loc.Time = time.Now()
}
loc.Geohash = EncodeGeohash(loc.Latitude, loc.Longitude, 12)
s.Log("debug", "store update", loc)
var last []Location
err := s.Store.Scan("locations", store.Scan{
Reverse: true,
Only: "values",
Limit: 1,
}, &last)
if err != nil {
s.Log("err/internal", "retrieve last err: "+err.Error())
}
if len(last) > 0 {
loc.Distance = HaversineDistance(last[0], loc)
loc.Speed = loc.Distance / loc.Time.Sub(last[0].Time).Seconds()
}
if _, err := s.Store.Put(loc.Key(), &loc); err != nil {
s.ReplyInternalError(msg, err)
}
if changed := s.Clusters.Advance(loc); changed {
c := s.Clusters.Current()
status := "enter"
if c.Status != ConfirmedCluster {
status = "leave"
c = s.Clusters.LastCompleted()
s.Clusters.ClearCompleted()
}
// TODO: make optional
if place, err := ReverseGeocode(c.Location); err == nil {
c.Address = place.Pretty()
}
s.Publish(sarif.CreateMessage("location/cluster/"+status, c))
}
if len(last) > 0 {
s.checkGeofences(last[0], loc)
}
}
示例14: messageToTable
func messageToTable(L *lua.LState, msg sarif.Message) lua.LValue {
t := L.NewTable()
tableSetString(t, "sarif", msg.Version)
tableSetString(t, "id", msg.Id)
tableSetString(t, "action", msg.Action)
tableSetString(t, "src", msg.Source)
tableSetString(t, "dest", msg.Destination)
tableSetString(t, "corr", msg.CorrId)
tableSetString(t, "text", msg.Text)
p := make(map[string]interface{})
msg.DecodePayload(&p)
t.RawSetH(lua.LString("p"), luareflect.ToLua(L, p))
return t
}
示例15: handleNaturalParse
func (s *Service) handleNaturalParse(msg sarif.Message) {
ctx := &natural.Context{}
msg.DecodePayload(ctx)
if ctx.Text == "" {
ctx.Text = msg.Text
}
res, err := s.parser.Parse(ctx)
if err != nil {
s.ReplyBadRequest(msg, err)
return
}
s.Reply(msg, sarif.CreateMessage("natural/parsed", res))
}