Repository: mr-linch/go-tg
Pull request #92 (2022-08-05 10:26:21)

feat(tgb): add Not filter
related #54 



codecov[bot]: # [Codecov](https://codecov.io/gh/mr-linch/go-tg/pull/92?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Sasha+Savchuk) Report
> Merging [#92](https://codecov.io/gh/mr-linch/go-tg/pull/92?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Sasha+Savchuk) (96aa77a) into [main](https://codecov.io/gh/mr-linch/go-tg/commit/e8afb583d9e44213095a9cff308ae2088e90d6fe?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Sasha+Savchuk) (e8afb58) will **increase** coverage by `0.02%`.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff             @@
##             main      #92      +/-   ##
==========================================
+ Coverage   91.72%   91.75%   +0.02%     
==========================================
  Files          17       17              
  Lines        2116     2123       +7     
==========================================
+ Hits         1941     1948       +7     
  Misses        142      142              
  Partials       33       33              
```


| [Impacted Files](https://codecov.io/gh/mr-linch/go-tg/pull/92?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Sasha+Savchuk) | Coverage Δ | |
|---|---|---|
| [tgb/filter.go](https://codecov.io/gh/mr-linch/go-tg/pull/92/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Sasha+Savchuk#diff-dGdiL2ZpbHRlci5nbw==) | `98.81% <100.00%> (+0.03%)` | :arrow_up: |

:mega: Codecov can now indicate which changes are the most critical in Pull Requests. [Learn more](https://about.codecov.io/product/feature/runtime-insights/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Sasha+Savchuk)


Patch:
diff --git a/tgb/filter.go b/tgb/filter.go
index 1fbcd07..89fc316 100644
--- a/tgb/filter.go
+++ b/tgb/filter.go
@@ -54,6 +54,17 @@ func All(filters ...Filter) Filter {
 	})
 }
 
+// Not pass update to handler, if specified filter does not allow it.
+func Not(filter Filter) Filter {
+	return FilterFunc(func(ctx context.Context, update *Update) (bool, error) {
+		allow, err := filter.Allow(ctx, update)
+		if err != nil {
+			return false, err
+		}
+		return !allow, nil
+	})
+}
+
 // commandFilter handles commands.
 // Filter is registered only for Message updates.
 // Custuming filter using WithCommand... options.


Test patch:
diff --git a/tgb/filter_test.go b/tgb/filter_test.go
index 4db283b..7022991 100644
--- a/tgb/filter_test.go
+++ b/tgb/filter_test.go
@@ -811,3 +811,28 @@ func TestMessageEntity(t *testing.T) {
 		})
 	}
 }
+
+func TestNot(t *testing.T) {
+	constFilter := func(v bool, err error) Filter {
+		return FilterFunc(func(ctx context.Context, update *Update) (bool, error) {
+			return v, err
+		})
+	}
+
+	trueFilter := constFilter(true, nil)
+
+	allow, err := Not(trueFilter).Allow(context.Background(), &Update{})
+	assert.False(t, allow)
+	assert.NoError(t, err)
+
+	falseFilter := constFilter(false, nil)
+
+	allow, err = Not(falseFilter).Allow(context.Background(), &Update{})
+	assert.True(t, allow)
+	assert.NoError(t, err)
+
+	errFilter := constFilter(true, errors.New("test"))
+	allow, err = Not(errFilter).Allow(context.Background(), &Update{})
+	assert.False(t, allow)
+	assert.Error(t, err)
+}