master
go 77 lines 1.9 KB
Raw
1 package options
2
3 type ObjectAddLinkSettings struct {
4 Create bool
5 SkipUnixFSValidation bool
6 }
7
8 type (
9 ObjectAddLinkOption func(*ObjectAddLinkSettings) error
10 )
11
12 func ObjectAddLinkOptions(opts ...ObjectAddLinkOption) (*ObjectAddLinkSettings, error) {
13 options := &ObjectAddLinkSettings{
14 Create: false,
15 }
16
17 for _, opt := range opts {
18 err := opt(options)
19 if err != nil {
20 return nil, err
21 }
22 }
23 return options, nil
24 }
25
26 type objectOpts struct{}
27
28 var Object objectOpts
29
30 // Create is an option for Object.AddLink which specifies whether create required
31 // directories for the child
32 func (objectOpts) Create(create bool) ObjectAddLinkOption {
33 return func(settings *ObjectAddLinkSettings) error {
34 settings.Create = create
35 return nil
36 }
37 }
38
39 // SkipUnixFSValidation is an option for Object.AddLink which skips the check
40 // that only allows adding named links to UnixFS directory nodes.
41 // Use this when operating on raw dag-pb nodes outside of UnixFS semantics.
42 func (objectOpts) SkipUnixFSValidation(skip bool) ObjectAddLinkOption {
43 return func(settings *ObjectAddLinkSettings) error {
44 settings.SkipUnixFSValidation = skip
45 return nil
46 }
47 }
48
49 type ObjectRmLinkSettings struct {
50 SkipUnixFSValidation bool
51 }
52
53 type (
54 ObjectRmLinkOption func(*ObjectRmLinkSettings) error
55 )
56
57 func ObjectRmLinkOptions(opts ...ObjectRmLinkOption) (*ObjectRmLinkSettings, error) {
58 options := &ObjectRmLinkSettings{}
59
60 for _, opt := range opts {
61 err := opt(options)
62 if err != nil {
63 return nil, err
64 }
65 }
66 return options, nil
67 }
68
69 // RmLinkSkipUnixFSValidation is an option for Object.RmLink which skips the
70 // check that only allows removing links from UnixFS directory nodes.
71 // Use this when operating on raw dag-pb nodes outside of UnixFS semantics.
72 func (objectOpts) RmLinkSkipUnixFSValidation(skip bool) ObjectRmLinkOption {
73 return func(settings *ObjectRmLinkSettings) error {
74 settings.SkipUnixFSValidation = skip
75 return nil
76 }
77 }