mirror of
https://github.com/laravel-ls/uri
synced 2026-07-04 16:34:11 +02:00
uri: fix MarshalJSON and UnmarshalJSON implements
This commit is contained in:
parent
2acea58812
commit
f880ad597a
1 changed files with 55 additions and 89 deletions
134
uri.go
134
uri.go
|
|
@ -10,6 +10,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"golang.org/x/xerrors"
|
"golang.org/x/xerrors"
|
||||||
)
|
)
|
||||||
|
|
@ -25,6 +27,15 @@ const (
|
||||||
HTTPSScheme = "https"
|
HTTPSScheme = "https"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
keyAuthority = "authority"
|
||||||
|
keyFragment = "fragment"
|
||||||
|
keyFsPath = "fsPath"
|
||||||
|
keyPath = "path"
|
||||||
|
keyQuery = "query"
|
||||||
|
keyScheme = "scheme"
|
||||||
|
)
|
||||||
|
|
||||||
// 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
|
||||||
|
|
@ -84,105 +95,60 @@ type URI struct {
|
||||||
|
|
||||||
// MarshalJSON implements json.Marshaler.
|
// MarshalJSON implements json.Marshaler.
|
||||||
func (u *URI) MarshalJSON() ([]byte, error) {
|
func (u *URI) MarshalJSON() ([]byte, error) {
|
||||||
buf := bytes.NewBuffer(make([]byte, 0))
|
buf := new(bytes.Buffer)
|
||||||
|
|
||||||
buf.WriteString("{")
|
buf.WriteString("{")
|
||||||
|
|
||||||
comma := false
|
buf.WriteString(`"` + strconv.Quote(keyAuthority) + `: "`)
|
||||||
// "Authority" field is required
|
authority, err := json.Marshal(u.Authority)
|
||||||
// only required object types supported for marshal checking (for now)
|
if err != nil {
|
||||||
// Marshal the "authority" field
|
|
||||||
if comma {
|
|
||||||
buf.WriteString(",")
|
|
||||||
}
|
|
||||||
|
|
||||||
buf.WriteString("\"authority\": ")
|
|
||||||
if tmp, err := json.Marshal(u.Authority); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
|
||||||
buf.Write(tmp)
|
|
||||||
}
|
}
|
||||||
|
buf.Write(authority)
|
||||||
|
|
||||||
comma = true
|
|
||||||
// "Fragment" field is required
|
|
||||||
// only required object types supported for marshal checking (for now)
|
|
||||||
// Marshal the "fragment" field
|
|
||||||
if comma {
|
|
||||||
buf.WriteString(",")
|
buf.WriteString(",")
|
||||||
}
|
buf.WriteString(`"` + strconv.Quote(keyFragment) + `: "`)
|
||||||
|
fragment, err := json.Marshal(u.Fragment)
|
||||||
buf.WriteString("\"fragment\": ")
|
if err != nil {
|
||||||
if tmp, err := json.Marshal(u.Fragment); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
|
||||||
buf.Write(tmp)
|
|
||||||
}
|
}
|
||||||
|
buf.Write(fragment)
|
||||||
|
|
||||||
comma = true
|
|
||||||
// "FsPath" field is required
|
|
||||||
// only required object types supported for marshal checking (for now)
|
|
||||||
// Marshal the "fsPath" field
|
|
||||||
if comma {
|
|
||||||
buf.WriteString(",")
|
buf.WriteString(",")
|
||||||
}
|
buf.WriteString(`"` + strconv.Quote(keyFsPath) + `: "`)
|
||||||
|
fsPath, err := json.Marshal(u.FsPath)
|
||||||
buf.WriteString("\"fsPath\": ")
|
if err != nil {
|
||||||
if tmp, err := json.Marshal(u.FsPath); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
|
||||||
buf.Write(tmp)
|
|
||||||
}
|
}
|
||||||
|
buf.Write(fsPath)
|
||||||
|
|
||||||
comma = true
|
|
||||||
// "Path" field is required
|
|
||||||
// only required object types supported for marshal checking (for now)
|
|
||||||
// Marshal the "path" field
|
|
||||||
if comma {
|
|
||||||
buf.WriteString(",")
|
buf.WriteString(",")
|
||||||
}
|
buf.WriteString(`"` + strconv.Quote(keyPath) + `: "`)
|
||||||
|
path, err := json.Marshal(u.Path)
|
||||||
buf.WriteString("\"path\": ")
|
if err != nil {
|
||||||
if tmp, err := json.Marshal(u.Path); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
|
||||||
buf.Write(tmp)
|
|
||||||
}
|
}
|
||||||
|
buf.Write(path)
|
||||||
|
|
||||||
comma = true
|
|
||||||
// "Query" field is required
|
|
||||||
// only required object types supported for marshal checking (for now)
|
|
||||||
// Marshal the "query" field
|
|
||||||
if comma {
|
|
||||||
buf.WriteString(",")
|
buf.WriteString(",")
|
||||||
}
|
buf.WriteString(`"` + strconv.Quote(keyQuery) + `: "`)
|
||||||
|
query, err := json.Marshal(u.Query)
|
||||||
buf.WriteString("\"query\": ")
|
if err != nil {
|
||||||
if tmp, err := json.Marshal(u.Query); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
|
||||||
buf.Write(tmp)
|
|
||||||
}
|
}
|
||||||
|
buf.Write(query)
|
||||||
|
|
||||||
comma = true
|
|
||||||
// "Scheme" field is required
|
|
||||||
// only required object types supported for marshal checking (for now)
|
|
||||||
// Marshal the "scheme" field
|
|
||||||
if comma {
|
|
||||||
buf.WriteString(",")
|
buf.WriteString(",")
|
||||||
}
|
buf.WriteString(`"` + strconv.Quote(keyScheme) + `: "`)
|
||||||
|
scheme, err := json.Marshal(u.Scheme)
|
||||||
buf.WriteString("\"scheme\": ")
|
if err != nil {
|
||||||
if tmp, err := json.Marshal(u.Scheme); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
|
||||||
buf.Write(tmp)
|
|
||||||
}
|
}
|
||||||
|
buf.Write(scheme)
|
||||||
comma = true
|
|
||||||
|
|
||||||
buf.WriteString("}")
|
buf.WriteString("}")
|
||||||
rv := buf.Bytes()
|
|
||||||
|
|
||||||
return rv, nil
|
return buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalJSON implements json.Unmarshaler.
|
// UnmarshalJSON implements json.Unmarshaler.
|
||||||
|
|
@ -202,37 +168,37 @@ func (u *URI) UnmarshalJSON(b []byte) error {
|
||||||
// parse all the defined properties
|
// parse all the defined properties
|
||||||
for k, v := range jm {
|
for k, v := range jm {
|
||||||
switch k {
|
switch k {
|
||||||
case "authority":
|
case keyAuthority:
|
||||||
if err := json.Unmarshal([]byte(v), &u.Authority); err != nil {
|
if err := json.Unmarshal([]byte(v), &u.Authority); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
authorityReceived = true
|
authorityReceived = true
|
||||||
|
|
||||||
case "fragment":
|
case keyFragment:
|
||||||
if err := json.Unmarshal([]byte(v), &u.Fragment); err != nil {
|
if err := json.Unmarshal([]byte(v), &u.Fragment); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fragmentReceived = true
|
fragmentReceived = true
|
||||||
|
|
||||||
case "fsPath":
|
case keyFsPath:
|
||||||
if err := json.Unmarshal([]byte(v), &u.FsPath); err != nil {
|
if err := json.Unmarshal([]byte(v), &u.FsPath); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fsPathReceived = true
|
fsPathReceived = true
|
||||||
|
|
||||||
case "path":
|
case keyPath:
|
||||||
if err := json.Unmarshal([]byte(v), &u.Path); err != nil {
|
if err := json.Unmarshal([]byte(v), &u.Path); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
pathReceived = true
|
pathReceived = true
|
||||||
|
|
||||||
case "query":
|
case keyQuery:
|
||||||
if err := json.Unmarshal([]byte(v), &u.Query); err != nil {
|
if err := json.Unmarshal([]byte(v), &u.Query); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
queryReceived = true
|
queryReceived = true
|
||||||
|
|
||||||
case "scheme":
|
case keyScheme:
|
||||||
if err := json.Unmarshal([]byte(v), &u.Scheme); err != nil {
|
if err := json.Unmarshal([]byte(v), &u.Scheme); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -245,32 +211,32 @@ func (u *URI) UnmarshalJSON(b []byte) error {
|
||||||
|
|
||||||
// check if authority (a required property) was received
|
// check if authority (a required property) was received
|
||||||
if !authorityReceived {
|
if !authorityReceived {
|
||||||
return xerrors.New("\"authority\" is required but was not present")
|
return xerrors.Errorf("%q is required but was not present", keyAuthority)
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if fragment (a required property) was received
|
// check if fragment (a required property) was received
|
||||||
if !fragmentReceived {
|
if !fragmentReceived {
|
||||||
return xerrors.New("\"fragment\" is required but was not present")
|
return xerrors.Errorf("%q is required but was not present", keyFragment)
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if fsPath (a required property) was received
|
// check if fsPath (a required property) was received
|
||||||
if !fsPathReceived {
|
if !fsPathReceived {
|
||||||
return xerrors.New("\"fsPath\" is required but was not present")
|
return xerrors.Errorf("%q is required but was not present", keyFsPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if path (a required property) was received
|
// check if path (a required property) was received
|
||||||
if !pathReceived {
|
if !pathReceived {
|
||||||
return xerrors.New("\"path\" is required but was not present")
|
return xerrors.Errorf("%q is required but was not present", keyPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if query (a required property) was received
|
// check if query (a required property) was received
|
||||||
if !queryReceived {
|
if !queryReceived {
|
||||||
return xerrors.New("\"query\" is required but was not present")
|
return xerrors.Errorf("%q is required but was not present", keyQuery)
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if scheme (a required property) was received
|
// check if scheme (a required property) was received
|
||||||
if !schemeReceived {
|
if !schemeReceived {
|
||||||
return xerrors.New("\"scheme\" is required but was not present")
|
return xerrors.Errorf("%q is required but was not present", keyScheme)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue