util.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package dbfunc
  11. import (
  12. "fmt"
  13. "devt.de/krotik/ecal/interpreter"
  14. "devt.de/krotik/ecal/parser"
  15. "devt.de/krotik/ecal/util"
  16. "devt.de/krotik/eliasdb/graph"
  17. )
  18. /*
  19. RaiseGraphEventHandledFunc returns the special graph.ErrEventHandled error which a sink,
  20. handling graph events, can return to notify the GraphManager that no further
  21. action is necessary.
  22. */
  23. type RaiseGraphEventHandledFunc struct {
  24. }
  25. /*
  26. Run executes the ECAL function.
  27. */
  28. func (f *RaiseGraphEventHandledFunc) Run(instanceID string, vs parser.Scope, is map[string]interface{}, tid uint64, args []interface{}) (interface{}, error) {
  29. return nil, graph.ErrEventHandled
  30. }
  31. /*
  32. DocString returns a descriptive string.
  33. */
  34. func (f *RaiseGraphEventHandledFunc) DocString() (string, error) {
  35. return "When handling a graph event, notify the GraphManager of EliasDB that no further action is necessary.", nil
  36. }
  37. /*
  38. ErrWebEventHandled is a special error to signal that a web request was handled.
  39. */
  40. var ErrWebEventHandled = fmt.Errorf("Web event handled")
  41. /*
  42. RaiseWebEventHandledFunc returns a special error which a sink can return to notify
  43. the web API that a web request was handled.
  44. */
  45. type RaiseWebEventHandledFunc struct {
  46. }
  47. /*
  48. Run executes the ECAL function.
  49. */
  50. func (f *RaiseWebEventHandledFunc) Run(instanceID string, vs parser.Scope, is map[string]interface{}, tid uint64, args []interface{}) (interface{}, error) {
  51. if arglen := len(args); arglen != 1 {
  52. return nil, fmt.Errorf("Function requires 1 parameter: request response object")
  53. }
  54. res := args[0]
  55. resMap, ok := res.(map[interface{}]interface{})
  56. if !ok {
  57. return nil, fmt.Errorf("Request response object should be a map")
  58. }
  59. if _, ok := resMap["status"]; !ok {
  60. resMap["status"] = 200
  61. }
  62. if _, ok := resMap["headers"]; !ok {
  63. resMap["header"] = map[interface{}]interface{}{
  64. "Content-Type": "application/json; charset=utf-8",
  65. "X-Content-Type-Options": "nosniff",
  66. }
  67. }
  68. if _, ok := resMap["body"]; !ok {
  69. resMap["body"] = map[interface{}]interface{}{}
  70. }
  71. erp := is["erp"].(*interpreter.ECALRuntimeProvider)
  72. node := is["astnode"].(*parser.ASTNode)
  73. return nil, &util.RuntimeErrorWithDetail{
  74. RuntimeError: erp.NewRuntimeError(ErrWebEventHandled, "", node).(*util.RuntimeError),
  75. Environment: vs,
  76. Data: res,
  77. }
  78. }
  79. /*
  80. DocString returns a descriptive string.
  81. */
  82. func (f *RaiseWebEventHandledFunc) DocString() (string, error) {
  83. return "When handling a web event, notify the web API of EliasDB that the web request was handled.", nil
  84. }