From b1092437f98916a16b92416b8fab417a229a187f Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 2 Oct 2018 11:10:21 +0200 Subject: [PATCH] refactor default type checkers | load default types if set in aicra.json (.build/DEFAULT_TYPES/*.so) --- cmd/aicra/main.go | 4 +++- internal/checker/default/any/main.go | 13 +++++++++-- internal/checker/default/int/main.go | 10 ++++++-- internal/checker/default/string/main.go | 10 ++++++-- internal/checker/default/varchar/main.go | 29 ++++++++++++++++-------- internal/checker/public.go | 4 ++++ server.go | 23 +++++++++++++++++++ 7 files changed, 77 insertions(+), 16 deletions(-) diff --git a/cmd/aicra/main.go b/cmd/aicra/main.go index f0d7f87..32e8da5 100644 --- a/cmd/aicra/main.go +++ b/cmd/aicra/main.go @@ -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) } diff --git a/internal/checker/default/any/main.go b/internal/checker/default/any/main.go index f2604c5..ae178ee 100644 --- a/internal/checker/default/any/main.go +++ b/internal/checker/default/any/main.go @@ -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 } diff --git a/internal/checker/default/int/main.go b/internal/checker/default/int/main.go index 74a1cdf..d0c4c2b 100644 --- a/internal/checker/default/int/main.go +++ b/internal/checker/default/int/main.go @@ -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() diff --git a/internal/checker/default/string/main.go b/internal/checker/default/string/main.go index 89e8086..aadd55a 100644 --- a/internal/checker/default/string/main.go +++ b/internal/checker/default/string/main.go @@ -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 diff --git a/internal/checker/default/varchar/main.go b/internal/checker/default/varchar/main.go index 05ec454..5c09876 100644 --- a/internal/checker/default/varchar/main.go +++ b/internal/checker/default/varchar/main.go @@ -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 } diff --git a/internal/checker/public.go b/internal/checker/public.go index 4fc1004..02e2fe8 100644 --- a/internal/checker/public.go +++ b/internal/checker/public.go @@ -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) { diff --git a/server.go b/server.go index ab5a4ca..e7f7160 100644 --- a/server.go +++ b/server.go @@ -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 {