From c813c5638bd2fc8527309c33bec0de03f86fc015 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Mon, 1 Oct 2018 19:28:54 +0200 Subject: [PATCH] compliant to new 'aicra' config --- aicra.json | 4 +- manifest.json => api.json | 0 .../ROOT/main.go | 0 .../token/main.go | 0 main.go | 9 +- .../1-auth/main.go | 0 type/any/main.go | 20 +++++ type/int/main.go | 42 +++++++++ type/string/main.go | 27 ++++++ type/varchar/main.go | 89 +++++++++++++++++++ 10 files changed, 184 insertions(+), 7 deletions(-) rename manifest.json => api.json (100%) rename {controller => controller.plugin}/ROOT/main.go (100%) rename {controller => controller.plugin}/token/main.go (100%) rename {middleware => middleware.plugin}/1-auth/main.go (100%) create mode 100644 type/any/main.go create mode 100644 type/int/main.go create mode 100644 type/string/main.go create mode 100644 type/varchar/main.go diff --git a/aicra.json b/aicra.json index dd706ab..1c09a07 100644 --- a/aicra.json +++ b/aicra.json @@ -10,9 +10,9 @@ "folder": "type" }, "controllers": { - "folder": "controller" + "folder": "controller.plugin" }, "middlewares": { - "folder": "middleware" + "folder": "middleware.plugin" } } \ No newline at end of file diff --git a/manifest.json b/api.json similarity index 100% rename from manifest.json rename to api.json diff --git a/controller/ROOT/main.go b/controller.plugin/ROOT/main.go similarity index 100% rename from controller/ROOT/main.go rename to controller.plugin/ROOT/main.go diff --git a/controller/token/main.go b/controller.plugin/token/main.go similarity index 100% rename from controller/token/main.go rename to controller.plugin/token/main.go diff --git a/main.go b/main.go index 2b31422..cce0c56 100644 --- a/main.go +++ b/main.go @@ -2,22 +2,21 @@ package main import ( "git.xdrm.io/go/aicra" - "git.xdrm.io/go/aicra/driver" "log" "net/http" ) func main() { - listen_addr := "127.0.0.1:4242" + listenTo := "127.0.0.1:4242" - server, err := aicra.New("manifest.json", new(driver.Plugin)) + server, err := aicra.New("api.json") if err != nil { panic(err) } - log.Printf("[Server up] %s\n", listen_addr) - err = http.ListenAndServe(listen_addr, server) + log.Printf("[Server up] %s\n", listenTo) + err = http.ListenAndServe(listenTo, server) if err != nil { panic(err) } diff --git a/middleware/1-auth/main.go b/middleware.plugin/1-auth/main.go similarity index 100% rename from middleware/1-auth/main.go rename to middleware.plugin/1-auth/main.go diff --git a/type/any/main.go b/type/any/main.go new file mode 100644 index 0000000..ae178ee --- /dev/null +++ b/type/any/main.go @@ -0,0 +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 (ack AnyChecker) Match(name string) bool { + return name == "any" +} + +// Check always returns true +func (ack AnyChecker) Check(value interface{}) bool { + return true +} diff --git a/type/int/main.go b/type/int/main.go new file mode 100644 index 0000000..d0c4c2b --- /dev/null +++ b/type/int/main.go @@ -0,0 +1,42 @@ +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, + reflect.Int: nil, + reflect.Int8: nil, + reflect.Int16: nil, + reflect.Int32: nil, + reflect.Int64: nil, + reflect.Uint: nil, + reflect.Uint8: nil, + reflect.Uint16: nil, + reflect.Uint32: nil, + reflect.Uint64: nil, +} + +type IntChecker int + +// Match matches the string 'int' +func (ick IntChecker) Match(name string) bool { + return name == "int" +} + +// Check returns true for any type from the @validationTable +func (ick IntChecker) Check(value interface{}) bool { + + kind := reflect.TypeOf(value).Kind() + + _, isTypeValid := validationTable[kind] + + return isTypeValid + +} diff --git a/type/string/main.go b/type/string/main.go new file mode 100644 index 0000000..aadd55a --- /dev/null +++ b/type/string/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "git.xdrm.io/go/aicra/driver" + "reflect" +) + +func main() {} +func Export() driver.Checker { return new(StringChecker) } + +type StringChecker int + +func (sck StringChecker) Match(name string) bool { + return name == "string" +} + +func (sck StringChecker) Check(value interface{}) bool { + + if value == nil { + return false + } + + kind := reflect.TypeOf(value).Kind() + + return kind == reflect.String + +} diff --git a/type/varchar/main.go b/type/varchar/main.go new file mode 100644 index 0000000..b332fad --- /dev/null +++ b/type/varchar/main.go @@ -0,0 +1,89 @@ +package main + +import ( + "git.xdrm.io/go/aicra/driver" + "regexp" + "strconv" +) + +func main() {} +func Export() driver.Checker { return new(VarcharChecker) } + +var min *uint64 +var max *uint64 + +type VarcharChecker struct { + min *uint64 + max *uint64 +} + +// Match filters the parameter type format "varchar(min, max)" +func (vck *VarcharChecker) Match(name string) bool { + + vck.min = nil + vck.max = nil + + /* (1) Create regexp */ + re, err := regexp.Compile(`^varchar\((\d+), ?(\d+)\)$`) + if err != nil { + panic(err) + } + + /* (2) Check if matches */ + matches := re.FindStringSubmatch(name) + if matches == nil || len(matches) < 3 { + return false + } + + /* (3) Extract min */ + minVal, err := strconv.ParseUint(matches[1], 10, 64) + if err != nil { + return false + } + vck.min = &minVal + + /* (4) Extract max */ + maxVal, err := strconv.ParseUint(matches[2], 10, 64) + if err != nil { + return false + } + + /* (5) Check that min <= max */ + if maxVal < minVal { + panic("varchar(x, y) ; constraint violation : x <= y") + } + vck.max = &maxVal + + return true + +} + +// Check whether the given value fulfills the condition (min, max) +func (vck *VarcharChecker) Check(value interface{}) bool { + + /* (1) Check if string */ + strval, ok := value.(string) + if !ok { + return false + } + + /* (2) Check if sizes set */ + if vck.min == nil || vck.max == nil { + return false + } + + length := uint64(len(strval)) + + /* (3) Check min */ + if length < *vck.min { + return false + } + + /* (4) Check max */ + if length > *vck.max { + return false + } + + return true + +}