Allow dashes in identfiers
TomTheBear opened this issue · 1 comments
TomTheBear commented
I have the case that one of my identifiers contains a -
, like type-id
. The parser fails because -
is not an allowed rune in an identifier.
Example:
func main() {
vars := map[string]interface{}{"type-id": "4"}
expr := `type-id == "4"`
e, err := gval.Full().NewEvaluable(expr)
if err != nil {
return
}
value, err := e.EvalBool(context.Background(), vars)
if err != nil {
return
}
fmt.Println(value)
}
I could open a PR, but the fix is so simple: Add the condition (pos > 0 && r == '-')
to https://github.com/PaesslerAG/gval/blob/master/parser.go#L33 . The example works, I tested it even for multiple dashes. But I'm new to gval, so I might miss some cases.
hoani commented
I'm a bit late to the party, but it looks like we can do this ourselves now:
lang := gval.NewLanguage(
gval.Init(func(ctx context.Context, p *gval.Parser) (gval.Evaluable, error) {
p.SetIsIdentRuneFunc(func(r rune, pos int) bool {
return unicode.IsLetter(r) || r == '_' || (pos > 0 && unicode.IsDigit(r)) || (pos > 0 && r == '-')
})
return p.ParseExpression(ctx)
}),
gval.Full()
)