Skip to content

Commit d5db061

Browse files
znullCopilot
authored andcommitted
Expose Function stream requirements
Add Function options for declaring stdin and stdout stream requirements, so Function stages can participate in file-preference negotiation. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 3db99a6 commit d5db061

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

pipe/function.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,32 @@ type StageFunc func(ctx context.Context, env Env, stdin io.Reader, stdout io.Wri
3333
// FunctionOption configures a Function stage.
3434
type FunctionOption func(*goStage)
3535

36+
// WithStdinRequirement returns a FunctionOption declaring the stage's stdin
37+
// requirement.
38+
func WithStdinRequirement(requirement StreamRequirement) FunctionOption {
39+
return func(s *goStage) {
40+
s.requirements.Stdin = requirement
41+
}
42+
}
43+
44+
// WithStdoutRequirement returns a FunctionOption declaring the stage's stdout
45+
// requirement.
46+
func WithStdoutRequirement(requirement StreamRequirement) FunctionOption {
47+
return func(s *goStage) {
48+
s.requirements.Stdout = requirement
49+
}
50+
}
51+
3652
// ForbidStdin returns a FunctionOption declaring that the stage must not be
3753
// connected to stdin.
3854
func ForbidStdin() FunctionOption {
39-
return func(s *goStage) {
40-
s.requirements.Stdin = StreamForbidden
41-
}
55+
return WithStdinRequirement(StreamForbidden)
4256
}
4357

4458
// ForbidStdout returns a FunctionOption declaring that the stage must not be
4559
// connected to stdout.
4660
func ForbidStdout() FunctionOption {
47-
return func(s *goStage) {
48-
s.requirements.Stdout = StreamForbidden
49-
}
61+
return WithStdoutRequirement(StreamForbidden)
5062
}
5163

5264
// Function returns a pipeline `Stage` that will run a `StageFunc` in

pipe/pipeline_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,24 @@ func TestFunctionOptionsForbidStreams(t *testing.T) {
968968
})
969969
}
970970

971+
func TestFunctionOptionsSetStreamRequirements(t *testing.T) {
972+
t.Parallel()
973+
974+
stage := pipe.Function(
975+
"file-preferring",
976+
func(_ context.Context, _ pipe.Env, _ io.Reader, _ io.Writer) error {
977+
return nil
978+
},
979+
pipe.WithStdinRequirement(pipe.StreamPreferFile),
980+
pipe.WithStdoutRequirement(pipe.StreamPreferFile),
981+
)
982+
983+
assert.Equal(t, pipe.StageRequirements{
984+
Stdin: pipe.StreamPreferFile,
985+
Stdout: pipe.StreamPreferFile,
986+
}, stage.Requirements())
987+
}
988+
971989
func TestStreamForbiddenStdin(t *testing.T) {
972990
t.Parallel()
973991
ctx := context.Background()

0 commit comments

Comments
 (0)