parse.go 955 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package util
  2. import (
  3. "net/url"
  4. "strconv"
  5. "strings"
  6. )
  7. func ParseInt(text string, defaultValue int) int {
  8. count, parseError := strconv.ParseInt(text, 10, 64)
  9. if parseError != nil {
  10. if len(text) > 0 {
  11. return 0
  12. }
  13. return defaultValue
  14. }
  15. return int(count)
  16. }
  17. func ParseUint64(text string, defaultValue uint64) uint64 {
  18. count, parseError := strconv.ParseUint(text, 10, 64)
  19. if parseError != nil {
  20. if len(text) > 0 {
  21. return 0
  22. }
  23. return defaultValue
  24. }
  25. return count
  26. }
  27. func ParseFilerUrl(entryPath string) (filerServer string, filerPort int64, path string, err error) {
  28. if !strings.HasPrefix(entryPath, "http://") && !strings.HasPrefix(entryPath, "https://") {
  29. entryPath = "http://" + entryPath
  30. }
  31. var u *url.URL
  32. u, err = url.Parse(entryPath)
  33. if err != nil {
  34. return
  35. }
  36. filerServer = u.Hostname()
  37. portString := u.Port()
  38. if portString != "" {
  39. filerPort, err = strconv.ParseInt(portString, 10, 32)
  40. }
  41. path = u.Path
  42. return
  43. }