| 67 | } |
| 68 | |
| 69 | func (this *Rule) Init() error { |
| 70 | // operator |
| 71 | switch this.Operator { |
| 72 | case RuleOperatorGt: |
| 73 | this.floatValue = types.Float64(this.Value) |
| 74 | case RuleOperatorGte: |
| 75 | this.floatValue = types.Float64(this.Value) |
| 76 | case RuleOperatorLt: |
| 77 | this.floatValue = types.Float64(this.Value) |
| 78 | case RuleOperatorLte: |
| 79 | this.floatValue = types.Float64(this.Value) |
| 80 | case RuleOperatorEq: |
| 81 | this.floatValue = types.Float64(this.Value) |
| 82 | case RuleOperatorNeq: |
| 83 | this.floatValue = types.Float64(this.Value) |
| 84 | case RuleOperatorContainsAny, RuleOperatorContainsAll, RuleOperatorContainsAnyWord, RuleOperatorContainsAllWords, RuleOperatorNotContainsAnyWord: |
| 85 | this.stringValues = []string{} |
| 86 | if len(this.Value) > 0 { |
| 87 | var lines = strings.Split(this.Value, "\n") |
| 88 | for _, line := range lines { |
| 89 | line = strings.TrimSpace(line) |
| 90 | if len(line) > 0 { |
| 91 | if this.IsCaseInsensitive { |
| 92 | this.stringValues = append(this.stringValues, strings.ToLower(line)) |
| 93 | } else { |
| 94 | this.stringValues = append(this.stringValues, line) |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | if this.Operator == RuleOperatorContainsAnyWord || this.Operator == RuleOperatorContainsAllWords || this.Operator == RuleOperatorNotContainsAnyWord { |
| 99 | sort.Strings(this.stringValues) |
| 100 | } |
| 101 | |
| 102 | this.stringValueRunes = [][]rune{} |
| 103 | for _, line := range this.stringValues { |
| 104 | this.stringValueRunes = append(this.stringValueRunes, []rune(line)) |
| 105 | } |
| 106 | } |
| 107 | case RuleOperatorMatch: |
| 108 | var v = this.Value |
| 109 | if this.IsCaseInsensitive && !strings.HasPrefix(v, "(?i)") { |
| 110 | v = "(?i)" + v |
| 111 | } |
| 112 | |
| 113 | v = this.unescape(v) |
| 114 | |
| 115 | reg, err := re.Compile(v) |
| 116 | if err != nil { |
| 117 | return err |
| 118 | } |
| 119 | this.reg = reg |
| 120 | case RuleOperatorNotMatch: |
| 121 | var v = this.Value |
| 122 | if this.IsCaseInsensitive && !strings.HasPrefix(v, "(?i)") { |
| 123 | v = "(?i)" + v |
| 124 | } |
| 125 | |
| 126 | v = this.unescape(v) |