helper.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. /*
  11. Package scope contains the block scope implementation for the event condition language ECAL.
  12. */
  13. package scope
  14. import (
  15. "fmt"
  16. "devt.de/krotik/common/stringutil"
  17. "devt.de/krotik/ecal/parser"
  18. )
  19. /*
  20. Default scope names
  21. */
  22. const (
  23. GlobalScope = "GlobalScope"
  24. )
  25. /*
  26. NameFromASTNode returns a scope name from a given ASTNode.
  27. */
  28. func NameFromASTNode(node *parser.ASTNode) string {
  29. return fmt.Sprintf("block: %v (Line:%d Pos:%d)", node.Name, node.Token.Lline, node.Token.Lpos)
  30. }
  31. /*
  32. EvalToString should be used if a value should be converted into a string.
  33. */
  34. func EvalToString(v interface{}) string {
  35. return stringutil.ConvertToString(v)
  36. }
  37. /*
  38. ToObject converts a Scope into an object.
  39. */
  40. func ToObject(vs parser.Scope) map[interface{}]interface{} {
  41. res := make(map[interface{}]interface{})
  42. for k, v := range vs.(*varsScope).storage {
  43. res[k] = v
  44. }
  45. return res
  46. }
  47. /*
  48. ToScope converts a given object into a Scope.
  49. */
  50. func ToScope(name string, o map[interface{}]interface{}) parser.Scope {
  51. vs := NewScope(name)
  52. for k, v := range o {
  53. vs.SetValue(fmt.Sprint(k), v)
  54. }
  55. return vs
  56. }