-
Notifications
You must be signed in to change notification settings - Fork 0
/
konfetty.go
151 lines (129 loc) · 3.87 KB
/
konfetty.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Package konfetty provides zero-dependency, type-safe and powerful post-processing for your data structs,
// mostly focused on applying defaults, transformations, and validations to configuration structures.
package konfetty
import (
"errors"
"fmt"
"reflect"
)
// Provider defines an interface for loading structured data.
type Provider[T any] interface {
Load() (T, error)
}
// dataSource is an internal type to represent the source of data.
type dataSource[T any] struct {
data *T
loaderFunc func() (T, error)
provider Provider[T]
}
// Builder orchestrates the building process. It manages the data source, defaults, transformations, and validations.
type Builder[T any] struct {
source dataSource[T]
defaults map[reflect.Type][]any
transform func(*T)
validate func(*T) error
}
// Processor exposes methods for further data-structure processing. It wraps a Builder and provides a fluent interface
// for configuration setup.
type Processor[T any] struct {
builder *Builder[T]
}
// FromStruct initializes a Processor with a pre-populated struct.
//
// cfg := &MyConfig{...}
// processor := konfetty.FromStruct(cfg)
func FromStruct[T any](config *T) *Processor[T] {
return &Processor[T]{
builder: &Builder[T]{
source: dataSource[T]{data: config},
},
}
}
// FromLoaderFunc initializes a Processor with a function that loads the data-structure.
//
// loader := func() (MyConfig, error) { ... }
// processor := konfetty.FromLoaderFunc(loader)
func FromLoaderFunc[T any](loader func() (T, error)) *Processor[T] {
return &Processor[T]{
builder: &Builder[T]{
source: dataSource[T]{loaderFunc: loader},
},
}
}
// FromProvider initializes a Processor with a Provider.
//
// provider := MyConfigProvider{}
// processor := konfetty.FromProvider(provider)
func FromProvider[T any](provider Provider[T]) *Processor[T] {
return &Processor[T]{
builder: &Builder[T]{
source: dataSource[T]{provider: provider},
},
}
}
// WithDefaults adds default values to the processing pipeline. Multiple defaults can be provided and will be applied
// in order.
func (p *Processor[T]) WithDefaults(defaultValues ...any) *Processor[T] {
if p.builder.defaults == nil {
p.builder.defaults = make(map[reflect.Type][]any)
}
for _, dv := range defaultValues {
t := reflect.TypeOf(dv)
p.builder.defaults[t] = append(p.builder.defaults[t], dv)
}
return p
}
// WithTransformer sets a custom transformation function to be applied to the data-structure.
func (p *Processor[T]) WithTransformer(fn func(*T)) *Processor[T] {
p.builder.transform = fn
return p
}
// WithValidator sets a custom validation function to be applied to the data-structure.
func (p *Processor[T]) WithValidator(fn func(*T) error) *Processor[T] {
p.builder.validate = fn
return p
}
// Build processes the data-structure, applying defaults, transformations, and validations. It returns the final
// struct or an error if any step fails.
func (p *Processor[T]) Build() (*T, error) {
return p.builder.build()
}
func (b *Builder[T]) build() (*T, error) {
cfg, err := b.load()
if err != nil {
return nil, fmt.Errorf("load: %w", err)
}
if err = applyDefaults(&cfg, b.defaults); err != nil {
return nil, fmt.Errorf("apply defaults: %w", err)
}
if b.transform != nil {
b.transform(&cfg)
}
if b.validate != nil {
if err = b.validate(&cfg); err != nil {
return nil, fmt.Errorf("validate: %w", err)
}
}
return &cfg, nil
}
func (b *Builder[T]) load() (T, error) {
var cfg T
var err error
switch {
case b.source.data != nil:
cfg = *b.source.data
case b.source.loaderFunc != nil:
cfg, err = b.source.loaderFunc()
if err != nil {
return cfg, fmt.Errorf("from loader func: %w", err)
}
case b.source.provider != nil:
cfg, err = b.source.provider.Load()
if err != nil {
return cfg, fmt.Errorf("from provider: %w", err)
}
default:
return cfg, errors.New("no data source provided")
}
return cfg, nil
}