Using maps like objects
lucasbbb opened this issue · 2 comments
Is your feature request related to a problem? Please describe.
Noticed that I can visit a JSON Fact as a normal object or a map, but I can't visit a map like a normal object.
Describe the solution you'd like
drls := `rule Demo "demo" salience 10 {
when
myMap.name == "foo"
then
myMap.name == "bar";
Retract("Demo");
}`
myMap := map[string]string{
"name": "foo",
}
err := dataContext.AddMap("myMap", myMap)
Due to the restrictions of the Go, there is no way to set the value of a struct's non-exported filed. If this feature can be supported, I am able to use non-exported fields when defining my DSL.
Describe alternatives you've considered
An alternative solution is define a tag, for example:
type User struct {
Name string `grule:"name"`
}
drls := `rule Demo "demo" salience 10 {
when
user.name == "foo"
then
user.name == "bar";
Retract("Demo");
}`
user := User{
Name: "foo",
}
err := dataContext.Add("user", &user)
If the tag is specified, the grule engine can use it When calculating the rules.
Additional context
type User struct {
Name string `json:"name"`
}
{
"name": "foo"
}
Most APIs are defined in snake case. So I wish can define the rules in snake case.
Also, the non-exported fields are readable but unmodifiable, which means they can be used in the When Statement but can't be used in the Then Statement.