util.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. var ErrWebEventHandled = fmt.Errorf("Web event handled")
  38. /*
  39. RaiseWebEventHandledFunc returns a special error which a sink can return to notify
  40. the web API that a web request was handled.
  41. */
  42. type RaiseWebEventHandledFunc struct {
  43. }
  44. /*
  45. Run executes the ECAL function.
  46. */
  47. func (f *RaiseWebEventHandledFunc) Run(instanceID string, vs parser.Scope, is map[string]interface{}, tid uint64, args []interface{}) (interface{}, error) {
  48. if arglen := len(args); arglen != 1 {
  49. return nil, fmt.Errorf("Function requires 1 parameter: request response object")
  50. }
  51. res := args[0]
  52. if resMap, ok := res.(map[interface{}]interface{}); !ok {
  53. return nil, fmt.Errorf("Request response object should be a map")
  54. } else {
  55. if _, ok := resMap["status"]; !ok {
  56. resMap["status"] = 200
  57. }
  58. if _, ok := resMap["headers"]; !ok {
  59. resMap["header"] = map[interface{}]interface{}{
  60. "Content-Type": "application/json; charset=utf-8",
  61. "X-Content-Type-Options": "nosniff",
  62. }
  63. }
  64. if _, ok := resMap["body"]; !ok {
  65. resMap["body"] = map[interface{}]interface{}{}
  66. }
  67. }
  68. erp := is["erp"].(*interpreter.ECALRuntimeProvider)
  69. node := is["astnode"].(*parser.ASTNode)
  70. return nil, &util.RuntimeErrorWithDetail{
  71. RuntimeError: erp.NewRuntimeError(ErrWebEventHandled, "", node).(*util.RuntimeError),
  72. Environment: vs,
  73. Data: res,
  74. }
  75. }
  76. /*
  77. DocString returns a descriptive string.
  78. */
  79. func (f *RaiseWebEventHandledFunc) DocString() (string, error) {
  80. return "When handling a web event, notify the web API of EliasDB that the web request was handled.", nil
  81. }