refactor default type checkers | load default types if set in aicra.json (.build/DEFAULT_TYPES/*.so)

This commit is contained in:
Adrien Marquès 2018-10-02 11:10:21 +02:00
parent 8c0d4df4b3
commit b1092437f9
7 changed files with 77 additions and 16 deletions

View File

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"git.xdrm.io/go/aicra/driver"
"git.xdrm.io/go/aicra/internal/clifmt" "git.xdrm.io/go/aicra/internal/clifmt"
"git.xdrm.io/go/aicra/internal/config" "git.xdrm.io/go/aicra/internal/config"
"os" "os"
@ -43,6 +44,7 @@ func main() {
/* (2) Compile Default Types */ /* (2) Compile Default Types */
if schema.Types.Default { if schema.Types.Default {
clifmt.Title("compile default types") clifmt.Title("compile default types")
files, err := filepath.Glob(defaultTypeFolder) files, err := filepath.Glob(defaultTypeFolder)
if err != nil { if err != nil {
@ -62,7 +64,7 @@ func main() {
// Get useful paths // Get useful paths
source := filepath.Join(file, "main.go") source := filepath.Join(file, "main.go")
build := filepath.Join(schema.Root, ".build/type", fmt.Sprintf("%s.so", typeName)) build := filepath.Join(schema.Root, ".build/DEFAULT_TYPES", fmt.Sprintf("%s.so", typeName))
compile(source, build) compile(source, build)
} }

View File

@ -1,11 +1,20 @@
package main package main
import (
"git.xdrm.io/go/aicra/driver"
)
func main() {}
func Export() driver.Checker { return new(AnyChecker) }
type AnyChecker int
// Match matches the string 'any' // Match matches the string 'any'
func Match(name string) bool { func (ack AnyChecker) Match(name string) bool {
return name == "any" return name == "any"
} }
// Check always returns true // Check always returns true
func Check(value interface{}) bool { func (ack AnyChecker) Check(value interface{}) bool {
return true return true
} }

View File

@ -1,9 +1,13 @@
package main package main
import ( import (
"git.xdrm.io/go/aicra/driver"
"reflect" "reflect"
) )
func main() {}
func Export() driver.Checker { return new(IntChecker) }
var validationTable = map[reflect.Kind]interface{}{ var validationTable = map[reflect.Kind]interface{}{
reflect.Float32: nil, reflect.Float32: nil,
reflect.Float64: nil, reflect.Float64: nil,
@ -19,13 +23,15 @@ var validationTable = map[reflect.Kind]interface{}{
reflect.Uint64: nil, reflect.Uint64: nil,
} }
type IntChecker int
// Match matches the string 'int' // Match matches the string 'int'
func Match(name string) bool { func (ick IntChecker) Match(name string) bool {
return name == "int" return name == "int"
} }
// Check returns true for any type from the @validationTable // Check returns true for any type from the @validationTable
func Check(value interface{}) bool { func (ick IntChecker) Check(value interface{}) bool {
kind := reflect.TypeOf(value).Kind() kind := reflect.TypeOf(value).Kind()

View File

@ -1,14 +1,20 @@
package main package main
import ( import (
"git.xdrm.io/go/aicra/driver"
"reflect" "reflect"
) )
func Match(name string) bool { func main() {}
func Export() driver.Checker { return new(StringChecker) }
type StringChecker int
func (sck StringChecker) Match(name string) bool {
return name == "string" return name == "string"
} }
func Check(value interface{}) bool { func (sck StringChecker) Check(value interface{}) bool {
if value == nil { if value == nil {
return false return false

View File

@ -1,15 +1,24 @@
package main package main
import ( import (
"git.xdrm.io/go/aicra/driver"
"regexp" "regexp"
"strconv" "strconv"
) )
var min *uint64 func main() {}
var max *uint64 func Export() driver.Checker { return new(VarcharChecker) }
type VarcharChecker struct {
min *uint64
max *uint64
}
// Match filters the parameter type format "varchar(min, max)" // Match filters the parameter type format "varchar(min, max)"
func Match(name string) bool { func (vck *VarcharChecker) Match(name string) bool {
vck.min = nil
vck.max = nil
/* (1) Create regexp */ /* (1) Create regexp */
re, err := regexp.Compile(`^varchar\((\d+), ?(\d+)\)$`) re, err := regexp.Compile(`^varchar\((\d+), ?(\d+)\)$`)
@ -28,7 +37,7 @@ func Match(name string) bool {
if err != nil { if err != nil {
return false return false
} }
min = &minVal vck.min = &minVal
/* (4) Extract max */ /* (4) Extract max */
maxVal, err := strconv.ParseUint(matches[2], 10, 64) maxVal, err := strconv.ParseUint(matches[2], 10, 64)
@ -40,14 +49,14 @@ func Match(name string) bool {
if maxVal < minVal { if maxVal < minVal {
panic("varchar(x, y) ; constraint violation : x <= y") panic("varchar(x, y) ; constraint violation : x <= y")
} }
max = &maxVal vck.max = &maxVal
return true return true
} }
// Check whether the given value fulfills the condition (min, max) // Check whether the given value fulfills the condition (min, max)
func Check(value interface{}) bool { func (vck *VarcharChecker) Check(value interface{}) bool {
/* (1) Check if string */ /* (1) Check if string */
strval, ok := value.(string) strval, ok := value.(string)
@ -56,17 +65,19 @@ func Check(value interface{}) bool {
} }
/* (2) Check if sizes set */ /* (2) Check if sizes set */
if min == nil || max == nil { if vck.min == nil || vck.max == nil {
return false return false
} }
length := uint64(len(strval))
/* (3) Check min */ /* (3) Check min */
if uint64(len(strval)) < *min { if length < *vck.min {
return false return false
} }
/* (4) Check max */ /* (4) Check max */
if uint64(len(strval)) > *max { if length > *vck.max {
return false return false
} }

View File

@ -26,6 +26,10 @@ func (reg Registry) Run(typeName string, value interface{}) error {
/* (1) Iterate to find matching type (take first) */ /* (1) Iterate to find matching type (take first) */
for _, t := range reg { for _, t := range reg {
if t == nil {
continue
}
// stop if found // stop if found
if t.Match(typeName) { if t.Match(typeName) {

View File

@ -1,6 +1,8 @@
package aicra package aicra
import ( import (
"errors"
"git.xdrm.io/go/aicra/driver"
e "git.xdrm.io/go/aicra/err" e "git.xdrm.io/go/aicra/err"
"git.xdrm.io/go/aicra/internal/api" "git.xdrm.io/go/aicra/internal/api"
"git.xdrm.io/go/aicra/internal/checker" "git.xdrm.io/go/aicra/internal/checker"
@ -10,6 +12,7 @@ import (
"git.xdrm.io/go/aicra/response" "git.xdrm.io/go/aicra/response"
"log" "log"
"net/http" "net/http"
"path/filepath"
"strings" "strings"
) )
@ -52,6 +55,26 @@ func New(_path string) (*Server, error) {
i.checker = checker.CreateRegistry() i.checker = checker.CreateRegistry()
// add default types if set // add default types if set
if schema.Types.Default {
// driver is Plugin for defaults (even if generic for the controllers etc)
defaultTypesDriver := new(driver.Plugin)
files, err := filepath.Glob(filepath.Join(schema.Root, ".build/DEFAULT_TYPES/*.so"))
if err != nil {
return nil, errors.New("cannot load default types")
}
for _, path := range files {
name := strings.TrimSuffix(filepath.Base(path), ".so")
mwFunc, err := defaultTypesDriver.LoadChecker(path)
if err != nil {
log.Printf("cannot load default type checker '%s' | %s", name, err)
}
i.checker.Add(name, mwFunc)
}
}
// add custom types // add custom types
for name, path := range schema.Types.Map { for name, path := range schema.Types.Map {