object.go 2.65 KB
Newer Older
Łukasz Magiera's avatar
Łukasz Magiera committed
1 2 3 4 5 6
package options

type ObjectNewSettings struct {
	Type string
}

7 8
type ObjectPutSettings struct {
	InputEnc string
Łukasz Magiera's avatar
Łukasz Magiera committed
9
	DataType string
10
	Pin      bool
11 12
}

Łukasz Magiera's avatar
Łukasz Magiera committed
13 14 15 16 17
type ObjectAddLinkSettings struct {
	Create bool
}

type ObjectNewOption func(*ObjectNewSettings) error
18
type ObjectPutOption func(*ObjectPutSettings) error
Łukasz Magiera's avatar
Łukasz Magiera committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
type ObjectAddLinkOption func(*ObjectAddLinkSettings) error

func ObjectNewOptions(opts ...ObjectNewOption) (*ObjectNewSettings, error) {
	options := &ObjectNewSettings{
		Type: "empty",
	}

	for _, opt := range opts {
		err := opt(options)
		if err != nil {
			return nil, err
		}
	}
	return options, nil
}

35 36 37
func ObjectPutOptions(opts ...ObjectPutOption) (*ObjectPutSettings, error) {
	options := &ObjectPutSettings{
		InputEnc: "json",
Łukasz Magiera's avatar
Łukasz Magiera committed
38
		DataType: "text",
39
		Pin:      false,
40 41 42 43 44 45 46 47 48 49 50
	}

	for _, opt := range opts {
		err := opt(options)
		if err != nil {
			return nil, err
		}
	}
	return options, nil
}

Łukasz Magiera's avatar
Łukasz Magiera committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64
func ObjectAddLinkOptions(opts ...ObjectAddLinkOption) (*ObjectAddLinkSettings, error) {
	options := &ObjectAddLinkSettings{
		Create: false,
	}

	for _, opt := range opts {
		err := opt(options)
		if err != nil {
			return nil, err
		}
	}
	return options, nil
}

65
type objectOpts struct{}
Łukasz Magiera's avatar
Łukasz Magiera committed
66

67 68 69 70 71 72 73 74
var Object objectOpts

// Type is an option for Object.New which allows to change the type of created
// dag node.
//
// Supported types:
// * 'empty' - Empty node
// * 'unixfs-dir' - Empty UnixFS directory
75
func (objectOpts) Type(t string) ObjectNewOption {
Łukasz Magiera's avatar
Łukasz Magiera committed
76 77 78 79 80 81
	return func(settings *ObjectNewSettings) error {
		settings.Type = t
		return nil
	}
}

82 83 84 85 86 87
// InputEnc is an option for Object.Put which specifies the input encoding of the
// data. Default is "json".
//
// Supported encodings:
// * "protobuf"
// * "json"
88
func (objectOpts) InputEnc(e string) ObjectPutOption {
89 90 91 92 93 94
	return func(settings *ObjectPutSettings) error {
		settings.InputEnc = e
		return nil
	}
}

95 96 97 98 99 100
// DataType is an option for Object.Put which specifies the encoding of data
// field when using Json or XML input encoding.
//
// Supported types:
// * "text" (default)
// * "base64"
101
func (objectOpts) DataType(t string) ObjectPutOption {
Łukasz Magiera's avatar
Łukasz Magiera committed
102 103 104 105 106 107
	return func(settings *ObjectPutSettings) error {
		settings.DataType = t
		return nil
	}
}

108 109 110 111 112 113 114 115 116
// WithPin is an option for Object.Put which specifies whether to pin the added
// objects, default is false
func (objectOpts) WithPin(pin bool) ObjectPutOption {
	return func(settings *ObjectPutSettings) error {
		settings.Pin = pin
		return nil
	}
}

117 118
// Create is an option for Object.AddLink which specifies whether create required
// directories for the child
119
func (objectOpts) Create(create bool) ObjectAddLinkOption {
Łukasz Magiera's avatar
Łukasz Magiera committed
120 121 122 123 124
	return func(settings *ObjectAddLinkSettings) error {
		settings.Create = create
		return nil
	}
}