/
context.go
149 lines (134 loc) · 3.79 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2019 Alexey Krivonogov. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
package vm
import (
"fmt"
"strings"
// "strings"
"unicode/utf8"
// "github.com/gentee/gentee/core"
)
const (
// CtxChar is a context boundery character
CtxChar = '#'
// CtxLength is the max length of the context key
CtxLength = 64
// CtxDeep is the max deep in replace function
CtxDeep = 16
)
var (
// ErrCtxLength is returned when the key is too long
ErrCtxLength = `key length is longer than %d characters`
// ErrCtxLoop is returned when there is a loop in values of context
ErrCtxLoop = `%s key refers to itself`
// ErrCtxDeep is returned if the maximum depth reached
ErrCtxDeep = `maximum depth reached`
)
// CtxIsºStr returns true if a context key exists
func CtxIsºStr(rt *Runtime, key string) int64 {
rt.Owner.CtxMutex.RLock()
defer rt.Owner.CtxMutex.RUnlock()
if _, ok := rt.Owner.Context[key]; ok {
return 1
}
return 0
}
// CtxSetºStrStr sets a context value
func CtxSetºStrStr(rt *Runtime, key, value string) (string, error) {
if utf8.RuneCountInString(key) > CtxLength {
return ``, fmt.Errorf(ErrCtxLength, CtxLength)
}
rt.Owner.CtxMutex.Lock()
rt.Owner.Context[key] = value
rt.Owner.CtxMutex.Unlock()
return value, nil
}
// CtxSetºStrFloat assign a float to a context key
func CtxSetºStrFloat(rt *Runtime, key string, value float64) (string, error) {
return CtxSetºStrStr(rt, key, strºFloat(value))
}
// CtxSetºStrBool assign a bool to a context key
func CtxSetºStrBool(rt *Runtime, key string, value int64) (string, error) {
return CtxSetºStrStr(rt, key, strºBool(value))
}
// CtxSetºStrInt assign an integer to a context key
func CtxSetºStrInt(rt *Runtime, key string, value int64) (string, error) {
return CtxSetºStrStr(rt, key, strºInt(value))
}
// CtxValueºStr returns a context value
func CtxValueºStr(rt *Runtime, key string) string {
rt.Owner.CtxMutex.RLock()
defer rt.Owner.CtxMutex.RUnlock()
return rt.Owner.Context[key]
}
// CtxºStr replaces context values in a string
func CtxºStr(rt *Runtime, input string) (string, error) {
stack := make([]string, 0)
ret, err := replace(rt, []rune(input), &stack)
return string(ret), err
}
// CtxGetºStr replaces context values in the value of the key
func CtxGetºStr(rt *Runtime, key string) (string, error) {
return CtxºStr(rt, CtxValueºStr(rt, key))
}
func replace(rt *Runtime, input []rune, stack *[]string) ([]rune, error) {
if len(input) == 0 || strings.IndexRune(string(input), CtxChar) == -1 {
return input, nil
}
var (
err error
isName, ok bool
value string
tmp []rune
)
result := make([]rune, 0, len(input))
name := make([]rune, 0, CtxLength+1)
for i := 0; i < len(input); i++ {
r := input[i]
if r != CtxChar {
if isName {
name = append(name, r)
if len(name) > CtxLength {
result = append(append(result, CtxChar), name...)
isName = false
name = name[:0]
}
} else {
result = append(result, r)
}
continue
}
if isName {
rt.Owner.CtxMutex.RLock()
value, ok = rt.Owner.Context[string(name)]
rt.Owner.CtxMutex.RUnlock()
if ok {
if len(*stack) < CtxDeep {
for _, item := range *stack {
if item == string(name) {
return result, fmt.Errorf(ErrCtxLoop, item)
}
}
} else {
return result, fmt.Errorf(ErrCtxDeep)
}
*stack = append(*stack, string(name))
if tmp, err = replace(rt, []rune(value), stack); err != nil {
return result, err
}
*stack = (*stack)[:len(*stack)-1]
result = append(result, tmp...)
} else {
result = append(append(result, CtxChar), name...)
i--
}
name = name[:0]
}
isName = !isName
}
if isName {
result = append(append(result, CtxChar), name...)
}
return result, nil
}