Skip to content

Commit

Permalink
fix(go): allow passing int32 and int64 to Compiler.DefineGlobal
Browse files Browse the repository at this point in the history
… and `Scanner.SetGlobal`
  • Loading branch information
plusvic committed Feb 28, 2024
1 parent 117cc3c commit 786442f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions go/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func (c *Compiler) DefineGlobal(ident string, value interface{}) error {
switch v := value.(type) {
case int:
ret = C.int(C.yrx_compiler_define_global_int(c.cCompiler, cIdent, C.int64_t(v)))
case int32:
ret = C.int(C.yrx_compiler_define_global_int(c.cCompiler, cIdent, C.int64_t(v)))
case int64:
ret = C.int(C.yrx_compiler_define_global_int(c.cCompiler, cIdent, C.int64_t(v)))
case bool:
ret = C.int(C.yrx_compiler_define_global_bool(c.cCompiler, cIdent, C.bool(v)))
case string:
Expand Down
4 changes: 4 additions & 0 deletions go/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ func (s *Scanner) SetGlobal(ident string, value interface{}) error {
switch v := value.(type) {
case int:
ret = C.int(C.yrx_scanner_set_global_int(s.cScanner, cIdent, C.int64_t(v)))
case int32:
ret = C.int(C.yrx_scanner_set_global_int(s.cScanner, cIdent, C.int64_t(v)))
case int64:
ret = C.int(C.yrx_scanner_set_global_int(s.cScanner, cIdent, C.int64_t(v)))
case bool:
ret = C.int(C.yrx_scanner_set_global_bool(s.cScanner, cIdent, C.bool(v)))
case string:
Expand Down
22 changes: 22 additions & 0 deletions go/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@ func TestScanner3(t *testing.T) {
assert.Len(t, matchingRules, 0)
}

func TestScanner4(t *testing.T) {
r, _ := Compile(
`rule t { condition: var_int == 1}`,
GlobalVars(map[string]interface{}{"var_int": 0}))

s := NewScanner(r)
matchingRules, _ := s.Scan([]byte{})
assert.Len(t, matchingRules, 0)

assert.NoError(t, s.SetGlobal("var_int", 1))
matchingRules, _ = s.Scan([]byte{})
assert.Len(t, matchingRules, 1)

assert.NoError(t, s.SetGlobal("var_int", int32(1)))
matchingRules, _ = s.Scan([]byte{})
assert.Len(t, matchingRules, 1)

assert.NoError(t, s.SetGlobal("var_int", int64(1)))
matchingRules, _ = s.Scan([]byte{})
assert.Len(t, matchingRules, 1)
}

func TestScannerTimeout(t *testing.T) {
r, _ := Compile("rule t { strings: $a = /a(.*)*a/ condition: $a }")
s := NewScanner(r)
Expand Down

0 comments on commit 786442f

Please sign in to comment.