add 'digest(length)' type checker + minfix import in cmd/main

This commit is contained in:
Adrien Marquès 2018-10-02 11:34:58 +02:00
parent b1092437f9
commit f9a928c181
2 changed files with 73 additions and 1 deletions

View File

@ -2,7 +2,6 @@ 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"

View File

@ -0,0 +1,73 @@
package main
import (
"git.xdrm.io/go/aicra/driver"
"strconv"
"strings"
)
func main() {}
func Export() driver.Checker { return new(DigestChecker) }
type DigestChecker struct {
Length *uint64
}
// Match filters the parameter type format "varchar(min, max)"
func (dck *DigestChecker) Match(name string) bool {
dck.Length = nil
/* (1) Check prefix/suffix */
if len(name) < len("digest(x)") || !strings.HasPrefix(name, "digest(") || name[len(name)-1] != ')' {
return false
}
/* (2) Extract length */
lengthStr := name[len("digest(") : len(name)-1]
length, err := strconv.ParseUint(lengthStr, 10, 64)
if err != nil {
return false
}
dck.Length = &length
return true
}
// Check whether the given value fulfills the condition (min, max)
func (dck *DigestChecker) Check(value interface{}) bool {
/* (1) Check if sizes set */
if dck.Length == nil {
return false
}
/* (2) Check if string */
strval, ok := value.(string)
if !ok {
return false
}
length := uint64(len(strval))
/* (3) Check length */
if length != *dck.Length {
return false
}
/* (4) Check character set */
for _, char := range strval {
if !isValidCharacter(char) {
return false
}
}
return true
}
func isValidCharacter(char rune) bool {
return (char >= '0' && char <= '9') || (char >= 'a' && char <= 'f')
}