Repository: ogen-go/ogen
Pull request #639 (2022-10-21 11:06:45)

refactor(xslices): pass the filtering slice by reference

codecov[bot]: # [Codecov](https://codecov.io/gh/ogen-go/ogen/pull/639?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ogen-go) Report
Base: **73.82**% // Head: **73.81**% // Decreases project coverage by **`-0.01%`** :warning:
> Coverage data is based on head [(`6fdad41`)](https://codecov.io/gh/ogen-go/ogen/pull/639?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ogen-go) compared to base [(`558accf`)](https://codecov.io/gh/ogen-go/ogen/commit/558accf3ac7cc4dc5ca4dce1d4d29a3378b3f39f?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ogen-go).
> Patch coverage: 100.00% of modified lines in pull request are covered.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main     #639      +/-   ##
==========================================
- Coverage   73.82%   73.81%   -0.02%     
==========================================
  Files         169      169              
  Lines       11751    11754       +3     
==========================================
+ Hits         8675     8676       +1     
- Misses       2628     2629       +1     
- Partials      448      449       +1     
```


| [Impacted Files](https://codecov.io/gh/ogen-go/ogen/pull/639?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ogen-go) | Coverage Δ | |
|---|---|---|
| [gen/generator.go](https://codecov.io/gh/ogen-go/ogen/pull/639/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ogen-go#diff-Z2VuL2dlbmVyYXRvci5nbw==) | `57.05% <100.00%> (ø)` | |
| [gen/schema\_gen\_sum.go](https://codecov.io/gh/ogen-go/ogen/pull/639/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ogen-go#diff-Z2VuL3NjaGVtYV9nZW5fc3VtLmdv) | `75.14% <100.00%> (ø)` | |
| [internal/xslices/xslices.go](https://codecov.io/gh/ogen-go/ogen/pull/639/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ogen-go#diff-aW50ZXJuYWwveHNsaWNlcy94c2xpY2VzLmdv) | `100.00% <100.00%> (ø)` | |
| [jsonschema/parser\_enum.go](https://codecov.io/gh/ogen-go/ogen/pull/639/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ogen-go#diff-anNvbnNjaGVtYS9wYXJzZXJfZW51bS5nbw==) | `50.00% <100.00%> (ø)` | |
| [gen/gen\_contents.go](https://codecov.io/gh/ogen-go/ogen/pull/639/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ogen-go#diff-Z2VuL2dlbl9jb250ZW50cy5nbw==) | `72.20% <0.00%> (-0.68%)` | :arrow_down: |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ogen-go). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ogen-go)

</details>

[:umbrella: View full report at Codecov](https://codecov.io/gh/ogen-go/ogen/pull/639?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ogen-go).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ogen-go).


Patch:
diff --git a/gen/generator.go b/gen/generator.go
index b2d75ad6..19676eed 100644
--- a/gen/generator.go
+++ b/gen/generator.go
@@ -174,7 +174,7 @@ func (g *Generator) makeWebhooks(webhooks []openapi.Webhook) error {
 				continue
 			}
 
-			spec.Parameters = xslices.Filter(spec.Parameters, func(p *openapi.Parameter) bool {
+			xslices.Filter(&spec.Parameters, func(p *openapi.Parameter) bool {
 				if p.In.Path() {
 					log.Warn("Webhooks can't have path parameters",
 						zap.String("name", p.Name),
diff --git a/gen/schema_gen_sum.go b/gen/schema_gen_sum.go
index d12a2842..fe267d86 100644
--- a/gen/schema_gen_sum.go
+++ b/gen/schema_gen_sum.go
@@ -235,7 +235,7 @@ func (g *schemaGen) oneOf(name string, schema *jsonschema.Schema) (*ir.Type, err
 					})
 
 					// Filter discriminator field in-place.
-					s.Fields = xslices.Filter(s.Fields, func(f *ir.Field) bool {
+					xslices.Filter(&s.Fields, func(f *ir.Field) bool {
 						return f.Tag.JSON != propName
 					})
 				}
diff --git a/internal/xslices/xslices.go b/internal/xslices/xslices.go
index 23ca0f3c..c2f929ee 100644
--- a/internal/xslices/xslices.go
+++ b/internal/xslices/xslices.go
@@ -4,15 +4,18 @@ package xslices
 import "golang.org/x/exp/slices"
 
 // Filter performs in-place filtering of a slice.
-func Filter[S ~[]E, E any](s S, keep func(E) bool) S {
-	var n int
+func Filter[S ~[]E, E any](sptr *S, keep func(E) bool) {
+	var (
+		n int
+		s = *sptr
+	)
 	for _, v := range s {
 		if keep(v) {
 			s[n] = v
 			n++
 		}
 	}
-	return s[:n]
+	*sptr = s[:n]
 }
 
 // ContainsFunc returns true if the slice contains an element satisfying the predicate.
diff --git a/jsonschema/parser_enum.go b/jsonschema/parser_enum.go
index 719b5d47..719bec4c 100644
--- a/jsonschema/parser_enum.go
+++ b/jsonschema/parser_enum.go
@@ -117,7 +117,7 @@ func handleNullableEnum(s *Schema) {
 	})
 	// Filter all `null`s.
 	if s.Nullable {
-		s.Enum = xslices.Filter(s.Enum, func(v any) bool {
+		xslices.Filter(&s.Enum, func(v any) bool {
 			return v != nil
 		})
 	}


Test patch:
diff --git a/internal/xslices/xslices_test.go b/internal/xslices/xslices_test.go
index 036af426..57311c1b 100644
--- a/internal/xslices/xslices_test.go
+++ b/internal/xslices/xslices_test.go
@@ -13,9 +13,14 @@ func TestFilter(t *testing.T) {
 	odd := func(x int) bool {
 		return x%2 == 1
 	}
-	a.Empty(Filter([]int(nil), odd))
-	a.Empty(Filter([]int{}, odd))
-	a.Equal(Filter([]int{1, 2, 3, 4, 5}, odd), []int{1, 3, 5})
+	filter := func(v []int, cb func(int) bool) []int {
+		Filter(&v, cb)
+		return v
+	}
+
+	a.Empty(filter([]int(nil), odd))
+	a.Empty(filter([]int{}, odd))
+	a.Equal(filter([]int{1, 2, 3, 4, 5}, odd), []int{1, 3, 5})
 }
 
 func TestContainsFunc(t *testing.T) {