github.com/dgraph-io/dgraph@v1.2.8/types/compare.go (about)

     1  /*
     2   * Copyright 2017-2018 Dgraph Labs, Inc. and Contributors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package types
    18  
    19  import "github.com/dgraph-io/dgraph/x"
    20  
    21  // CompareVals compares two values using the given comparison type.
    22  // Should be used only in filtering arg1 by comparing with arg2.
    23  // arg2 is reference Val to which arg1 is compared.
    24  func CompareVals(op string, arg1, arg2 Val) bool {
    25  	negateRes := func(b bool, e error) (bool, error) { // reverses result
    26  		return !b, e
    27  	}
    28  	noError := func(b bool, e error) bool {
    29  		return b && e == nil
    30  	}
    31  	switch op {
    32  	case "ge":
    33  		return noError(negateRes(Less(arg1, arg2)))
    34  	case "gt":
    35  		return noError(Less(arg2, arg1))
    36  	case "le":
    37  		return noError(negateRes(Less(arg2, arg1)))
    38  	case "lt":
    39  		return noError(Less(arg1, arg2))
    40  	case "eq":
    41  		return noError(Equal(arg1, arg2))
    42  	default:
    43  		// should have been checked at query level.
    44  		x.Fatalf("Unknown ineqType %v", op)
    45  	}
    46  	return false
    47  }