generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(logic): replace the parser by a router
- Loading branch information
Showing
5 changed files
with
60 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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,40 @@ | ||
package fs | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
) | ||
|
||
type URIHandler interface { | ||
Scheme() string | ||
Open(ctx context.Context, uri *url.URL) ([]byte, error) | ||
} | ||
|
||
type Router struct { | ||
handlers map[string]URIHandler | ||
} | ||
|
||
func NewRouter() Router { | ||
return Router{ | ||
handlers: make(map[string]URIHandler), | ||
} | ||
} | ||
func (r *Router) Open(ctx context.Context, name string) ([]byte, error) { | ||
uri, err := url.Parse(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
handler, ok := r.handlers[uri.Scheme] | ||
|
||
if !ok { | ||
return nil, fmt.Errorf("could not find handler for load %s file", name) | ||
} | ||
|
||
return handler.Open(ctx, uri) | ||
} | ||
|
||
func (r *Router) RegisterHandler(handler URIHandler) { | ||
r.handlers[handler.Scheme()] = handler | ||
} |
This file contains 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 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