12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- /*
- * ECAL
- *
- * Copyright 2020 Matthias Ladkau. All rights reserved.
- *
- * This Source Code Form is subject to the terms of the MIT
- * License, If a copy of the MIT License was not distributed with this
- * file, You can obtain one at https://opensource.org/licenses/MIT.
- */
- /*
- Package scope contains the block scope implementation for the event condition language ECAL.
- */
- package scope
- import (
- "fmt"
- "devt.de/krotik/common/stringutil"
- "devt.de/krotik/ecal/parser"
- )
- /*
- Default scope names
- */
- const (
- GlobalScope = "GlobalScope"
- FuncPrefix = "func:"
- )
- /*
- NameFromASTNode returns a scope name from a given ASTNode.
- */
- func NameFromASTNode(node *parser.ASTNode) string {
- return fmt.Sprintf("block: %v (Line:%d Pos:%d)", node.Name, node.Token.Lline, node.Token.Lpos)
- }
- /*
- EvalToString should be used if a value should be converted into a string.
- */
- func EvalToString(v interface{}) string {
- return stringutil.ConvertToString(v)
- }
- /*
- ToObject converts a Scope into an object.
- */
- func ToObject(vs parser.Scope) map[interface{}]interface{} {
- res := make(map[interface{}]interface{})
- for k, v := range vs.(*varsScope).storage {
- res[k] = v
- }
- return res
- }
- /*
- ToScope converts a given object into a Scope.
- */
- func ToScope(name string, o map[interface{}]interface{}) parser.Scope {
- vs := NewScope(name)
- for k, v := range o {
- vs.SetValue(fmt.Sprint(k), v)
- }
- return vs
- }
- /*
- ConvertJSONToECALObject converts a JSON container structure into an object which
- can be used by ECAL.
- */
- func ConvertJSONToECALObject(v interface{}) interface{} {
- res := v
- if mapContainer, ok := v.(map[string]interface{}); ok {
- newRes := make(map[interface{}]interface{})
- for mk, mv := range mapContainer {
- newRes[mk] = ConvertJSONToECALObject(mv)
- }
- res = newRes
- } else if mapList, ok := v.([]interface{}); ok {
- newRes := make([]interface{}, len(mapList))
- for i, lv := range mapList {
- newRes[i] = ConvertJSONToECALObject(lv)
- }
- res = newRes
- }
- return res
- }
|