Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expression_validate function #6

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .clangd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CompileFlags:
Add: ['-DDEFINE_ATC_ROUTER_FFI=1']
95 changes: 94 additions & 1 deletion atc-router.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package goatcrouter

// #cgo CFLAGS: -DDEFINE_ATC_ROUTER_FFI=1
// #cgo LDFLAGS: -L/tmp/lib -latc_router
// #include <stdlib.h>
// #include "atc-router.h"
import "C"

import (
"fmt"
"runtime"
"slices"
"unsafe"

"github.com/google/uuid"
Expand Down Expand Up @@ -113,3 +114,95 @@ func (r *Router) GetFields() ([]string, error) {

return flds, nil
}

const (
defaultNumFields = 100
defaultMaxFieldSize = 100
errBufsize = 500
mayurm88 marked this conversation as resolved.
Show resolved Hide resolved
)

var (
fieldsBuf = []byte{}
fieldsLen = C.ulong(defaultMaxFieldSize * defaultNumFields)
fieldsNum = C.ulong(defaultNumFields)
errorBuf = [errBufsize]C.uchar{}
)

type ValidationResult struct {
fields []string
operators uint64
errorMsg string
}

func ValidateExpression(s Schema, atc string) *ValidationResult {
atcC := unsafe.Pointer(C.CString(atc))
defer C.free(atcC)

operators := C.ulong(0)
errorBufLen := C.ulong(errBufsize)

loop:
for {
if len(fieldsBuf) < int(fieldsLen) {
fieldsBuf = make([]byte, fieldsLen)
}

switch C.expression_validate(
(*C.uchar)(atcC), s.s,
(*C.uchar)(&fieldsBuf[0]), &fieldsLen, &fieldsNum,
&operators,
&errorBuf[0], &errorBufLen) {
case 0:
break loop
case 1:
return &ValidationResult{
errorMsg: C.GoStringN(
(*C.char)(unsafe.Pointer(&errorBuf[0])),
C.int(errorBufLen),
),
}
mayurm88 marked this conversation as resolved.
Show resolved Hide resolved
case 2:
continue
}
}
return &ValidationResult{
fields: splitByNulls(fieldsBuf, int(fieldsNum)),
operators: uint64(operators),
}
}

const (
BinaryOperatorFlags_EQUALS = 1 << iota
BinaryOperatorFlags_NOT_EQUALS
BinaryOperatorFlags_REGEX
BinaryOperatorFlags_PREFIX
BinaryOperatorFlags_POSTFIX
BinaryOperatorFlags_GREATER
BinaryOperatorFlags_GREATER_OR_EQUAL
BinaryOperatorFlags_LESS
BinaryOperatorFlags_LESS_OR_EQUAL
BinaryOperatorFlags_IN
BinaryOperatorFlags_NOT_IN
BinaryOperatorFlags_CONTAINS
)
mayurm88 marked this conversation as resolved.
Show resolved Hide resolved

func splitByNulls(b []byte, n int) []string {
out := make([]string, n)
pos := 0
for i := range out {
fieldLen := slices.Index(b[pos:], 0)
if fieldLen < 0 {
break
}

str := C.GoStringN((*C.char)(unsafe.Pointer(&b[pos])), C.int(fieldLen))

out[i] = str
pos += fieldLen + 1
if pos >= len(b) {
break
}
}
slices.Sort(out)
mayurm88 marked this conversation as resolved.
Show resolved Hide resolved
return out
}
Loading