From f9a928c1817e8598fcb1abe76723b3ab96844f5e Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 2 Oct 2018 11:34:58 +0200 Subject: [PATCH] add 'digest(length)' type checker + minfix import in cmd/main --- cmd/aicra/main.go | 1 - internal/checker/default/digest/main.go | 73 +++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 internal/checker/default/digest/main.go diff --git a/cmd/aicra/main.go b/cmd/aicra/main.go index 32e8da5..02d42b6 100644 --- a/cmd/aicra/main.go +++ b/cmd/aicra/main.go @@ -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" diff --git a/internal/checker/default/digest/main.go b/internal/checker/default/digest/main.go new file mode 100644 index 0000000..14b6c45 --- /dev/null +++ b/internal/checker/default/digest/main.go @@ -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') +}