Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 187 additions & 0 deletions config-parser/parsers/lua-load-per-thread.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}
2 changes: 1 addition & 1 deletion config-parser/section-parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ func (p *configParser) getGlobalParser() *Parsers { //nolint: maintidx
addParser(parser, &sequence, &simple.Enabled{Name: "insecure-setuid-wanted"})
addParser(parser, &sequence, &simple.String{Name: "issuers-chain-path"})
addParser(parser, &sequence, &simple.Enabled{Name: "h2-workaround-bogus-websocket-clients"})
addParser(parser, &sequence, &simple.String{Name: "lua-load-per-thread"})
addParser(parser, &sequence, &parsers.LuaLoadPerThread{})
addParser(parser, &sequence, &simple.Number{Name: "mworker-max-reloads"})
addParser(parser, &sequence, &simple.String{Name: "node"})
addParser(parser, &sequence, &parsers.NumaCPUMapping{})
Expand Down
120 changes: 120 additions & 0 deletions config-parser/tests/lua-load-per-thread_generated_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading