-
Notifications
You must be signed in to change notification settings - Fork 23
/
extensions.go
64 lines (51 loc) · 1.47 KB
/
extensions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package extensions
import (
"context"
"io"
"github.com/alcionai/clues"
"github.com/alcionai/corso/src/pkg/backup/details"
"github.com/alcionai/corso/src/pkg/logger"
)
type CreateItemExtensioner interface {
CreateItemExtension(
context.Context,
io.ReadCloser,
details.ItemInfo,
*details.ExtensionData,
) (io.ReadCloser, error)
}
// AddItemExtensions wraps provided readcloser with extensions
// supplied via factory, with the first extension in slice being
// the innermost one. If no extensions are provided, the original
// readcloser is returned.
func AddItemExtensions(
ctx context.Context,
rc io.ReadCloser,
info details.ItemInfo,
factories []CreateItemExtensioner,
) (io.ReadCloser, *details.ExtensionData, error) {
if rc == nil {
return nil, nil, clues.New("nil readcloser")
}
// If no extensions were supplied, return the original readcloser
if len(factories) == 0 {
return rc, &details.ExtensionData{}, nil
}
ctx = clues.Add(ctx, "num_extensions", len(factories))
// Create extension data store to be passed to extensions
extData := &details.ExtensionData{
Data: make(map[string]any),
}
for _, factory := range factories {
if factory == nil {
return nil, nil, clues.New("nil extension factory")
}
extRc, err := factory.CreateItemExtension(ctx, rc, info, extData)
if err != nil {
return nil, nil, clues.Wrap(err, "create item extension")
}
rc = extRc
}
logger.Ctx(ctx).Debug("added item extensions")
return rc, extData, nil
}