RSRParser.Compile with tests

This commit is contained in:
DanB
2018-07-20 19:34:08 +02:00
parent 68e6475892
commit 37f22b7ce0
2 changed files with 41 additions and 8 deletions

View File

@@ -24,7 +24,7 @@ import (
"strings"
)
func NewRSRParser(parserRules, inRuleSep string) (rsrParser *RSRParser, err error) {
func NewRSRParser(parserRules string) (rsrParser *RSRParser, err error) {
if len(parserRules) == 0 {
return
}
@@ -35,7 +35,7 @@ func NewRSRParser(parserRules, inRuleSep string) (rsrParser *RSRParser, err erro
return nil, fmt.Errorf("invalid RSRFilter start rule in string: <%s>", parserRules)
}
fltrVal := parserRules[fltrStart+1 : len(parserRules)-1]
rsrParser.filters, err = ParseRSRFilters(fltrVal, inRuleSep)
rsrParser.filters, err = ParseRSRFilters(fltrVal, ANDSep)
if err != nil {
return nil, fmt.Errorf("Invalid FilterValue in string: %s, err: %s", fltrVal, err.Error())
}
@@ -48,7 +48,7 @@ func NewRSRParser(parserRules, inRuleSep string) (rsrParser *RSRParser, err erro
parserRules)
}
convertersStr := parserRules[idxConverters+1 : len(parserRules)-1] // strip also {}
convsSplt := strings.Split(convertersStr, inRuleSep)
convsSplt := strings.Split(convertersStr, ANDSep)
rsrParser.converters = make(DataConverters, len(convsSplt))
for i, convStr := range convsSplt {
if conv, err := NewDataConverter(convStr); err != nil {
@@ -109,17 +109,27 @@ type RSRParser struct {
filters RSRFilters // The value to compare when used as filter
}
func NewRSRParsers(parsersRules, rlsSep, inRlSep string) (prsrs RSRParsers, err error) {
// Compile parses Rules string and repopulates other fields
func (prsr *RSRParser) Compile() (err error) {
var newPrsr *RSRParser
if newPrsr, err = NewRSRParser(prsr.Rules); err != nil {
return
}
*prsr = *newPrsr
return
}
func NewRSRParsers(parsersRules string) (prsrs RSRParsers, err error) {
if parsersRules == "" {
return
}
return NewRSRParsersFromSlice(strings.Split(parsersRules, rlsSep), inRlSep)
return NewRSRParsersFromSlice(strings.Split(parsersRules, INFIELD_SEP))
}
func NewRSRParsersFromSlice(parsersRules []string, inRlSep string) (prsrs RSRParsers, err error) {
func NewRSRParsersFromSlice(parsersRules []string) (prsrs RSRParsers, err error) {
prsrs = make(RSRParsers, len(parsersRules))
for i, rlStr := range parsersRules {
if rsrPrsr, err := NewRSRParser(rlStr, inRlSep); err != nil {
if rsrPrsr, err := NewRSRParser(rlStr); err != nil {
return nil, err
} else if rsrPrsr == nil {
return nil, fmt.Errorf("emtpy RSRParser in rule: <%s>", rlStr)

View File

@@ -48,9 +48,32 @@ func TestNewRSRParsers(t *testing.T) {
NewDataConverterMustCompile("*round:2")},
},
}
if rsrParsers, err := NewRSRParsers(ruleStr, INFIELD_SEP, ANDSep); err != nil {
if rsrParsers, err := NewRSRParsers(ruleStr); err != nil {
t.Error("Unexpected error: ", err.Error())
} else if !reflect.DeepEqual(eRSRParsers, rsrParsers) {
t.Errorf("expecting: %+v, received: %+v", eRSRParsers, rsrParsers)
}
}
func TestRSRParserCompile(t *testing.T) {
ePrsr := &RSRParser{
Rules: "~Header4:s/a/${1}b/{*duration_seconds&*round:2}(b&c)",
attrName: "Header4",
rsrRules: []*ReSearchReplace{
&ReSearchReplace{
SearchRegexp: regexp.MustCompile(`a`),
ReplaceTemplate: "${1}b"}},
converters: DataConverters{NewDataConverterMustCompile("*duration_seconds"),
NewDataConverterMustCompile("*round:2")},
filters: RSRFilters{NewRSRFilterMustCompile("b"),
NewRSRFilterMustCompile("c")},
}
prsr := &RSRParser{
Rules: "~Header4:s/a/${1}b/{*duration_seconds&*round:2}(b&c)",
}
if err := prsr.Compile(); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(ePrsr, prsr) {
t.Errorf("expecting: %+v, received: %+v", ePrsr, prsr)
}
}