-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc.go
More file actions
239 lines (239 loc) · 7.19 KB
/
doc.go
File metadata and controls
239 lines (239 loc) · 7.19 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Package needle provides a type-safe dependency injection framework for Go 1.25+.
//
// Needle is designed to be simple yet powerful, offering compile-time type safety
// through generics, lifecycle management, scoped dependencies, and modular organization.
//
// # Quick Start
//
// Create a container and register providers:
//
// c := needle.New()
//
// needle.Provide(c, func(ctx context.Context, r needle.Resolver) (*Config, error) {
// return &Config{Port: 8080}, nil
// })
//
// needle.Provide(c, func(ctx context.Context, r needle.Resolver) (*Server, error) {
// cfg := needle.MustInvoke[*Config](c)
// return &Server{config: cfg}, nil
// })
//
// c.Run(ctx)
//
// # Providers
//
// Providers are functions that create instances of a type. They receive a context
// and a Resolver for accessing other dependencies:
//
// needle.Provide[T](c, provider) // Register a provider
// needle.ProvideValue[T](c, value) // Register an existing value
// needle.ProvideNamed[T](c, "name", prov) // Register a named provider
//
// # Auto-Wiring
//
// Reduce boilerplate with constructor auto-wiring and struct tag injection.
//
// Constructor auto-wiring automatically resolves function parameters:
//
// func NewUserService(db *Database, log *Logger) *UserService {
// return &UserService{db: db, log: log}
// }
// needle.ProvideFunc[*UserService](c, NewUserService)
//
// Struct tag injection uses the `needle` tag to inject fields:
//
// type UserService struct {
// DB *Database `needle:""` // inject by type
// Log *Logger `needle:"appLogger"` // inject by name
// Cache *Cache `needle:",optional"` // optional dependency
// }
// needle.ProvideStruct[*UserService](c)
//
// Or invoke directly without registering:
//
// svc, err := needle.InvokeStruct[*UserService](c)
//
// # Resolution
//
// Resolve dependencies using the Invoke functions:
//
// svc, err := needle.Invoke[*Service](c) // Returns value and error
// svc := needle.MustInvoke[*Service](c) // Panics on error
//
// # Optional Dependencies
//
// Use Optional for dependencies that may or may not be registered:
//
// opt, err := needle.InvokeOptional[*Cache](c)
// if err != nil {
// // registered but resolution failed
// }
// if opt.Present() {
// cache := opt.Value()
// }
//
// // Or use OrElse for default values
// opt, _ := needle.InvokeOptional[*Cache](c)
// cache := opt.OrElse(defaultCache)
//
// // OrElseFunc for lazy defaults
// opt, _ := needle.InvokeOptional[*Cache](c)
// cache := opt.OrElseFunc(func() *Cache {
// return NewDefaultCache()
// })
//
// # Lifecycle
//
// Services can participate in the container's lifecycle:
//
// needle.Provide(c, NewServer,
// needle.WithOnStart(func(ctx context.Context) error {
// return server.Listen()
// }),
// needle.WithOnStop(func(ctx context.Context) error {
// return server.Shutdown(ctx)
// }),
// )
//
// c.Start(ctx) // Starts all services in dependency order
// c.Stop(ctx) // Stops all services in reverse order
// c.Run(ctx) // Start + wait for signal + Stop
//
// # Lazy Providers
//
// Defer instantiation until first use:
//
// needle.Provide(c, NewExpensiveService, needle.WithLazy())
//
// Lazy services are not instantiated during Start(). They are created on first
// Invoke(), and their OnStart hooks run at that time if the container is running.
//
// # Parallel Startup
//
// Start independent services concurrently for faster boot times:
//
// c := needle.New(needle.WithParallel())
//
// Services at the same dependency level start in parallel. Services still
// wait for their dependencies before starting.
//
// # Shutdown Timeout
//
// Configure a deadline for graceful shutdown:
//
// c := needle.New(needle.WithShutdownTimeout(30 * time.Second))
//
// The timeout applies to Stop() and is checked between service shutdowns.
// Individual OnStop hooks receive the timeout context.
//
// # Debug Visualization
//
// Print the dependency graph for debugging:
//
// c.PrintGraph() // ASCII to stdout
// c.PrintGraphDOT() // Graphviz DOT to stdout
// output := c.SprintGraph()
// info := c.Graph() // Structured GraphInfo
//
// # Modules
//
// Group related providers into modules:
//
// var ConfigModule = needle.NewModule("config")
// needle.ModuleProvideValue(ConfigModule, &Config{Port: 8080})
//
// var HTTPModule = needle.NewModule("http")
// needle.ModuleProvide(HTTPModule, NewServer)
// needle.ModuleProvide(HTTPModule, NewRouter)
//
// c.Apply(ConfigModule, HTTPModule)
//
// Modules can include other modules:
//
// var AppModule = needle.NewModule("app").
// Include(ConfigModule).
// Include(HTTPModule)
//
// # Interface Binding
//
// Bind interfaces to concrete implementations:
//
// needle.Bind[UserRepository, *PostgresUserRepo](c)
// needle.BindNamed[Cache, *RedisCache](c, "session")
//
// Or within modules:
//
// needle.ModuleBind[UserRepository, *PostgresUserRepo](module)
//
// # Decorators
//
// Wrap services with cross-cutting concerns:
//
// needle.Decorate(c, func(ctx context.Context, r needle.Resolver, log *Logger) (*Logger, error) {
// return log.Named("app"), nil
// })
//
// Decorators are applied in order and can be chained:
//
// needle.Decorate(c, addMetrics)
// needle.Decorate(c, addTracing)
//
// # Scopes
//
// Control instance lifetime with scopes:
//
// needle.Provide(c, NewService, needle.WithScope(needle.Transient))
// needle.Provide(c, NewService, needle.WithScope(needle.Request))
// needle.Provide(c, NewService, needle.WithPoolSize(10))
//
// Available scopes: Singleton (default), Transient, Request, Pooled.
//
// # Health Checks
//
// Services can implement health check interfaces:
//
// type Database struct{}
// func (d *Database) HealthCheck(ctx context.Context) error { return d.Ping(ctx) }
// func (d *Database) ReadinessCheck(ctx context.Context) error { return d.Ready(ctx) }
//
// Check health status:
//
// err := c.Live(ctx) // Fails if any HealthChecker returns error
// err := c.Ready(ctx) // Fails if any ReadinessChecker returns error
// reports := c.Health(ctx) // Get detailed health reports with latency
//
// # Hot Reload / Dynamic Replacement
//
// Replace services at runtime without restarting the container:
//
// needle.ReplaceValue(c, &Config{NewValue: "updated"})
// needle.Replace(c, newProvider)
// needle.ReplaceFunc[*Service](c, NewServiceConstructor)
// needle.ReplaceStruct[*Service](c)
//
// Named variants are also available:
//
// needle.ReplaceNamedValue(c, "primary", &Config{})
// needle.ReplaceNamed(c, "primary", provider)
//
// This is useful for feature flags, A/B testing, or configuration updates.
//
// # Metrics Observers
//
// Observe container operations for metrics integration:
//
// c := needle.New(
// needle.WithResolveObserver(func(key string, d time.Duration, err error) {
// metrics.RecordResolve(key, d, err)
// }),
// needle.WithProvideObserver(func(key string) {
// metrics.RecordProvide(key)
// }),
// needle.WithStartObserver(func(key string, d time.Duration, err error) {
// metrics.RecordStart(key, d, err)
// }),
// needle.WithStopObserver(func(key string, d time.Duration, err error) {
// metrics.RecordStop(key, d, err)
// }),
// )
package needle