-
Notifications
You must be signed in to change notification settings - Fork 1
Fluent Builder Implementation #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| package taskgraph | ||
|
|
||
| import "fmt" | ||
|
|
||
| // TaskBuilder helps construct taskgraph Tasks with a fluent API. | ||
| type TaskBuilder[T any] struct { | ||
| name string | ||
| resultKey Key[T] | ||
| depends []any | ||
| fn any | ||
| condition Condition | ||
| defaultVal T | ||
| defaultSet bool | ||
| defaultBindings []Binding | ||
| } | ||
|
|
||
| // NewTaskBuilder creates a new builder for a task that produces a result of type T. | ||
| func NewTaskBuilder[T any](name string, key Key[T]) *TaskBuilder[T] { | ||
| return &TaskBuilder[T]{ | ||
| name: name, | ||
| resultKey: key, | ||
| } | ||
| } | ||
|
|
||
| // DependsOn adds dependencies to the task. | ||
| func (b *TaskBuilder[T]) DependsOn(deps ...any) *TaskBuilder[T] { | ||
| b.depends = append(b.depends, deps...) | ||
| return b | ||
| } | ||
|
|
||
| // Run sets the function to execute. The function signature must match the dependencies. | ||
| func (b *TaskBuilder[T]) Run(fn any) *TaskBuilder[T] { | ||
| b.fn = fn | ||
| return b | ||
| } | ||
|
|
||
| // RunIf sets a condition for the task execution. | ||
| func (b *TaskBuilder[T]) RunIf(cond Condition) *TaskBuilder[T] { | ||
| b.condition = cond | ||
| return b | ||
| } | ||
|
|
||
| // RunIfAll sets a ConditionAnd (logical AND) for the task execution using the provided keys. | ||
| func (b *TaskBuilder[T]) RunIfAll(keys ...ReadOnlyKey[bool]) *TaskBuilder[T] { | ||
| b.condition = ConditionAnd(keys) | ||
| return b | ||
| } | ||
|
|
||
| // RunIfAny sets a ConditionOr (logical OR) for the task execution using the provided keys. | ||
| func (b *TaskBuilder[T]) RunIfAny(keys ...ReadOnlyKey[bool]) *TaskBuilder[T] { | ||
| b.condition = ConditionOr(keys) | ||
| return b | ||
| } | ||
|
|
||
| // Default sets the default value for the result key if the condition is false. | ||
| func (b *TaskBuilder[T]) Default(val T) *TaskBuilder[T] { | ||
| b.defaultVal = val | ||
| b.defaultSet = true | ||
| return b | ||
| } | ||
|
|
||
| // WithDefaultBindings adds arbitrary default bindings if the condition is false. | ||
| func (b *TaskBuilder[T]) WithDefaultBindings(bindings ...Binding) *TaskBuilder[T] { | ||
| b.defaultBindings = append(b.defaultBindings, bindings...) | ||
| return b | ||
| } | ||
|
|
||
| // Build constructs and returns the Task. | ||
| func (b *TaskBuilder[T]) Build() TaskSet { | ||
| reflect := Reflect[T]{ | ||
| Name: b.name, | ||
| ResultKey: b.resultKey, | ||
| Depends: b.depends, | ||
| Fn: b.fn, | ||
| } | ||
| reflect.location = getLocation(2) | ||
| var task TaskSet = reflect | ||
|
|
||
| if b.condition != nil { | ||
| conditional := Conditional{ | ||
| Wrapped: task, | ||
| Condition: b.condition, | ||
| } | ||
|
|
||
| if b.defaultSet { | ||
| conditional.DefaultBindings = append(conditional.DefaultBindings, b.resultKey.Bind(b.defaultVal)) | ||
| } | ||
| conditional.DefaultBindings = append(conditional.DefaultBindings, b.defaultBindings...) | ||
|
|
||
| conditional.location = getLocation(2) | ||
| task = conditional | ||
| } | ||
|
|
||
| return task | ||
| } | ||
|
|
||
| // MultiTaskBuilder helps construct taskgraph Tasks that provide multiple outputs or perform side effects. | ||
| type MultiTaskBuilder struct { | ||
| name string | ||
| depends []any | ||
| fn any | ||
| provides []ID | ||
| condition Condition | ||
| defaultBindings []Binding | ||
| } | ||
|
|
||
| // NewMultiTaskBuilder creates a new builder for a multi-output or side-effect task. | ||
| func NewMultiTaskBuilder(name string) *MultiTaskBuilder { | ||
| return &MultiTaskBuilder{ | ||
| name: name, | ||
| } | ||
| } | ||
|
|
||
| // DependsOn adds dependencies to the task. | ||
| func (b *MultiTaskBuilder) DependsOn(deps ...any) *MultiTaskBuilder { | ||
| b.depends = append(b.depends, deps...) | ||
| return b | ||
| } | ||
|
|
||
| // Provides declares the keys that this task provides. | ||
| func (b *MultiTaskBuilder) Provides(keys ...any) *MultiTaskBuilder { | ||
| for _, k := range keys { | ||
| rk, err := newReflectKey(k) | ||
| if err != nil { | ||
| panic(fmt.Errorf("invalid key passed to Provides: %w", err)) | ||
| } | ||
| id, err := rk.ID() | ||
| if err != nil { | ||
| panic(fmt.Errorf("invalid key ID in Provides: %w", err)) | ||
| } | ||
| b.provides = append(b.provides, id) | ||
| } | ||
| return b | ||
| } | ||
|
|
||
| // Run sets the function to execute. The function signature must match the dependencies. | ||
| // Fn must return []Binding or ([]Binding, error). | ||
| func (b *MultiTaskBuilder) Run(fn any) *MultiTaskBuilder { | ||
| b.fn = fn | ||
| return b | ||
| } | ||
|
|
||
| // RunIf sets a condition for the task execution. | ||
| func (b *MultiTaskBuilder) RunIf(cond Condition) *MultiTaskBuilder { | ||
| b.condition = cond | ||
| return b | ||
| } | ||
|
|
||
| // RunIfAll sets a ConditionAnd (logical AND) for the task execution using the provided keys. | ||
| func (b *MultiTaskBuilder) RunIfAll(keys ...ReadOnlyKey[bool]) *MultiTaskBuilder { | ||
| b.condition = ConditionAnd(keys) | ||
| return b | ||
| } | ||
|
|
||
| // RunIfAny sets a ConditionOr (logical OR) for the task execution using the provided keys. | ||
| func (b *MultiTaskBuilder) RunIfAny(keys ...ReadOnlyKey[bool]) *MultiTaskBuilder { | ||
| b.condition = ConditionOr(keys) | ||
| return b | ||
| } | ||
|
|
||
| // WithDefaultBindings adds arbitrary default bindings if the condition is false. | ||
| func (b *MultiTaskBuilder) WithDefaultBindings(bindings ...Binding) *MultiTaskBuilder { | ||
| b.defaultBindings = append(b.defaultBindings, bindings...) | ||
| return b | ||
| } | ||
|
|
||
| // Build constructs and returns the Task. | ||
| func (b *MultiTaskBuilder) Build() TaskSet { | ||
| reflect := ReflectMulti{ | ||
| Name: b.name, | ||
| Depends: b.depends, | ||
| Fn: b.fn, | ||
| Provides: b.provides, | ||
| } | ||
| reflect.location = getLocation(2) | ||
| var task TaskSet = reflect | ||
|
|
||
| if b.condition != nil { | ||
| conditional := Conditional{ | ||
| Wrapped: task, | ||
| Condition: b.condition, | ||
| DefaultBindings: b.defaultBindings, | ||
| } | ||
| conditional.location = getLocation(2) | ||
| task = conditional | ||
| } | ||
|
|
||
| return task | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package taskgraph | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestTaskBuilder_RunIfAll(t *testing.T) { | ||
| k1 := NewKey[bool]("k1") | ||
| k2 := NewKey[bool]("k2") | ||
| res := NewKey[string]("res") | ||
|
|
||
| task := NewTaskBuilder[string]("test", res). | ||
| Run(func() string { return "ok" }). | ||
| RunIfAll(k1, k2). | ||
| Default("default"). | ||
| Build() | ||
|
|
||
| // Simulate execution (simplified verification) | ||
| tasks := task.Tasks() | ||
| if len(tasks) != 1 { | ||
| t.Fatalf("expected 1 task, got %d", len(tasks)) | ||
| } | ||
| // We can't easily execute it without a full graph, but we can check if it didn't panic and produced a task. | ||
| } | ||
|
|
||
| func TestMultiTaskBuilder_Provides(t *testing.T) { | ||
| k1 := NewKey[string]("k1") | ||
| k2 := NewKey[int]("k2") | ||
|
|
||
| task := NewMultiTaskBuilder("multi"). | ||
| Provides(k1, k2). | ||
| Run(func() ([]Binding, error) { | ||
| return []Binding{k1.Bind("s"), k2.Bind(1)}, nil | ||
| }). | ||
| Build() | ||
|
|
||
| tasks := task.Tasks() | ||
| if len(tasks) != 1 { | ||
| t.Fatalf("expected 1 task, got %d", len(tasks)) | ||
| } | ||
| provided := tasks[0].Provides() | ||
| if len(provided) != 2 { | ||
| t.Fatalf("expected 2 provided keys, got %d", len(provided)) | ||
| } | ||
| } | ||
|
|
||
| func TestMultiTaskBuilder_Provides_InvalidKey(t *testing.T) { | ||
| defer func() { | ||
| if r := recover(); r == nil { | ||
| t.Errorf("expected panic on invalid key") | ||
| } | ||
| }() | ||
| NewMultiTaskBuilder("fail").Provides("not a key") | ||
| } | ||
|
|
||
| func TestMultiTaskBuilder_RunIfAny(t *testing.T) { | ||
| k1 := NewKey[bool]("k1") | ||
| k2 := NewKey[bool]("k2") | ||
|
|
||
| task := NewMultiTaskBuilder("multi_cond"). | ||
| RunIfAny(k1, k2). | ||
| Run(func() []Binding { return nil }). | ||
| Build() | ||
|
|
||
| if task == nil { | ||
| t.Fatal("expected task to be built") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not ok with this panicking. At a minimum it should use the
Musthelper, but I think it would be better to capture the errors, store them in the builder struct as a slice, and have theBuildfunction return the errors (and then optionally provide aTasksmethod which callsMust(Build())like we do forReflectMulti)