-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathlua-load-per-thread.go
More file actions
187 lines (167 loc) · 4.84 KB
/
lua-load-per-thread.go
File metadata and controls
187 lines (167 loc) · 4.84 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
Copyright 2019 HAProxy Technologies
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either expressed or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package parsers
import (
"github.com/haproxytech/client-native/v6/config-parser/common"
"github.com/haproxytech/client-native/v6/config-parser/errors"
"github.com/haproxytech/client-native/v6/config-parser/types"
)
type LuaLoadPerThread struct {
data []types.LuaLoad
preComments []string // comments that appear before the actual line
}
func (p *LuaLoadPerThread) Init() {
p.data = []types.LuaLoad{}
p.preComments = []string{}
}
func (p *LuaLoadPerThread) GetParserName() string {
return "lua-load-per-thread"
}
func (p *LuaLoadPerThread) Get(createIfNotExist bool) (common.ParserData, error) {
if len(p.data) == 0 && !createIfNotExist {
return nil, errors.ErrFetch
}
return p.data, nil
}
func (p *LuaLoadPerThread) GetPreComments() ([]string, error) {
return p.preComments, nil
}
func (p *LuaLoadPerThread) SetPreComments(preComments []string) {
p.preComments = preComments
}
func (p *LuaLoadPerThread) GetOne(index int) (common.ParserData, error) {
if index < 0 || index >= len(p.data) {
return nil, errors.ErrFetch
}
return p.data[index], nil
}
func (p *LuaLoadPerThread) Delete(index int) error {
if index < 0 || index >= len(p.data) {
return errors.ErrFetch
}
copy(p.data[index:], p.data[index+1:])
p.data[len(p.data)-1] = types.LuaLoad{}
p.data = p.data[:len(p.data)-1]
return nil
}
func (p *LuaLoadPerThread) Insert(data common.ParserData, index int) error {
if data == nil {
return errors.ErrInvalidData
}
switch newValue := data.(type) {
case []types.LuaLoad:
p.data = newValue
case *types.LuaLoad:
if index > -1 {
if index > len(p.data) {
return errors.ErrIndexOutOfRange
}
p.data = append(p.data, types.LuaLoad{})
copy(p.data[index+1:], p.data[index:])
p.data[index] = *newValue
} else {
p.data = append(p.data, *newValue)
}
case types.LuaLoad:
if index > -1 {
if index > len(p.data) {
return errors.ErrIndexOutOfRange
}
p.data = append(p.data, types.LuaLoad{})
copy(p.data[index+1:], p.data[index:])
p.data[index] = newValue
} else {
p.data = append(p.data, newValue)
}
default:
return errors.ErrInvalidData
}
return nil
}
func (p *LuaLoadPerThread) Set(data common.ParserData, index int) error {
if data == nil {
p.Init()
return nil
}
switch newValue := data.(type) {
case []types.LuaLoad:
p.data = newValue
case *types.LuaLoad:
if index > -1 && index < len(p.data) {
p.data[index] = *newValue
} else if index == -1 {
p.data = append(p.data, *newValue)
} else {
return errors.ErrIndexOutOfRange
}
case types.LuaLoad:
if index > -1 && index < len(p.data) {
p.data[index] = newValue
} else if index == -1 {
p.data = append(p.data, newValue)
} else {
return errors.ErrIndexOutOfRange
}
default:
return errors.ErrInvalidData
}
return nil
}
func (p *LuaLoadPerThread) PreParse(line string, parts []string, preComments []string, comment string) (string, error) {
changeState, err := p.Parse(line, parts, comment)
if err == nil && preComments != nil {
p.preComments = append(p.preComments, preComments...)
}
return changeState, err
}
func (p *LuaLoadPerThread) Parse(line string, parts []string, comment string) (string, error) {
if parts[0] == "lua-load-per-thread" {
data, err := p.parse(line, parts, comment)
if err != nil {
if _, ok := err.(*errors.ParseError); ok {
return "", err
}
return "", &errors.ParseError{Parser: "LuaLoadPerThread", Line: line}
}
p.data = append(p.data, *data)
return "", nil
}
return "", &errors.ParseError{Parser: "LuaLoadPerThread", Line: line}
}
func (p *LuaLoadPerThread) Result() ([]common.ReturnResultLine, error) {
if len(p.data) == 0 {
return nil, errors.ErrFetch
}
result := make([]common.ReturnResultLine, len(p.data))
for index, data := range p.data {
result[index] = common.ReturnResultLine{
Data: "lua-load-per-thread " + data.File,
Comment: data.Comment,
}
}
return result, nil
}
func (p *LuaLoadPerThread) ResultAll() ([]common.ReturnResultLine, []string, error) {
res, err := p.Result()
return res, p.preComments, err
}
func (p *LuaLoadPerThread) parse(line string, parts []string, comment string) (*types.LuaLoad, error) {
if len(parts) < 2 {
return nil, &errors.ParseError{Parser: "LuaLoadPerThread", Line: line}
}
lua := &types.LuaLoad{
File: parts[1],
Comment: comment,
}
return lua, nil
}