object.go 1.57 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 9 10
type ObjectPutSettings struct {
	InputEnc string
}

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

type ObjectNewOption func(*ObjectNewSettings) error
16
type ObjectPutOption func(*ObjectPutSettings) error
Łukasz Magiera's avatar
Łukasz Magiera committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
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
}

33 34 35 36 37 38 39 40 41 42 43 44 45 46
func ObjectPutOptions(opts ...ObjectPutOption) (*ObjectPutSettings, error) {
	options := &ObjectPutSettings{
		InputEnc: "json",
	}

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

Łukasz Magiera's avatar
Łukasz Magiera committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
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
}

type ObjectOptions struct{}

func (api *ObjectOptions) WithType(t string) ObjectNewOption {
	return func(settings *ObjectNewSettings) error {
		settings.Type = t
		return nil
	}
}

70 71 72 73 74 75 76
func (api *ObjectOptions) WithInputEnc(e string) ObjectPutOption {
	return func(settings *ObjectPutSettings) error {
		settings.InputEnc = e
		return nil
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
77 78 79 80 81 82
func (api *ObjectOptions) WithCreate(create bool) ObjectAddLinkOption {
	return func(settings *ObjectAddLinkSettings) error {
		settings.Create = create
		return nil
	}
}