Skip to content

Commit

Permalink
[F] Alter pointerValue to be a JSONSubscriptType.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominic Frei committed Jan 21, 2016
1 parent 255eb8b commit b13d363
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
14 changes: 8 additions & 6 deletions JsonPatchSwift/JPSJsonPatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,15 @@ extension JPSJsonPatch {
throw JPSJsonPatchInitialisationError.InvalidPatchFormat(message: "Operation '\(operation)' is invalid.")
}

let pointer = try JPSJsonPointer(rawValue: path)

switch JPSOperation.JPSOperationType(rawValue: operation)! {
case .Add: return JPSOperation(type: JPSOperation.JPSOperationType.Add)
case .Remove: return JPSOperation(type: JPSOperation.JPSOperationType.Remove)
case .Replace: return JPSOperation(type: JPSOperation.JPSOperationType.Replace)
case .Move: return JPSOperation(type: JPSOperation.JPSOperationType.Move)
case .Copy: return JPSOperation(type: JPSOperation.JPSOperationType.Copy)
case .Test: return JPSOperation(type: JPSOperation.JPSOperationType.Test)
case .Add: return JPSOperation(type: JPSOperation.JPSOperationType.Add, pointer: pointer)
case .Remove: return JPSOperation(type: JPSOperation.JPSOperationType.Remove, pointer: pointer)
case .Replace: return JPSOperation(type: JPSOperation.JPSOperationType.Replace, pointer: pointer)
case .Move: return JPSOperation(type: JPSOperation.JPSOperationType.Move, pointer: pointer)
case .Copy: return JPSOperation(type: JPSOperation.JPSOperationType.Copy, pointer: pointer)
case .Test: return JPSOperation(type: JPSOperation.JPSOperationType.Test, pointer: pointer)
}
}

Expand Down
10 changes: 5 additions & 5 deletions JsonPatchSwift/JPSJsonPointer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ enum JPSJsonPointerError: ErrorType {
struct JPSJsonPointer {

let rawValue: String
let pointerValue: [String]
let pointerValue: [JSONSubscriptType]

}

Expand All @@ -39,7 +39,7 @@ extension JPSJsonPointer {

let pointerValueWithoutFirstDelimiter = Array(rawValue.componentsSeparatedByString("/").dropFirst())
let pointerValueAfterDecodingDelimiter = pointerValueWithoutFirstDelimiter.map { $0.stringByReplacingOccurrencesOfString("~1", withString: "/") }
let pointerValue = pointerValueAfterDecodingDelimiter.map { $0.stringByReplacingOccurrencesOfString("~0", withString: "~") }
let pointerValue: [JSONSubscriptType] = pointerValueAfterDecodingDelimiter.map { $0.stringByReplacingOccurrencesOfString("~0", withString: "~")}

self.init(rawValue: rawValue, pointerValue: pointerValue)
}
Expand All @@ -57,10 +57,10 @@ extension JPSJsonPointer {

for i in 0..<pointer.pointerValue.count {
if tempJson.type == .Array {
if "-" == pointer.pointerValue[i] {
if let value = pointer.pointerValue[i] as? String where "-" == value {
tempJson = tempJson.arrayValue.last!
} else {
tempJson = tempJson[Int(pointer.pointerValue[i])!]
} else if let value = pointer.pointerValue[i] as? Int {
tempJson = tempJson[value]
}
} else {
tempJson = tempJson[pointer.pointerValue[i]]
Expand Down

0 comments on commit b13d363

Please sign in to comment.