@@ -2,6 +2,7 @@ package xlog
22
33import (
44 "context"
5+ "reflect"
56 "regexp"
67 "runtime"
78 "sync"
@@ -61,30 +62,9 @@ func EachPage(ctx context.Context, f func(Page)) {
6162
6263var concurrency = runtime .NumCPU () * 4
6364
64- // EachPageCon Similar to EachPage but iterates concurrently
65- func EachPageCon (ctx context.Context , f func (Page )) {
66- if pages == nil {
67- populatePagesCache (ctx )
68- }
69-
70- grp , ctx := errgroup .WithContext (ctx )
71- grp .SetLimit (concurrency )
72-
73- currentPages := pages
74- for _ , p := range currentPages {
75- select {
76- case <- ctx .Done ():
77- break
78- default :
79- grp .Go (func () (err error ) { f (p ); return })
80- }
81- }
82-
83- grp .Wait ()
84- }
85-
86- // MapPageCon Similar to EachPage but iterates concurrently
87- func MapPageCon [T any ](ctx context.Context , f func (Page ) T ) []T {
65+ // MapPage Similar to EachPage but iterates concurrently and accumulates
66+ // returns in a slice
67+ func MapPage [T any ](ctx context.Context , f func (Page ) T ) []T {
8868 if pages == nil {
8969 populatePagesCache (ctx )
9070 }
@@ -103,7 +83,7 @@ func MapPageCon[T any](ctx context.Context, f func(Page) T) []T {
10383 default :
10484 grp .Go (func () (err error ) {
10585 val := f (p )
106- if any (val ) == nil {
86+ if isNil (val ) {
10787 return
10888 }
10989
@@ -121,6 +101,20 @@ func MapPageCon[T any](ctx context.Context, f func(Page) T) []T {
121101 return output
122102}
123103
104+ // From https://stackoverflow.com/a/77341451/22401486
105+ func isNil [T any ](t T ) bool {
106+ v := reflect .ValueOf (t )
107+ kind := v .Kind ()
108+ // Must be one of these types to be nillable
109+ return (kind == reflect .Ptr ||
110+ kind == reflect .Interface ||
111+ kind == reflect .Slice ||
112+ kind == reflect .Map ||
113+ kind == reflect .Chan ||
114+ kind == reflect .Func ) &&
115+ v .IsNil ()
116+ }
117+
124118func clearPagesCache (p Page ) (err error ) {
125119 pages = nil
126120 return nil
0 commit comments