Extend rule structure and nip rule parser (#16)
This commit is contained in:
@@ -34,7 +34,7 @@ func TestEvaluate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
rule, err := nip.ParseLine(tt.args.nipRule)
|
rule, err := nip.ParseLine(tt.args.nipRule, "dummy", 0)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
if _, got := Evaluate(tt.args.i, []nip.Rule{rule}); got != tt.want {
|
if _, got := Evaluate(tt.args.i, []nip.Rule{rule}); got != tt.want {
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ func ParseNIPFile(filePath string) ([]Rule, error) {
|
|||||||
lineNumber := 0
|
lineNumber := 0
|
||||||
for fileScanner.Scan() {
|
for fileScanner.Scan() {
|
||||||
lineNumber++
|
lineNumber++
|
||||||
rule, err := ParseLine(fileScanner.Text())
|
rule, err := ParseLine(fileScanner.Text(), filePath, lineNumber)
|
||||||
if errors.Is(err, errEmptyLine) {
|
if errors.Is(err, errEmptyLine) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ var (
|
|||||||
propertyNameRegex = regexp.MustCompile(`\[(.*)\]`)
|
propertyNameRegex = regexp.MustCompile(`\[(.*)\]`)
|
||||||
)
|
)
|
||||||
|
|
||||||
func ParseLine(line string) (Rule, error) {
|
func ParseLine(line string, filename string, linenum int) (Rule, error) {
|
||||||
line = lineCleanup(line)
|
line = lineCleanup(line)
|
||||||
if line == "" {
|
if line == "" {
|
||||||
return Rule{}, errEmptyLine
|
return Rule{}, errEmptyLine
|
||||||
@@ -46,6 +46,9 @@ func ParseLine(line string) (Rule, error) {
|
|||||||
Properties: properties,
|
Properties: properties,
|
||||||
Stats: stats,
|
Stats: stats,
|
||||||
MaxQuantity: maxQuantity,
|
MaxQuantity: maxQuantity,
|
||||||
|
Filename: filename,
|
||||||
|
LineNumber: linenum,
|
||||||
|
Enabled: true,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
const nipLine = "[type] == boots && [quality] == rare # [frw] >= 10 && [fireresist] >= 10 && ([lightresist]+[coldresist] >= 10 && [dexterity] >= 1 && [fireresist]+[poisonresist] >= 10) // this is a comment"
|
const nipLine = "[type] == boots && [quality] == rare # [frw] >= 10 && [fireresist] >= 10 && ([lightresist]+[coldresist] >= 10 && [dexterity] >= 1 && [fireresist]+[poisonresist] >= 10) // this is a comment"
|
||||||
|
|
||||||
func Test_parseLine(t *testing.T) {
|
func Test_parseLine(t *testing.T) {
|
||||||
rules, err := ParseLine(nipLine)
|
rules, err := ParseLine(nipLine, "dummy", 0)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
expected := Rule{
|
expected := Rule{
|
||||||
|
|||||||
@@ -16,6 +16,9 @@ type Rule struct {
|
|||||||
Properties []Group
|
Properties []Group
|
||||||
Stats []Group
|
Stats []Group
|
||||||
MaxQuantity []Group
|
MaxQuantity []Group
|
||||||
|
Filename string
|
||||||
|
LineNumber int
|
||||||
|
Enabled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Keyword string
|
type Keyword string
|
||||||
|
|||||||
Reference in New Issue
Block a user