Categorygithub.com/nytimes/go-compare-expressions
modulepackage
1.1.2
Repository: https://github.com/nytimes/go-compare-expressions.git
Documentation: pkg.go.dev

# README

go-compare-expressions

Provides support for comparing boolean/logical expressions. You can use this package to check if two given expressions are duplicate/equivalent

Description

Logically two expressions P and Q are considered duplicate/equivalent, if

  • when P is true, Q is true as well and when P is false, Q is false as well and vice versa

This is done by generating truth tables for the given expression and comparing the truth tables. For generating truth tables, we evaluate the expressions using govaluate. If both the generated truth tables are equal, we concur that given expressions are equal as well.

Installation

Download this package into your golang project.

go get github.com/nytimes/go-compare-expressions

Usage

To compare any two boolean/logical expression, call CheckIfDuplicateExpressions method by passing the two expression that you want to compare.

   expr1 := "(foo == 1 && bar == 1) || baz = 0 " 
   expr2 := "baz == 1 || bar == 1 && foo == 1"
   result, err := CheckIfDuplicateExpressions(expr1,expr2)

Test

go test .

Examples

Examples can be found here

Operations supported:

  • Comparators: ==
  • Logical operators: && ||
  • Only Binary values allowed: 0 1
  • Parenthesis can be used to define precedence of operations ()

License

This project is licensed under the MIT general use license.

# Packages

No description provided by the author

# Functions

* This is the main function that is to be called to verify if 2 expressions are duplicate or not.
* This function uses govaluate library to evaluate the provided boolean expression */.
* This function filters the duplicates from given array of string */.
* This function generates the truth table for the given expression and set of parameters present in the expression.
* This function checks whether all strings present in params2 is contained in params1 */.
* This function validates the given input expression.
* This function is to validate the input in the following way.