github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/accounts/url.go (about) 1 package accounts 2 3 import ( 4 "encoding/json" 5 "errors" 6 "fmt" 7 "strings" 8 ) 9 10 type URL struct { 11 Scheme string 12 Path string 13 } 14 15 func parseURL(url string) (URL, error) { 16 parts := strings.Split(url, "://") 17 if len(parts) != 2 || parts[0] == "" { 18 return URL{}, errors.New("protocol scheme missing") 19 } 20 return URL{ 21 Scheme: parts[0], 22 Path: parts[1], 23 }, nil 24 } 25 26 func (u URL) String() string { 27 if u.Scheme != "" { 28 return fmt.Sprintf("%s://%s", u.Scheme, u.Path) 29 } 30 return u.Path 31 } 32 33 func (u URL) TerminalString() string { 34 url := u.String() 35 if len(url) > 32 { 36 return url[:31] + "…" 37 } 38 return url 39 } 40 41 func (u URL) MarshalJSON() ([]byte, error) { 42 return json.Marshal(u.String()) 43 } 44 45 func (u URL) Cmp(url URL) int { 46 if u.Scheme == url.Scheme { 47 return strings.Compare(u.Path, url.Path) 48 } 49 return strings.Compare(u.Scheme, url.Scheme) 50 }