From 320405a6c3ed362aea15116e8f5509454419246e Mon Sep 17 00:00:00 2001 From: Jan-Matthias Reinke Date: Wed, 30 May 2018 17:26:45 +0200 Subject: [PATCH] add XGtIsOr rule --- README.md | 1 + util.go | 4 ++++ xgtisor.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ xgtisor_test.go | 38 ++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 xgtisor.go create mode 100644 xgtisor_test.go diff --git a/README.md b/README.md index 81c84e0..86d5f20 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,7 @@ if err != nil { - `XAnd(arg interface{})` Required when arg is neither nil nor empty - `XAndOr(arg interface{}, args ...interface{})` Required when arg is neither nil nor empty and all args are nil or empty - `XGt(arg1 interface{}, arg2 int)` Required when arg1 is greater than arg2 +- `XGtOr(arg1 interface{}, arg2 int, arg3 interface{}, arg4 interface{}, args ...interface{})` Required when arg1 is greater than arg2, arg3 is equal to arg4 and all args are nil or empty - `XGtOr(arg1 interface{}, arg2 int, args ...interface{})` Required when arg1 is greater than arg2 and all args are nil or empty - `XLt(arg1 interface{}, arg2 int)` Required when arg1 is lower than arg2 - `XLtOr(arg1 interface{}, arg2 int, args ...interface{})` Required when arg1 is lower than arg2 and all args are nil or empty diff --git a/util.go b/util.go index 02d4c15..c4652b2 100644 --- a/util.go +++ b/util.go @@ -135,3 +135,7 @@ func LengthOf(value interface{}) (int, error) { } return 0, fmt.Errorf("Could not determine length of argument type %v", v.Kind()) } + +func DeepEqual(s interface{}, c interface{}) bool { + return reflect.DeepEqual(s, c) +} diff --git a/xgtisor.go b/xgtisor.go new file mode 100644 index 0000000..61e2d5b --- /dev/null +++ b/xgtisor.go @@ -0,0 +1,55 @@ +package orwell + +import ( + "fmt" +) + +// XGtIsOr func +func (*Orwell) XGtIsOr(gtField interface{}, gtValue int, isField interface{}, isValue interface{}, ors ...interface{}) *xGtIsOr { + return &xGtIsOr{ + gtField: gtField, + gtValue: gtValue, + isField: isField, + isValue: isValue, + ors: ors, + msg: fmt.Sprintf("Validation error for 'XGtOr' rule"), + } +} + +// xGtOr struct +type xGtIsOr struct { + gtField interface{} + gtValue int + isField interface{} + isValue interface{} + ors []interface{} + msg string +} + +// Apply rule +func (r *xGtIsOr) Apply(value interface{}) error { + if !NOE(value) { + return nil + } + + gtField, isNil := IsNil(r.gtField) + if isNil || IsEmpty(gtField) { + return nil + } + + gtFieldInt64, err := ToInt64(gtField) + if err != nil { + return fmt.Errorf("%s: %s", r.msg, err.Error()) + } + + if gtFieldInt64 > int64(r.gtValue) && DeepEqual(r.isField, r.isValue) { + for _, or := range r.ors { + if !NOE(or) { + return nil + } + } + return fmt.Errorf(r.msg) + } + + return nil +} diff --git a/xgtisor_test.go b/xgtisor_test.go new file mode 100644 index 0000000..50fe444 --- /dev/null +++ b/xgtisor_test.go @@ -0,0 +1,38 @@ +package orwell + +import ( + "testing" +) + +func TestApplyXGtIsOr(t *testing.T) { + t.Run("gtField > gtValue, isField == isValue, ors nil, value not nil", func(t *testing.T) { + r := &xGtIsOr{gtField: 10, gtValue: 9, isField: "test", isValue: "test", ors: nil} + if err := r.Apply("not nil"); err != nil { + t.Error("Expected valid") + } + }) + t.Run("gtField > gtValue, isField == isValue, ors nil, value not nil", func(t *testing.T) { + r := &xGtIsOr{gtField: 10, gtValue: 9, isField: "test", isValue: "test", ors: nil} + if err := r.Apply(nil); err == nil { + t.Error("Expected error") + } + }) + t.Run("gtField > gtValue, isField == isValue, ors nil, value not nil", func(t *testing.T) { + r := &xGtIsOr{gtField: 10, gtValue: 10, isField: 0, isValue: 0, ors: nil} + if err := r.Apply(nil); err != nil { + t.Error("Expected valid because gtField is not greater than igtValue") + } + }) + t.Run("gtField > gtValue, isField == isValue, ors nil, value not nil", func(t *testing.T) { + r := &xGtIsOr{gtField: 10, gtValue: 9, isField: 0, isValue: 1, ors: nil} + if err := r.Apply(nil); err != nil { + t.Error("Expected valid because isField and isValue are not equal") + } + }) + t.Run("gtField > gtValue, isField == isValue, ors nil, value not nil", func(t *testing.T) { + r := &xGtIsOr{gtField: 10, gtValue: 9, isField: 1, isValue: 1, ors: []interface{}{"test"}} + if err := r.Apply(nil); err != nil { + t.Error("Expected valid because ors is not empty") + } + }) +}