mirror of
https://github.com/laravel-ls/uri
synced 2026-06-19 03:20:01 +02:00
uri: implements Parse, File and From functions
This commit is contained in:
parent
1d82d5b968
commit
c2efe8eb04
1 changed files with 68 additions and 3 deletions
71
uri.go
71
uri.go
|
|
@ -5,6 +5,15 @@
|
||||||
// Package uri implementation vscode-uri for Go.
|
// Package uri implementation vscode-uri for Go.
|
||||||
package uri
|
package uri
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
// FileScheme schema of filesystem path.
|
||||||
|
const FileScheme = "file"
|
||||||
|
|
||||||
// URI Uniform Resource Identifier (URI) http://tools.ietf.org/html/rfc3986.
|
// URI Uniform Resource Identifier (URI) http://tools.ietf.org/html/rfc3986.
|
||||||
//
|
//
|
||||||
// This class is a simple parser which creates the basic component parts
|
// This class is a simple parser which creates the basic component parts
|
||||||
|
|
@ -62,9 +71,29 @@ type URI struct {
|
||||||
Scheme string `json:"scheme,omitempty"`
|
Scheme string `json:"scheme,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Components represents a URI Components.
|
// String implements fmt.Stringer.
|
||||||
type Components struct {
|
func (u *URI) String() string {
|
||||||
*URI
|
switch u.Scheme {
|
||||||
|
case FileScheme:
|
||||||
|
uri := &url.URL{
|
||||||
|
Scheme: u.Scheme,
|
||||||
|
Path: u.Path,
|
||||||
|
}
|
||||||
|
return uri.String()
|
||||||
|
|
||||||
|
case "http", "https":
|
||||||
|
uri := &url.URL{
|
||||||
|
Scheme: u.Scheme,
|
||||||
|
Host: u.Authority,
|
||||||
|
Path: u.Path,
|
||||||
|
RawQuery: url.QueryEscape(u.Query),
|
||||||
|
Fragment: u.Fragment,
|
||||||
|
}
|
||||||
|
return uri.String()
|
||||||
|
|
||||||
|
default:
|
||||||
|
return "unknown schema"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// State represents a URI State.
|
// State represents a URI State.
|
||||||
|
|
@ -73,3 +102,39 @@ type State struct {
|
||||||
Mid float64 `json:"$mid,omitempty"`
|
Mid float64 `json:"$mid,omitempty"`
|
||||||
External string `json:"external,omitempty"`
|
External string `json:"external,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parse parses and creates a new URI from uri.
|
||||||
|
func Parse(uri string) *URI {
|
||||||
|
u, err := url.Parse(uri)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("url.Parse: %#v\n", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return &URI{
|
||||||
|
Scheme: u.Scheme,
|
||||||
|
Authority: u.Host,
|
||||||
|
Path: u.Path,
|
||||||
|
Query: u.Query().Encode(),
|
||||||
|
Fragment: u.Fragment,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// File parses and creates a new URI filesystem path from path.
|
||||||
|
func File(path string) *URI {
|
||||||
|
return &URI{
|
||||||
|
Scheme: FileScheme,
|
||||||
|
Path: path,
|
||||||
|
FsPath: filepath.FromSlash(path),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// From returns the new URI from args.
|
||||||
|
func From(scheme, authority, path, query, fragment string) *URI {
|
||||||
|
return &URI{
|
||||||
|
Scheme: scheme,
|
||||||
|
Authority: authority,
|
||||||
|
Path: path,
|
||||||
|
Query: query,
|
||||||
|
Fragment: fragment,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue