-
Notifications
You must be signed in to change notification settings - Fork 174
Refactor: extract common loader/saver interfaces (Github Workflow generation prep). #3262
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
Merged
knative-prow
merged 1 commit into
knative:main
from
twoGiants:issue-744-extract-common-interfaces
Dec 1, 2025
Merged
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,47 @@ | ||
| package common | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| fn "knative.dev/func/pkg/functions" | ||
| ) | ||
|
|
||
| // DefaultLoaderSaver implements FunctionLoaderSaver composite interface | ||
| var DefaultLoaderSaver FunctionLoaderSaver = standardLoaderSaver{} | ||
|
|
||
| // FunctionLoader loads a function from a filesystem path. | ||
| type FunctionLoader interface { | ||
| Load(path string) (fn.Function, error) | ||
| } | ||
|
|
||
| // FunctionSaver persists a function to storage. | ||
| type FunctionSaver interface { | ||
| Save(f fn.Function) error | ||
| } | ||
|
|
||
| // FunctionLoaderSaver combines loading and saving capabilities for functions. | ||
| type FunctionLoaderSaver interface { | ||
| FunctionLoader | ||
| FunctionSaver | ||
| } | ||
|
|
||
| type standardLoaderSaver struct{} | ||
|
|
||
| // Load creates and validates a function from the given filesystem path. | ||
| func (s standardLoaderSaver) Load(path string) (fn.Function, error) { | ||
| f, err := fn.NewFunction(path) | ||
| if err != nil { | ||
| return fn.Function{}, fmt.Errorf("failed to create new function (path: %q): %w", path, err) | ||
| } | ||
|
|
||
| if !f.Initialized() { | ||
| return fn.Function{}, fn.NewErrNotInitialized(f.Root) | ||
| } | ||
|
|
||
| return f, nil | ||
| } | ||
|
|
||
| // Save writes the function configuration to disk. | ||
| func (s standardLoaderSaver) Save(f fn.Function) error { | ||
| return f.Write() | ||
| } |
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,54 @@ | ||
| package common_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "gotest.tools/v3/assert" | ||
| "knative.dev/func/cmd/common" | ||
| cmdTest "knative.dev/func/cmd/testing" | ||
| fn "knative.dev/func/pkg/functions" | ||
| fnTest "knative.dev/func/pkg/testing" | ||
| ) | ||
|
|
||
| func TestDefaultLoaderSaver_SuccessfulLoad(t *testing.T) { | ||
| existingFunc := cmdTest.CreateFuncInTempDir(t, "ls-func") | ||
|
|
||
| actualFunc, err := common.DefaultLoaderSaver.Load(existingFunc.Root) | ||
|
|
||
| assert.NilError(t, err) | ||
| assert.Equal(t, existingFunc.Name, actualFunc.Name) | ||
| } | ||
|
|
||
| func TestDefaultLoaderSaver_GenericFuncCreateError_WhenFuncPathInvalid(t *testing.T) { | ||
| _, err := common.DefaultLoaderSaver.Load("/non-existing-path") | ||
|
|
||
| assert.ErrorContains(t, err, "failed to create new function") | ||
| } | ||
|
|
||
| func TestDefaultLoaderSaver_IsNotInitializedError_WhenNoFuncAtPath(t *testing.T) { | ||
| expectedErrMsg := fn.NewErrNotInitialized(fnTest.Cwd()).Error() | ||
|
|
||
| _, err := common.DefaultLoaderSaver.Load(fnTest.Cwd()) | ||
|
|
||
| assert.Error(t, err, expectedErrMsg) | ||
| } | ||
|
|
||
| func TestDefaultLoaderSaver_SuccessfulSave(t *testing.T) { | ||
| existingFunc := cmdTest.CreateFuncInTempDir(t, "") | ||
| name := "environment" | ||
| value := "test" | ||
| existingFunc.Run.Envs.Add(name, value) | ||
|
|
||
| saveErr := common.DefaultLoaderSaver.Save(existingFunc) | ||
| actualFunc, loadErr := common.DefaultLoaderSaver.Load(existingFunc.Root) | ||
|
|
||
| assert.NilError(t, saveErr) | ||
| assert.NilError(t, loadErr) | ||
| assert.Equal(t, actualFunc.Run.Envs.Slice()[0], "environment=test") | ||
| } | ||
|
|
||
| func TestDefaultLoaderSaver_ForwardsSaveError(t *testing.T) { | ||
| err := common.DefaultLoaderSaver.Save(fn.Function{}) | ||
|
|
||
| assert.Error(t, err, "function root path is required") | ||
| } |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package testing | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "gotest.tools/v3/assert" | ||
| fn "knative.dev/func/pkg/functions" | ||
| fnTest "knative.dev/func/pkg/testing" | ||
| ) | ||
|
|
||
| // CreateFuncInTempDir creates and initializes a Go function in a temporary | ||
| // directory for testing. | ||
| func CreateFuncInTempDir(t *testing.T, fnName string) fn.Function { | ||
| t.Helper() | ||
|
|
||
| var name string | ||
| if fnName == "" { | ||
| name = "go-func" | ||
| } | ||
|
|
||
| result, err := fn.New().Init(fn.Function{ | ||
| Name: name, | ||
| Runtime: "go", | ||
| Root: fnTest.FromTempDirectory(t), | ||
| }) | ||
| assert.NilError(t, err) | ||
|
|
||
| return result | ||
| } |
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 am wondering, if we should have them in the
pkg/functionspackage instead, like we have it for some function related interfaces too 🤔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 kept in in the cmd layer because it just a thin wrapper around very little functionality of the Function package which is not used outside the cmd layer.
i.e. it is not a real extension of the
FunctionsAPI...just a facade.