compliant to new 'aicra' config

This commit is contained in:
Adrien Marquès 2018-10-01 19:28:54 +02:00
parent c2c1835964
commit c813c5638b
10 changed files with 184 additions and 7 deletions

View File

@ -10,9 +10,9 @@
"folder": "type"
},
"controllers": {
"folder": "controller"
"folder": "controller.plugin"
},
"middlewares": {
"folder": "middleware"
"folder": "middleware.plugin"
}
}

View File

@ -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)
}

20
type/any/main.go Normal file
View File

@ -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
}

42
type/int/main.go Normal file
View File

@ -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
}

27
type/string/main.go Normal file
View File

@ -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
}

89
type/varchar/main.go Normal file
View File

@ -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
}