Skip to content

Commit

Permalink
to better support Windows OS when using pkg.NewFileResourceBundle to … (
Browse files Browse the repository at this point in the history
#87)

* added index for viewdoc

* pushing first viewdoc

* Added more formating for viewdoc

* to better support Windows OS when using pkg.NewFileResourceBundle to load rules

* use filepath.Abs() to fix joint path in Windows OS

* reformat of indent

* to better support Windows OS when using pkg.NewFileResourceBundle to load rules

* use filepath.Abs() to fix joint path in Windows OS

* reformat of indent

* Added resource bundle test for windows

Co-authored-by: Ferdinand Neman <[email protected]>
Co-authored-by: Xiao Xi XX22 Liu <[email protected]>
  • Loading branch information
3 people authored Aug 14, 2020
1 parent 5c722fd commit 5de6f01
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 4 deletions.
6 changes: 6 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@

[![Gopheer Holds The Rule](https://github.com/hyperjumptech/grule-rule-engine/blob/master/gopher-grule.png?raw=true)](https://github.com/hyperjumptech/grule-rule-engine/blob/master/gopher-grule.png?raw=true)


__"Gopher Holds The Rules"__

# Grule

```go
Expand Down Expand Up @@ -98,3 +103,4 @@ By using Rules, you create a repository of knowledge (a knowledge base) which is
### Agility To Change

Since business rules are actually treated as data. Adjusting the rule according to business dynamic nature become trivial. No need to re-build codes, deploy as normal software development do, you only need to roll out sets of rule and apply them to knowledge repository.

7 changes: 3 additions & 4 deletions pkg/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"io/ioutil"
"net/http"
"path/filepath"

"github.com/bmatcuk/doublestar"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -102,9 +103,7 @@ func (bundle *FileResourceBundle) loadPath(path string) ([]Resource, error) {
ret := make([]Resource, 0)
for _, finfo := range finfos {
fulPath := fmt.Sprintf("%s/%s", path, finfo.Name())
if path == "/" && finfo.IsDir() {
fulPath = fmt.Sprintf("/%s", finfo.Name())
}
fulPath, _ = filepath.Abs(fulPath)
if finfo.IsDir() {
gres, err := bundle.loadPath(fulPath)
if err != nil {
Expand All @@ -113,7 +112,7 @@ func (bundle *FileResourceBundle) loadPath(path string) ([]Resource, error) {
ret = append(ret, gres...)
} else {
for _, pattern := range bundle.PathPattern {
matched, err := doublestar.Match(pattern, fulPath)
matched, err := doublestar.PathMatch(pattern, fulPath)
if err != nil {
return nil, err
}
Expand Down
43 changes: 43 additions & 0 deletions pkg/resource_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,57 @@
package pkg

import (
"os"
"reflect"
"runtime"
"strings"
"testing"
)

const (
loremipsum = `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum`
)


func TestFileResourceBundle_Load(t *testing.T) {
path, err := os.Getwd()
if err != nil {
t.Error(err)
t.Fail()
}
t.Logf("OS is : %s", runtime.GOOS)
t.Logf("Path : %s", path)
var frb *FileResourceBundle
if runtime.GOOS == "windows" {
frb = NewFileResourceBundle(path, "**\\*.grl")
} else {
frb = NewFileResourceBundle(path, "/**/*.grl")
}
resources := frb.MustLoad()
if len(resources) != 6 {
t.Errorf("Expected 6 but get %d", len(resources) )
t.FailNow()
}
if !strings.HasSuffix(resources[0].String(), "GrlFile11.grl") {
t.Errorf("Expect [0] to have suffix GrlFile11.grl. But %s", resources[0].String())
}
if !strings.HasSuffix(resources[1].String(), "GrlFile12.grl") {
t.Errorf("Expect [1] to have suffix GrlFile12.grl. But %s", resources[0].String())
}
if !strings.HasSuffix(resources[2].String(), "GrlFile21.grl") {
t.Errorf("Expect [2] to have suffix GrlFile11.grl. But %s", resources[0].String())
}
if !strings.HasSuffix(resources[3].String(), "GrlFile22.grl") {
t.Errorf("Expect [3] to have suffix GrlFile11.grl. But %s", resources[0].String())
}
if !strings.HasSuffix(resources[4].String(), "GrlFile211.grl") {
t.Errorf("Expect [4] to have suffix GrlFile11.grl. But %s", resources[0].String())
}
if !strings.HasSuffix(resources[5].String(), "GrlFile212.grl") {
t.Errorf("Expect [5] to have suffix GrlFile11.grl. But %s", resources[0].String())
}
}

func TestNewBytesResource(t *testing.T) {
resource := NewBytesResource([]byte(loremipsum))
loaded, err := resource.Load()
Expand Down
7 changes: 7 additions & 0 deletions pkg/test/subfold1/GrlFile11.grl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rule GrlFile11 "This is dummy rule" {
when
true
then
Log("Nice");
Complete();
}
7 changes: 7 additions & 0 deletions pkg/test/subfold1/GrlFile12.grl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rule GrlFile12 "This is dummy rule" {
when
true
then
Log("Nice");
Complete();
}
7 changes: 7 additions & 0 deletions pkg/test/subfold2/GrlFile21.grl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rule GrlFile21 "This is dummy rule" {
when
true
then
Log("Nice");
Complete();
}
7 changes: 7 additions & 0 deletions pkg/test/subfold2/GrlFile22.grl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rule GrlFile22 "This is dummy rule" {
when
true
then
Log("Nice");
Complete();
}
7 changes: 7 additions & 0 deletions pkg/test/subfold2/subfold21/GrlFile211.grl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rule GrlFile211 "This is dummy rule" {
when
true
then
Log("Nice");
Complete();
}
7 changes: 7 additions & 0 deletions pkg/test/subfold2/subfold21/GrlFile212.grl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rule GrlFile212 "This is dummy rule" {
when
true
then
Log("Nice");
Complete();
}

0 comments on commit 5de6f01

Please sign in to comment.