diff --git a/controller.plugin/ROOT/main.go b/controller.plugin/ROOT/main.go index a4d7781..55070b6 100644 --- a/controller.plugin/ROOT/main.go +++ b/controller.plugin/ROOT/main.go @@ -2,10 +2,9 @@ package main import ( "git.xdrm.io/example/aicra/db" + "git.xdrm.io/go/aicra/api" "git.xdrm.io/go/aicra/driver" e "git.xdrm.io/go/aicra/err" - "git.xdrm.io/go/aicra/response" - i "git.xdrm.io/go/aicra/response" ) func main() {} @@ -15,9 +14,9 @@ type RootController int func Export() driver.Controller { return new(RootController) } // Redirects to an url from a key -func (rctl RootController) Get(d i.Arguments) i.Response { +func (rctl RootController) Get(d api.Arguments) api.Response { - r := response.New() + r := api.NewResponse() /* (1) Init redis connection */ cli := db.Connect() @@ -27,11 +26,12 @@ func (rctl RootController) Get(d i.Arguments) i.Response { } /* (2) Extract api input */ - key, ok := d["url"].(string) + key, err := d.GetString("url") - if !ok { + if err != nil { r.Err = e.InvalidParam r.Err.Put("url") + r.Err.Put(err.Error()) return *r } @@ -49,9 +49,9 @@ func (rctl RootController) Get(d i.Arguments) i.Response { } // Stores a new tinyurl/fullurl combination -func (rctl RootController) Post(d i.Arguments) i.Response { +func (rctl RootController) Post(d api.Arguments) api.Response { - r := response.New() + r := api.NewResponse() /* (1) Init redis connection */ cli := db.Connect() if cli == nil { @@ -60,17 +60,27 @@ func (rctl RootController) Post(d i.Arguments) i.Response { } /* (2) Extract api input */ - target, ok1 := d["target"].(string) - url, ok2 := d["url"].(string) - - if !ok1 || !ok2 { + target, err := d.GetString("target") + if err != nil { r.Err = e.InvalidParam + r.Err.Put("target") + r.Err.Put(err.Error()) + return *r + } + + url, err := d.GetString("url") + + if err != nil { + r.Err = e.InvalidParam + r.Err.Put("url") + r.Err.Put(err.Error()) return *r } /* (3) Check if key already used */ if cli.Get(db.DATA, url) != nil { r.Err = e.AlreadyExists + r.Err.Put("url") return *r } @@ -85,9 +95,9 @@ func (rctl RootController) Post(d i.Arguments) i.Response { } // Overrides a existing tinyurl with new target -func (rctl RootController) Put(d i.Arguments) i.Response { +func (rctl RootController) Put(d api.Arguments) api.Response { - r := response.New() + r := api.NewResponse() /* (1) Init redis connection */ cli := db.Connect() @@ -97,11 +107,20 @@ func (rctl RootController) Put(d i.Arguments) i.Response { } /* (2) Extract api input */ - target, ok1 := d["target"].(string) - url, ok2 := d["url"].(string) - - if !ok1 || !ok2 { + target, err := d.GetString("target") + if err != nil { r.Err = e.InvalidParam + r.Err.Put("target") + r.Err.Put(err.Error()) + return *r + } + + url, err := d.GetString("url") + + if err != nil { + r.Err = e.InvalidParam + r.Err.Put("url") + r.Err.Put(err.Error()) return *r } @@ -122,9 +141,9 @@ func (rctl RootController) Put(d i.Arguments) i.Response { } // Deletes an existing tinyurl -func (rctl RootController) Delete(d i.Arguments) i.Response { +func (rctl RootController) Delete(d api.Arguments) api.Response { - r := response.New() + r := api.NewResponse() /* (1) Init redis connection */ cli := db.Connect() @@ -134,10 +153,12 @@ func (rctl RootController) Delete(d i.Arguments) i.Response { } /* (2) Extract api input */ - url, ok := d["url"].(string) + url, err := d.GetString("url") - if !ok { + if err != nil { r.Err = e.InvalidParam + r.Err.Put("url") + r.Err.Put(err.Error()) return *r } diff --git a/controller.plugin/token/main.go b/controller.plugin/token/main.go index 2f67d51..a05e3a3 100644 --- a/controller.plugin/token/main.go +++ b/controller.plugin/token/main.go @@ -4,10 +4,9 @@ import ( "crypto/sha512" "encoding/hex" "git.xdrm.io/example/aicra/db" + "git.xdrm.io/go/aicra/api" "git.xdrm.io/go/aicra/driver" e "git.xdrm.io/go/aicra/err" - "git.xdrm.io/go/aicra/response" - i "git.xdrm.io/go/aicra/response" "strconv" "time" ) @@ -19,9 +18,9 @@ type TokenController int func Export() driver.Controller { return new(TokenController) } // Builds an access token from credentials -func (tctl TokenController) Post(d i.Arguments) i.Response { +func (tctl TokenController) Post(d api.Arguments) api.Response { - r := response.New() + r := api.NewResponse() /* (1) Init redis connection */ cli := db.Connect() @@ -31,10 +30,11 @@ func (tctl TokenController) Post(d i.Arguments) i.Response { } /* (2) Extract api input */ - role, ok := d["role"].(string) - if !ok { + role, err := d.GetString("role") + if err != nil { r.Err = e.InvalidParam r.Err.Put("url") + r.Err.Put(err.Error()) return *r } @@ -55,6 +55,6 @@ func (tctl TokenController) Post(d i.Arguments) i.Response { return *r } -func (tctl TokenController) Get(d i.Arguments) i.Response { return *i.New() } -func (tctl TokenController) Put(d i.Arguments) i.Response { return *i.New() } -func (tctl TokenController) Delete(d i.Arguments) i.Response { return *i.New() } +func (tctl TokenController) Get(d api.Arguments) api.Response { return *api.NewResponse() } +func (tctl TokenController) Put(d api.Arguments) api.Response { return *api.NewResponse() } +func (tctl TokenController) Delete(d api.Arguments) api.Response { return *api.NewResponse() } diff --git a/type/any/main.go b/type/any/main.go deleted file mode 100644 index ae178ee..0000000 --- a/type/any/main.go +++ /dev/null @@ -1,20 +0,0 @@ -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 deleted file mode 100644 index d0c4c2b..0000000 --- a/type/int/main.go +++ /dev/null @@ -1,42 +0,0 @@ -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 deleted file mode 100644 index aadd55a..0000000 --- a/type/string/main.go +++ /dev/null @@ -1,27 +0,0 @@ -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 deleted file mode 100644 index b332fad..0000000 --- a/type/varchar/main.go +++ /dev/null @@ -1,89 +0,0 @@ -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 - -}