This repository has been archived by the owner on Apr 16, 2019. It is now read-only.
forked from NET-A-PORTER/scala-uri
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Converted `Uri` to abstract, and created case classes that extend `Uri` to allow easier validation. Extracted `Authority` and `UserInfo` classes from the UriParser to be used fully. Changed `PathPart` to `Segment`. This is more consistent with the RFC. Also, "path" and "part" were too similar and being confused. Changed `QueryString` to `Query`. This is more consistent with the RFC. Created `Scheme` and `Fragment` classes. This seemed more consistent with the rest of the structure and eased validation. Created `Parameter` class. This was mentioned in issue NET-A-PORTER#34 and eased validation. Consistency changes throughout. Deprecated as much as possible to ease transition of existing users. Started updating the README, not complete. No DSL changes (aside from compatibility changes) as this will be completed as a separate task.
- Loading branch information
1 parent
818d220
commit 362193d
Showing
55 changed files
with
6,425 additions
and
1,277 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 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,43 @@ | ||
package com.netaporter.uri | ||
|
||
import com.netaporter.uri.config.UriConfig | ||
|
||
sealed abstract case class Authority(userInfo: Option[UserInfo], host: String, port: Option[Int]) { | ||
|
||
def user: Option[String] = userInfo.map(_.user) | ||
|
||
def password: Option[String] = userInfo.flatMap(_.password) | ||
|
||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
|
||
def copy(userInfo: Option[UserInfo] = userInfo, host: String = host, port: Option[Int] = port): Authority = Authority(userInfo, host, port) | ||
|
||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
|
||
def toString(implicit c: UriConfig): String = | ||
"//" + userInfo.map(_.toString).getOrElse("") + host + port.map(":" + _).getOrElse("") | ||
|
||
def toStringRaw(implicit c: UriConfig): String = toString(c.withNoEncoding) | ||
} | ||
|
||
object Authority { | ||
|
||
def apply(userInfo: Option[UserInfo], host: String, port: Option[Int]): Authority = { | ||
if (host == null || host.isEmpty) throw new IllegalArgumentException("`host` cannot be `null`.") | ||
if (port.nonEmpty && port.exists(port => port < 1 || port > 65535)) throw new IllegalArgumentException("Invalid `port`. [" + port + "]") | ||
new Authority(userInfo, host, port) {} | ||
} | ||
|
||
def apply(user: String = null, password: String = null, host: String, port: Int = 0): Authority = | ||
apply(UserInfo.option(user, password), host, if (port == 0) None else Option(port)) | ||
|
||
def option(userInfo: Option[UserInfo], host: String, port: Option[Int]): Option[Authority] = { | ||
if (host == null || host.isEmpty) { | ||
if (userInfo.nonEmpty || port.nonEmpty) throw new IllegalArgumentException("`host` cannot be `null`.") | ||
None | ||
} else Option(apply(userInfo, host, port)) | ||
} | ||
|
||
def option(user: String = null, password: String = null, host: String, port: Int = 0): Option[Authority] = | ||
option(UserInfo.option(user, password), host, if (port == 0) None else Option(port)) | ||
} |
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,31 @@ | ||
package com.netaporter.uri | ||
|
||
import com.netaporter.uri.config.UriConfig | ||
|
||
sealed abstract case class Fragment(fragment: String) { | ||
|
||
def copy(fragment: String = fragment): Fragment = Fragment(fragment) | ||
|
||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
|
||
def toString(implicit c: UriConfig): String = | ||
"#" + c.fragmentEncoder.encode(fragment, c.charset) | ||
|
||
def toStringRaw(implicit c: UriConfig): String = toString(c.withNoEncoding) | ||
} | ||
|
||
// TODO: Make `Fragment` abstract, and add `StringFragment` and `ParameterFragment`? GitHub Issue 14; https://en.wikipedia.org/wiki/Fragment_identifier | ||
|
||
object Fragment { | ||
|
||
def apply(fragment: String): Fragment = { | ||
if (fragment == null) throw new IllegalArgumentException("`fragment` cannot be `null`.") | ||
if (fragment == "") EmptyFragment else new Fragment(fragment) {} | ||
} | ||
|
||
def option(fragment: String): Option[Fragment] = { | ||
if (fragment == null) None else Option(apply(fragment)) | ||
} | ||
} | ||
|
||
object EmptyFragment extends Fragment("") |
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,37 @@ | ||
package com.netaporter.uri | ||
|
||
import com.netaporter.uri.encoding.UriEncoder | ||
|
||
/** | ||
* Regular Expression used to replace existing `"???" -> Some("???")`: | ||
* ("[^"]+") -> ("[^"]*"|None|Some\("[^"]*"\)|Option\("[^"]*"\)) | ||
* Parameter($1, $2) | ||
*/ | ||
sealed abstract case class Parameter(key: String, value: Option[String]) { | ||
|
||
def mapKey(f: String => String): Parameter = Parameter(f(key), value) | ||
|
||
def mapValue(f: Option[String] => Option[String]): Parameter = Parameter(key, f(value)) | ||
|
||
def copy(key: String = key, value: Option[String] = value): Parameter = Parameter(key, value) | ||
|
||
def withValue(newValue: String): Parameter = Parameter(key, newValue) | ||
|
||
def withValue(newValue: Option[String]): Parameter = Parameter(key, newValue) | ||
|
||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
|
||
def toString(encoder: UriEncoder, charset: String): String = | ||
encoder.encode(key, charset) + value.map("=" + encoder.encode(_, charset)).getOrElse("") | ||
} | ||
|
||
object Parameter { | ||
|
||
def apply(key: String, value: Option[String] = None): Parameter = { | ||
if (key == null || key.isEmpty) throw new IllegalArgumentException("`key` cannot be `null` and cannot be empty.") | ||
if (value == null) throw new IllegalArgumentException("`value` cannot be `null`.") | ||
new Parameter(key, value) {} | ||
} | ||
|
||
def apply(key: String, value: String): Parameter = apply(key, Option(value)) | ||
} |
Oops, something went wrong.