bitbucket.org/lygo/lygo_ext_dbbolt@v0.1.3/lygo_db_bolt_query.go (about)

     1  package lygo_ext_dbbolt
     2  
     3  import (
     4  	"bitbucket.org/lygo/lygo_commons/lygo_conv"
     5  	"bitbucket.org/lygo/lygo_commons/lygo_io"
     6  	"bitbucket.org/lygo/lygo_commons/lygo_reflect"
     7  	"bitbucket.org/lygo/lygo_commons/lygo_strings"
     8  	"encoding/json"
     9  	"strings"
    10  )
    11  
    12  const (
    13  	ComparatorEqual        = "=="
    14  	ComparatorNotEqual     = "!="
    15  	ComparatorGreater      = ">"
    16  	ComparatorLower        = "<"
    17  	ComparatorLowerEqual   = "<="
    18  	ComparatorGreaterEqual = ">="
    19  
    20  	OperatorAnd = "&&"
    21  	OperatorOr  = "||"
    22  )
    23  
    24  //----------------------------------------------------------------------------------------------------------------------
    25  //	t y p e
    26  //----------------------------------------------------------------------------------------------------------------------
    27  
    28  type BoltQuery struct {
    29  	Conditions []*BoltQueryConditionGroup `json:"conditions"`
    30  }
    31  
    32  type BoltQueryConditionGroup struct {
    33  	Operator string                `json:"operator"`
    34  	Filters  []*BoltQueryCondition `json:"filters"`
    35  }
    36  
    37  type BoltQueryCondition struct {
    38  	Field      interface{} `json:"field"`      // absolute value or field "doc.name"
    39  	Comparator string      `json:"comparator"` // ==, !=, > ...
    40  	Value      interface{} `json:"value"`      // absolute value or field "doc.surname", "Rossi"
    41  }
    42  
    43  //----------------------------------------------------------------------------------------------------------------------
    44  //	BoltQuery
    45  //----------------------------------------------------------------------------------------------------------------------
    46  
    47  func NewQueryFromFile(path string) (*BoltQuery, error) {
    48  	query := new(BoltQuery)
    49  	text, err := lygo_io.ReadTextFromFile(path)
    50  	if nil != err {
    51  		return nil, err
    52  	}
    53  	err = query.Parse(text)
    54  	if nil != err {
    55  		return nil, err
    56  	}
    57  	return query, nil
    58  }
    59  
    60  func (instance *BoltQuery) Parse(text string) error {
    61  	return json.Unmarshal([]byte(text), &instance)
    62  }
    63  
    64  func (instance *BoltQuery) ToString() string {
    65  	b, err := json.Marshal(&instance)
    66  	if nil == err {
    67  		return string(b)
    68  	}
    69  	return ""
    70  }
    71  
    72  func (instance *BoltQuery) MatchFilter(entity interface{}) bool {
    73  	response := false
    74  	if nil != instance {
    75  		if nil != instance.Conditions && nil != entity {
    76  			conditions := instance.Conditions
    77  			if len(conditions) > 0 {
    78  				// OPTIMISTIC CONDITION
    79  				response = true
    80  				for _, condition := range conditions {
    81  					if nil != condition {
    82  						operator := condition.Operator
    83  						filters := condition.Filters
    84  						for _, filter := range filters {
    85  							f1 := getValue(entity, filter.Field)
    86  							f2 := getValue(entity, filter.Value)
    87  							cp := filter.Comparator
    88  
    89  							match := false
    90  							switch cp {
    91  							case ComparatorEqual:
    92  								match = lygo_conv.Equals(f1, f2)
    93  							case ComparatorNotEqual:
    94  								match = lygo_conv.NotEquals(f1, f2)
    95  							default:
    96  								match = false
    97  							}
    98  
    99  							switch operator {
   100  							case OperatorAnd:
   101  								// AND
   102  								response = response && match
   103  							case OperatorOr:
   104  								// OR
   105  								response = response || match
   106  							default:
   107  								// invalid operator
   108  								response = false
   109  							}
   110  							if !response {
   111  								break
   112  							}
   113  						} // filters
   114  						if !response {
   115  							break
   116  						}
   117  					}
   118  				} // conditions
   119  			}
   120  		}
   121  
   122  	}
   123  	return response
   124  }
   125  
   126  //----------------------------------------------------------------------------------------------------------------------
   127  //	p r i v a t e
   128  //----------------------------------------------------------------------------------------------------------------------
   129  
   130  func getValue(entity interface{}, propertyOrValue interface{}) interface{} {
   131  	if b, property := lygo_conv.IsString(propertyOrValue); b {
   132  		if strings.Index(property, ".") > -1 {
   133  			// found a property
   134  			field := ""
   135  			tokens := lygo_strings.Split(property, ".")
   136  			switch len(tokens) {
   137  			case 1:
   138  				field = tokens[0]
   139  			default:
   140  				field = tokens[1]
   141  			}
   142  			if len(field) > 0 {
   143  				r := lygo_reflect.Get(entity, field)
   144  				if nil!=r{
   145  					return r
   146  				}
   147  				return lygo_reflect.Get(entity, lygo_strings.CapitalizeAll(field))
   148  			}
   149  		}
   150  	}
   151  
   152  	return propertyOrValue
   153  }