-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitializer.go
35 lines (28 loc) · 1.45 KB
/
initializer.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
package common
// Original Idea:
// ==== Initializer Function Types ====
// type BackendInitializer[T any] func(modelPath string, opts ...InitializationOptionSetter) (*T, error)
// type BackendInitializerWithOptions[T any] func(modelPath string, initializationOptions InitializationOptions) (*T, error)
// Improved Form:
// ==== BackendInitializer Generic Struct ====
// Each go-* backend should export one of these where T is the wrapper for that backend.
// map[string]BackendInitializer can be created to easily initialize
// TODO: this really could just be any, but this looks margially cleaner?
type Backend interface {
Name() string
Close() error
}
type BackendInitializer[T Backend] struct {
DefaultInitializationOptions InitializationOptions
Constructor func(modelPath string, initializationOptions InitializationOptions) (*T, error)
}
func (bi BackendInitializer[T]) Defaults(modelPath string) (*T, error) {
return bi.Constructor(modelPath, bi.DefaultInitializationOptions)
}
func (bi BackendInitializer[T]) New(modelPath string, opts ...InitializationOptionSetter) (*T, error) {
optionCopy := bi.DefaultInitializationOptions
return bi.Constructor(modelPath, *MergeInitializationOptions(&optionCopy, opts...))
}
func (bi BackendInitializer[T]) NewWithInitializationOptions(modelPath string, initializationOptions InitializationOptions) (*T, error) {
return bi.Constructor(modelPath, initializationOptions)
}