Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IP and IPNET support #101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions TokenKind.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const (

PREFIX
NUMERIC
IP
IPNET
BOOLEAN
STRING
PATTERN
Expand Down Expand Up @@ -41,6 +43,10 @@ func (kind TokenKind) String() string {
return "PREFIX"
case NUMERIC:
return "NUMERIC"
case IP:
return "IP"
case IPNET:
return "IPNET"
case BOOLEAN:
return "BOOLEAN"
case STRING:
Expand Down
48 changes: 46 additions & 2 deletions dummies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package govaluate
import (
"errors"
"fmt"
"net"
)

/*
Expand All @@ -14,6 +15,10 @@ type dummyParameter struct {
BoolFalse bool
Nil interface{}
Nested dummyNestedParameter
IP1 net.IP
IP2 net.IP
CIDR1 net.IPNet
CIDR2 net.IPNet
}

func (this dummyParameter) Func() string {
Expand All @@ -33,9 +38,9 @@ func (this dummyParameter) FuncArgStr(arg1 string) string {
}

func (this dummyParameter) TestArgs(str string, ui uint, ui8 uint8, ui16 uint16, ui32 uint32, ui64 uint64, i int, i8 int8, i16 int16, i32 int32, i64 int64, f32 float32, f64 float64, b bool) string {

var sum float64

sum = float64(ui) + float64(ui8) + float64(ui16) + float64(ui32) + float64(ui64)
sum += float64(i) + float64(i8) + float64(i16) + float64(i32) + float64(i64)
sum += float64(f32)
Expand Down Expand Up @@ -67,13 +72,27 @@ var dummyParameterInstance = dummyParameter{
Nested: dummyNestedParameter{
Funk: "funkalicious",
},
IP1: net.ParseIP("127.0.0.1"),
IP2: net.ParseIP("127.0.0.3"),
CIDR1: mustParseCIDR("127.0.0.4/22"),
CIDR2: mustParseCIDR("27.0.0.0/12"),
}

var fooParameter = EvaluationParameter{
Name: "foo",
Value: dummyParameterInstance,
}

var fooParameterEmptyIP = EvaluationParameter{
Name: "foo",
Value: dummyParameter{
IP1: nil,
IP2: nil,
CIDR1: mustParseCIDR("127.0.0.4/22"),
CIDR2: mustParseCIDR("27.0.0.0/12"),
},
}

var fooPtrParameter = EvaluationParameter{
Name: "fooptr",
Value: &dummyParameterInstance,
Expand All @@ -83,3 +102,28 @@ var fooFailureParameters = map[string]interface{}{
"foo": fooParameter.Value,
"fooptr": &fooPtrParameter.Value,
}

func mustParseCIDR(cidr string) net.IPNet {
_, ipNet, err := net.ParseCIDR(cidr)
if err != nil {
panic(err)
}
return *ipNet

}

var CIDRTestFunction = map[string]ExpressionFunction{
"InNetwork": func(args ...interface{}) (interface{}, error) {
ip, ok1 := args[0].(net.IP)
ipNet, ok2 := args[1].(net.IPNet)

if !ok1 {
return nil, fmt.Errorf("variable %s not ip", args[0])
}
if !ok2 {
return nil, fmt.Errorf("variable %s not IPnet its %T", args[1], args[1])
}

return ipNet.Contains(ip), nil
},
}
93 changes: 93 additions & 0 deletions evaluationFailure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package govaluate
import (
"errors"
"fmt"
"net"
"strings"
"testing"
)
Expand Down Expand Up @@ -43,6 +44,8 @@ var EVALUATION_FAILURE_PARAMETERS = map[string]interface{}{
"number": 1,
"string": "foo",
"bool": true,
"ip": net.ParseIP("127.0.0.1"),
"ipnet": mustParseCIDR("127.0.0.1/12"),
}

func TestComplexParameter(test *testing.T) {
Expand Down Expand Up @@ -185,6 +188,42 @@ func TestModifierTyping(test *testing.T) {
Input: "number >> bool",
Expected: INVALID_MODIFIER_TYPES,
},
EvaluationFailureTest{

Name: "BITWISE_RSHIFT bool to IP",
Input: "bool >> ip",
Expected: INVALID_MODIFIER_TYPES,
},
EvaluationFailureTest{

Name: "BITWISE_OR string to ip",
Input: "string | ip",
Expected: INVALID_MODIFIER_TYPES,
},
EvaluationFailureTest{

Name: "BITWISE_RSHIFT bool to ipnet",
Input: "bool >> ipnet",
Expected: INVALID_MODIFIER_TYPES,
},
EvaluationFailureTest{

Name: "BITWISE_OR string to ipnet",
Input: "string | ipnet",
Expected: INVALID_MODIFIER_TYPES,
},
EvaluationFailureTest{

Name: "BITWISE_RSHIFT number to ipnet",
Input: "number >> ipnet",
Expected: INVALID_MODIFIER_TYPES,
},
EvaluationFailureTest{

Name: "BITWISE_OR number to ipnet",
Input: "number | ipnet",
Expected: INVALID_MODIFIER_TYPES,
},
}

runEvaluationFailureTests(evaluationTests, test)
Expand Down Expand Up @@ -241,6 +280,42 @@ func TestLogicalOperatorTyping(test *testing.T) {
Input: "string || bool",
Expected: INVALID_LOGICALOP_TYPES,
},
EvaluationFailureTest{

Name: "AND bool to IP",
Input: "bool && ip",
Expected: INVALID_LOGICALOP_TYPES,
},
EvaluationFailureTest{

Name: "OR string to ip",
Input: "string || ip",
Expected: INVALID_LOGICALOP_TYPES,
},
EvaluationFailureTest{

Name: "AND bool to ipnet",
Input: "bool && ipnet",
Expected: INVALID_LOGICALOP_TYPES,
},
EvaluationFailureTest{

Name: "OR string to ipnet",
Input: "string || ipnet",
Expected: INVALID_LOGICALOP_TYPES,
},
EvaluationFailureTest{

Name: "AND number to ipnet",
Input: "number && ipnet",
Expected: INVALID_LOGICALOP_TYPES,
},
EvaluationFailureTest{

Name: "OR number to ipnet",
Input: "number || ipnet",
Expected: INVALID_LOGICALOP_TYPES,
},
}

runEvaluationFailureTests(evaluationTests, test)
Expand Down Expand Up @@ -368,6 +443,18 @@ func TestComparatorTyping(test *testing.T) {
Input: "1 in true",
Expected: INVALID_COMPARATOR_TYPES,
},
EvaluationFailureTest{

Name: "NREQ bool to ipnet",
Input: "bool !~ ipnet",
Expected: INVALID_COMPARATOR_TYPES,
},
EvaluationFailureTest{

Name: "IN string to ipnet",
Input: "string in ipnet",
Expected: INVALID_COMPARATOR_TYPES,
},
}

runEvaluationFailureTests(evaluationTests, test)
Expand All @@ -388,6 +475,12 @@ func TestTernaryTyping(test *testing.T) {
Input: "'foo' ? true",
Expected: INVALID_TERNARY_TYPES,
},
EvaluationFailureTest{

Name: "Ternary with ip",
Input: "'foo' ? ip",
Expected: INVALID_TERNARY_TYPES,
},
}

runEvaluationFailureTests(evaluationTests, test)
Expand Down
25 changes: 24 additions & 1 deletion evaluationStage.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package govaluate

import (
"bytes"
"errors"
"fmt"
"math"
"net"
"reflect"
"regexp"
"strings"
Expand Down Expand Up @@ -114,24 +116,36 @@ func gteStage(left interface{}, right interface{}, parameters Parameters) (inter
if isString(left) && isString(right) {
return boolIface(left.(string) >= right.(string)), nil
}
if isIp(left) && isIp(right) {
return boolIface(bytes.Compare(left.(net.IP).To4(), right.(net.IP).To4()) >= 0), nil
}
return boolIface(left.(float64) >= right.(float64)), nil
}
func gtStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
if isString(left) && isString(right) {
return boolIface(left.(string) > right.(string)), nil
}
if isIp(left) && isIp(right) {
return boolIface(bytes.Compare(left.(net.IP).To4(), right.(net.IP).To4()) > 0), nil
}
return boolIface(left.(float64) > right.(float64)), nil
}
func lteStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
if isString(left) && isString(right) {
return boolIface(left.(string) <= right.(string)), nil
}
if isIp(left) && isIp(right) {
return boolIface(bytes.Compare(left.(net.IP).To4(), right.(net.IP).To4()) <= 0), nil
}
return boolIface(left.(float64) <= right.(float64)), nil
}
func ltStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
if isString(left) && isString(right) {
return boolIface(left.(string) < right.(string)), nil
}
if isIp(left) && isIp(right) {
return boolIface(bytes.Compare(left.(net.IP).To4(), right.(net.IP).To4()) == 0), nil
}
return boolIface(left.(float64) < right.(float64)), nil
}
func equalStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
Expand Down Expand Up @@ -421,7 +435,7 @@ func separatorStage(left interface{}, right interface{}, parameters Parameters)
func inStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {

for _, value := range right.([]interface{}) {
if left == value {
if reflect.DeepEqual(left, value) {
return true, nil
}
}
Expand All @@ -439,6 +453,11 @@ func isString(value interface{}) bool {
return false
}

func isIp(value interface{}) bool {
_, ok := value.(net.IP)
return ok
}

func isRegexOrString(value interface{}) bool {

switch value.(type) {
Expand Down Expand Up @@ -493,6 +512,10 @@ func comparatorTypeCheck(left interface{}, right interface{}) bool {
if isString(left) && isString(right) {
return true
}
if isIp(left) && isIp(right) {
return true
}

return false
}

Expand Down
Loading