Skip to content

Commit f23f6b6

Browse files
fix: add helper to register operation without calling untyped
1 parent ec99ee5 commit f23f6b6

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

operations/operation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ type Bundle struct {
1818
reporter Reporter
1919
// internal use only, for storing the hash of the report to avoid repeat sha256 computation.
2020
reportHashCache *sync.Map
21-
OperationRegistry OperationRegistry
21+
OperationRegistry *OperationRegistry
2222
}
2323

2424
// BundleOption is a functional option for configuring a Bundle
2525
type BundleOption func(*Bundle)
2626

2727
// WithOperationRegistry sets a custom OperationRegistry for the Bundle
28-
func WithOperationRegistry(registry OperationRegistry) BundleOption {
28+
func WithOperationRegistry(registry *OperationRegistry) BundleOption {
2929
return func(b *Bundle) {
3030
b.OperationRegistry = registry
3131
}

operations/registry.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ type OperationRegistry struct {
88
}
99

1010
// NewOperationRegistry creates a new OperationRegistry with the provided untyped operations.
11-
func NewOperationRegistry(ops ...*Operation[any, any, any]) OperationRegistry {
12-
return OperationRegistry{
11+
func NewOperationRegistry(ops ...*Operation[any, any, any]) *OperationRegistry {
12+
return &OperationRegistry{
1313
ops: ops,
1414
}
1515
}
@@ -26,3 +26,12 @@ func (s OperationRegistry) Retrieve(def Definition) (*Operation[any, any, any],
2626

2727
return nil, errors.New("operation not found in registry")
2828
}
29+
30+
// RegisterOperation registers new operations in the registry.
31+
// To register operations with different input, output, and dependency types,
32+
// call RegisterOperation multiple times with different type parameters.
33+
func RegisterOperation[D, I, O any](r *OperationRegistry, op ...*Operation[D, I, O]) {
34+
for _, o := range op {
35+
r.ops = append(r.ops, o.AsUntyped())
36+
}
37+
}

operations/registry_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ func ExampleOperationRegistry() {
3636
return input, nil
3737
},
3838
)
39-
// Create registry with untyped operations
40-
registry := NewOperationRegistry(stringOp.AsUntyped(), intOp.AsUntyped())
39+
// Create registry with untyped operations by providing optional initial operation
40+
registry := NewOperationRegistry(stringOp.AsUntyped())
41+
42+
// An alternative way to register additional operations without calling AsUntyped()
43+
RegisterOperation(registry, intOp)
4144

4245
// Create execution environment
4346
b := NewBundle(context.Background, logger.Nop(), NewMemoryReporter(), WithOperationRegistry(registry))

0 commit comments

Comments
 (0)