46 lines
1.2 KiB
Go
46 lines
1.2 KiB
Go
|
package fs
|
||
|
|
||
|
import "io"
|
||
|
|
||
|
type GetPathFunc[K comparable] func(key K) ([]string, error)
|
||
|
type MarshalValueFunc[V any] func(value V) (io.Reader, error)
|
||
|
type UnmarshalValueFunc[V any] func(r io.Reader) (V, error)
|
||
|
|
||
|
type Options[K comparable, V any] struct {
|
||
|
GetPath GetPathFunc[K]
|
||
|
MarshalValue MarshalValueFunc[V]
|
||
|
UnmarshalValue UnmarshalValueFunc[V]
|
||
|
}
|
||
|
|
||
|
type OptionsFunc[K comparable, V any] func(opts *Options[K, V])
|
||
|
|
||
|
func DefaultOptions[K comparable, V any](funcs ...OptionsFunc[K, V]) *Options[K, V] {
|
||
|
opts := &Options[K, V]{
|
||
|
GetPath: DefaultGetPath[K],
|
||
|
MarshalValue: DefaultMarshalValue[V],
|
||
|
UnmarshalValue: DefaultUnmarshalValue[V],
|
||
|
}
|
||
|
for _, fn := range funcs {
|
||
|
fn(opts)
|
||
|
}
|
||
|
return opts
|
||
|
}
|
||
|
|
||
|
func WithGetPath[K comparable, V any](getKeyHash GetPathFunc[K]) OptionsFunc[K, V] {
|
||
|
return func(opts *Options[K, V]) {
|
||
|
opts.GetPath = getKeyHash
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func WithMarshalValue[K comparable, V any](marshalValue MarshalValueFunc[V]) OptionsFunc[K, V] {
|
||
|
return func(opts *Options[K, V]) {
|
||
|
opts.MarshalValue = marshalValue
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func WithUnmarshalValue[K comparable, V any](unmarshalValue UnmarshalValueFunc[V]) OptionsFunc[K, V] {
|
||
|
return func(opts *Options[K, V]) {
|
||
|
opts.UnmarshalValue = unmarshalValue
|
||
|
}
|
||
|
}
|