-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added group finder * updated readme * added information of group finder to the main README * minor fixes on documentation
- Loading branch information
1 parent
602c8ef
commit caa84ab
Showing
21 changed files
with
2,801 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") | ||
|
||
go_library( | ||
name = "finder_lib", | ||
srcs = ["main.go"], | ||
importpath = "github.com/pedroegsilva/gofindthem/examples/group/finder", | ||
visibility = ["//visibility:private"], | ||
deps = [ | ||
"//finder", | ||
"//group/finder", | ||
], | ||
) | ||
|
||
go_binary( | ||
name = "finder", | ||
embed = [":finder_lib"], | ||
visibility = ["//visibility:public"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pedroegsilva/gofindthem/finder" | ||
gfinder "github.com/pedroegsilva/gofindthem/group/finder" | ||
) | ||
|
||
func main() { | ||
gofindthemRules := map[string][]string{ | ||
"tag1": { | ||
`"string1"`, | ||
`"string2"`, | ||
}, | ||
"tag2": { | ||
`"string3"`, | ||
`"string4"`, | ||
}, | ||
"tag3": { | ||
`"string5"`, | ||
`"string6"`, | ||
}, | ||
"tag4": { | ||
`"string7"`, | ||
`"string8"`, | ||
}, | ||
} | ||
|
||
rules := map[string][]string{ | ||
"rule1": {`"tag1" or "tag2"`}, | ||
"rule2": {`"tag3:Field3.SomeField1" or "tag4"`}, | ||
"rule3": {`"tag3:Field3" or "tag4"`}, | ||
} | ||
|
||
gft, err := finder.NewFinderWithExpressions( | ||
&finder.CloudflareForkEngine{}, | ||
&finder.RegexpEngine{}, | ||
false, | ||
gofindthemRules, | ||
) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
gftg, err := gfinder.NewFinderWithRules(gft, rules) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
someObject := struct { | ||
Field1 string | ||
Field2 int | ||
Field3 struct { | ||
SomeField1 string | ||
SomeField2 []string | ||
} | ||
}{ | ||
Field1: "some pretty text with string1", | ||
Field2: 42, | ||
Field3: struct { | ||
SomeField1 string | ||
SomeField2 []string | ||
}{ | ||
SomeField1: "some pretty text with string5", | ||
SomeField2: []string{"some pretty text with string5", "some pretty text with string2", "some pretty text with string3"}, | ||
}, | ||
} | ||
|
||
matchedExpByFieldByTag, err := gftg.TagObject(someObject, gftg.GetFieldNames(), nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for tag, expressionsByField := range matchedExpByFieldByTag { | ||
fmt.Println("Tag: ", tag) | ||
for field, exprs := range expressionsByField { | ||
fmt.Println(" Field: ", field) | ||
for exp := range exprs { | ||
fmt.Println(" Expressions: ", exp) | ||
} | ||
} | ||
} | ||
|
||
res, err := gftg.ProcessObject(someObject, gftg.GetFieldNames(), nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println("ProcessObject: ", res) | ||
|
||
fmt.Println("-----------------------------") | ||
arr := []struct { | ||
FieldN string | ||
FieldX string | ||
}{ | ||
{FieldN: "some pretty text with string5"}, | ||
{FieldN: "some pretty text with string2"}, | ||
{FieldN: "some pretty text with string3"}, | ||
} | ||
|
||
matchedExpByFieldByTag2, err := gftg.TagObject(arr, nil, nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
for tag, expressionsByField := range matchedExpByFieldByTag2 { | ||
fmt.Println("Tag: ", tag) | ||
for field, exprs := range expressionsByField { | ||
fmt.Println(" Field: ", field) | ||
for exp := range exprs { | ||
fmt.Println(" Expressions: ", exp) | ||
} | ||
} | ||
} | ||
|
||
res2, err := gftg.ProcessObject(arr, nil, nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println("ProcessObject2: ", res2) | ||
|
||
fmt.Println("-----------------------------") | ||
rawJson := ` | ||
{ | ||
"Field1": "some pretty text with string1", | ||
"Field2": 42, | ||
"Field3": | ||
{ | ||
"SomeField1": "some pretty text with string5", | ||
"SomeField2": | ||
[ | ||
"some pretty text with string5", | ||
"some pretty text with string2", | ||
"some pretty text with string3" | ||
] | ||
} | ||
} | ||
` | ||
|
||
matchedExpByFieldByTag3, err := gftg.TagJson(rawJson, gftg.GetFieldNames(), nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
for tag, expressionsByField := range matchedExpByFieldByTag3 { | ||
fmt.Println("Tag: ", tag) | ||
for field, exprs := range expressionsByField { | ||
fmt.Println(" Field: ", field) | ||
for exp := range exprs { | ||
fmt.Println(" Expressions: ", exp) | ||
} | ||
} | ||
} | ||
res3, err := gftg.ProcessJson(rawJson, gftg.GetFieldNames(), nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println("ProcessJson: ", res3) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# Group Finder | ||
Group finder is a package that adds another DSL to improve the maintainability | ||
of the searched patterns and enables searches on specific fields of structured documents. | ||
|
||
## Finder | ||
The finder is used to manage multiple rules. It will use the DSL along with the gofindthem finder | ||
to verify if the tags where found on the specified fields. | ||
|
||
### Usage | ||
You will need 2 sets of expressions, one to define the patterns that are needed to | ||
be searched with its tag and the second one with the expressions to define the tags | ||
and field relations. | ||
|
||
```golang | ||
gofindthemRules := map[string][]string{ | ||
"tag1": { | ||
`"string1"`, | ||
`"string2"`, | ||
}, | ||
"tag2": { | ||
`"string3"`, | ||
`"string4"`, | ||
}, | ||
"tag3": { | ||
`"string5"`, | ||
`"string6"`, | ||
}, | ||
"tag4": { | ||
`"string7"`, | ||
`"string8"`, | ||
}, | ||
} | ||
|
||
rules := map[string][]string{ | ||
"rule1": {`"tag1" or "tag2" and not "tag3"`}, | ||
"rule2": {`"tag3:Field3.SomeField1" or "tag4"`}, | ||
"rule3": {`"tag3:Field3" or "tag4"`}, | ||
} | ||
``` | ||
|
||
With the 2 sets of expressions ready you will first need to create the | ||
gofinthem finder and the group finder: | ||
|
||
```golang | ||
gft, err := finder.NewFinderWithExpressions( | ||
&finder.CloudflareForkEngine{}, | ||
&finder.RegexpEngine{}, | ||
false, | ||
gofindthemRules, | ||
) | ||
|
||
gftg, err := gfinder.NewFinderWithRules(gft, rules) | ||
if err != nil { | ||
panic(err) | ||
} | ||
``` | ||
|
||
Now its possible check which rules where evaluated as true on a text | ||
or on a structured document: | ||
|
||
```golang | ||
// searching on a struct | ||
res, err := gftg.ProcessObject(someObject, gftg.GetFieldNames(), nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println("ProcessObject: ", res) | ||
|
||
// searching on a raw json | ||
res3, err := gftg.ProcessJson(rawJson, gftg.GetFieldNames(), nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println("ProcessJson: ", res3) | ||
``` | ||
The full example can be found at `/examples/group/finder/main.go` | ||
|
||
## Group Finder DSL | ||
### Definition | ||
The DSL uses 3 operators (AND, OR, NOT), Tag (defined by "tag:(field)"), | ||
where the field is optional, and parentheses to form expressions. | ||
A valid expression can be: | ||
|
||
- A single rule with or without a specific field. Eg: `"tag1"` `"tag1:field1"` | ||
- The result of an operation. `"tag1" OR "tag2:field1"` | ||
- An expression enclosed by parentheses `("tag1" OR "tag2:field1")` | ||
|
||
Each operator functions as the following: | ||
|
||
- **AND** - Uses the expression before and after it to solve them as a logical `AND` operator. | ||
> (valid expression) AND (valid expression) eg: `"tag1" AND "tag2"` | ||
- **OR** - Uses the expression before and after it to solve them as a logical `OR` operator. | ||
> \<valid expression\> OR \<valid expression\> eg: `"tag1" OR "tag2"` | ||
- **NOT** - Uses the expression after it to solve them as a logical `NOT` operator. | ||
> NOT \<valid expression\> eg: `NOT "tag1"` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "dsl", | ||
srcs = [ | ||
"expression.go", | ||
"parser.go", | ||
"scanner.go", | ||
], | ||
importpath = "github.com/pedroegsilva/gofindthem/group/dsl", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
go_test( | ||
name = "dsl_test", | ||
srcs = [ | ||
"expression_test.go", | ||
"parser_test.go", | ||
"scanner_test.go", | ||
], | ||
embed = [":dsl"], | ||
deps = ["@com_github_stretchr_testify//assert"], | ||
) |
Oops, something went wrong.