File tree Expand file tree Collapse file tree 2 files changed +17
-2
lines changed
Expand file tree Collapse file tree 2 files changed +17
-2
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,20 @@ type Source struct {
1919 Type SourceType
2020}
2121
22+ // NewSource creates a Source by categorizing the input path as stdin, URL, or file.
23+ // This function is intentionally infallible (does not return an error) to allow
24+ // clean usage in struct literal initialization and avoid error handling boilerplate
25+ // in hundreds of call sites throughout the codebase.
26+ //
27+ // Design rationale:
28+ // - Valid http/https URLs are categorized as SourceTypeURL
29+ // - "-" is categorized as SourceTypeStdin
30+ // - Everything else (including URLs with invalid schemes like ftp://) is categorized as SourceTypeFile
31+ // - Actual validation and error handling occurs later when the Loader interface methods
32+ // (LoadFromURI, LoadFromFile, LoadFromStdin) are called
33+ //
34+ // This design provides clean separation of concerns: NewSource categorizes inputs,
35+ // while Loader implementations handle validation and produce appropriate error messages.
2236func NewSource (path string ) * Source {
2337 if path == "-" {
2438 return & Source {
@@ -27,7 +41,8 @@ func NewSource(path string) *Source {
2741 }
2842 }
2943
30- if uri , err := getURL (path ); err == nil {
44+ uri , err := getURL (path )
45+ if err == nil {
3146 return & Source {
3247 Path : path ,
3348 Type : SourceTypeURL ,
Original file line number Diff line number Diff line change @@ -21,5 +21,5 @@ func TestLoadInfo_UriInvalid(t *testing.T) {
2121
2222func TestLoadInfo_UriBadScheme (t * testing.T ) {
2323 _ , err := load .NewSpecInfo (MockLoader {}, load .NewSource ("ftp://localhost/null" ))
24- require .EqualError (t , err , "open ftp:// localhost/null: no such file or directory" )
24+ require .EqualError (t , err , "open ftp:/localhost/null: no such file or directory" )
2525}
You can’t perform that action at this time.
0 commit comments