1package proptools
2
3import (
4	"errors"
5	"math"
6	"strings"
7)
8
9func ShardBySize[T ~[]E, E any](toShard T, shardSize int) []T {
10	if len(toShard) == 0 {
11		return nil
12	}
13
14	ret := make([]T, 0, (len(toShard)+shardSize-1)/shardSize)
15	for len(toShard) > shardSize {
16		ret = append(ret, toShard[0:shardSize])
17		toShard = toShard[shardSize:]
18	}
19	if len(toShard) > 0 {
20		ret = append(ret, toShard)
21	}
22	return ret
23}
24
25func ShardByCount[T ~[]E, E any](toShard T, shardCount int) []T {
26	return ShardBySize(toShard, int(math.Ceil(float64(len(toShard))/float64(shardCount))))
27}
28
29// MergeErrors merges a list of errors into a single error.
30func MergeErrors(errs []error) error {
31	if errs != nil {
32		var sb strings.Builder
33		for i, err := range errs {
34			if i != 0 {
35				sb.WriteString("\n")
36			}
37			sb.WriteString(err.Error())
38		}
39		return errors.New(sb.String())
40	}
41	return nil
42}
43