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 (
"fmt"
"git.xdrm.io/go/aicra/driver"
"git.xdrm.io/go/aicra/internal/clifmt"
"git.xdrm.io/go/aicra/internal/config"
"os"
@ -43,6 +44,7 @@ func main() {
/* (2) Compile Default Types */
if schema.Types.Default {
clifmt.Title("compile default types")
files, err := filepath.Glob(defaultTypeFolder)
if err != nil {
@ -62,7 +64,7 @@ func main() {
// Get useful paths
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)
}

View File

@ -1,11 +1,20 @@
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'
func Match(name string) bool {
func (ack AnyChecker) Match(name string) bool {
return name == "any"
}
// Check always returns true
func Check(value interface{}) bool {
func (ack AnyChecker) Check(value interface{}) bool {
return true
}

View File

@ -1,9 +1,13 @@
package main
import (
"git.xdrm.io/go/aicra/driver"
"reflect"
)
func main() {}
func Export() driver.Checker { return new(IntChecker) }
var validationTable = map[reflect.Kind]interface{}{
reflect.Float32: nil,
reflect.Float64: nil,
@ -19,13 +23,15 @@ var validationTable = map[reflect.Kind]interface{}{
reflect.Uint64: nil,
}
type IntChecker int
// Match matches the string 'int'
func Match(name string) bool {
func (ick IntChecker) Match(name string) bool {
return name == "int"
}
// 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()

View File

@ -1,14 +1,20 @@
package main
import (
"git.xdrm.io/go/aicra/driver"
"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"
}
func Check(value interface{}) bool {
func (sck StringChecker) Check(value interface{}) bool {
if value == nil {
return false

View File

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

View File

@ -1,6 +1,8 @@
package aicra
import (
"errors"
"git.xdrm.io/go/aicra/driver"
e "git.xdrm.io/go/aicra/err"
"git.xdrm.io/go/aicra/internal/api"
"git.xdrm.io/go/aicra/internal/checker"
@ -10,6 +12,7 @@ import (
"git.xdrm.io/go/aicra/response"
"log"
"net/http"
"path/filepath"
"strings"
)
@ -52,6 +55,26 @@ func New(_path string) (*Server, error) {
i.checker = checker.CreateRegistry()
// 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
for name, path := range schema.Types.Map {