Skip to content

Commit

Permalink
Support Level 3 Operators
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdeem committed May 1, 2018
1 parent 4062b66 commit db5ded8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
6 changes: 6 additions & 0 deletions Source/Internal/Components.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ internal struct ExpressionComponent : Component {
guard let encodedValue = value.addingPercentEncoding(withAllowedCharacters: configuration.percentEncodingAllowedCharacterSet) else {
throw URITemplate.Error.expansionFailure(position: templatePosition, reason: "Failed expanding variable \"\(variableName)\": Percent Encoding Failed")
}
if (configuration.named) {
if (encodedValue.isEmpty && configuration.omittOrphanedEquals) {
return String(variableName)
}
return "\(variableName)=\(encodedValue)"
}
return encodedValue
}
if (expansions.count == 0) {
Expand Down
2 changes: 2 additions & 0 deletions Source/Internal/ExpansionConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ internal struct ExpansionConfiguration {
let percentEncodingAllowedCharacterSet: CharacterSet
let prefix: String?
let separator: String
let named: Bool
let omittOrphanedEquals: Bool
}
47 changes: 44 additions & 3 deletions Source/Internal/ExpressionOperator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,62 @@ internal enum ExpressionOperator : Unicode.Scalar {
case simple = "\0"
case reserved = "+"
case fragment = "#"
case label = "."
case pathSegment = "/"
case pathStyle = ";"
case query = "?"
case queryContinuation = "&"

func expansionConfiguration() -> ExpansionConfiguration {
switch self {
case .simple:
return ExpansionConfiguration(percentEncodingAllowedCharacterSet:unreservedCharacterSet,
prefix:nil,
separator:",")
separator:",",
named:false,
omittOrphanedEquals:false)
case .reserved:
return ExpansionConfiguration(percentEncodingAllowedCharacterSet:reservedAndUnreservedCharacterSet,
prefix:nil,
separator:",")
separator:",",
named:false,
omittOrphanedEquals:false)
case .fragment:
return ExpansionConfiguration(percentEncodingAllowedCharacterSet:reservedAndUnreservedCharacterSet,
prefix:"#",
separator:",")
separator:",",
named:false,
omittOrphanedEquals:false)
case .label:
return ExpansionConfiguration(percentEncodingAllowedCharacterSet:unreservedCharacterSet,
prefix:".",
separator:".",
named:false,
omittOrphanedEquals:false)
case .pathSegment:
return ExpansionConfiguration(percentEncodingAllowedCharacterSet:unreservedCharacterSet,
prefix:"/",
separator:"/",
named:false,
omittOrphanedEquals:false)
case .pathStyle:
return ExpansionConfiguration(percentEncodingAllowedCharacterSet:unreservedCharacterSet,
prefix:";",
separator:";",
named:true,
omittOrphanedEquals:true)
case .query:
return ExpansionConfiguration(percentEncodingAllowedCharacterSet:unreservedCharacterSet,
prefix:"?",
separator:"&",
named:true,
omittOrphanedEquals:false)
case .queryContinuation:
return ExpansionConfiguration(percentEncodingAllowedCharacterSet:unreservedCharacterSet,
prefix:"&",
separator:"&",
named:true,
omittOrphanedEquals:false)
}
}
}

0 comments on commit db5ded8

Please sign in to comment.