summaryrefslogtreecommitdiff
path: root/lib/cocoa/src/TProtocol.swift
blob: 1775849790ac4b3a5b7809f5a3586e25436ca5f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import Foundation


public extension TProtocol {
  
  public func readMessageBegin() throws -> (String, TMessageType, Int) {
    
    var name : NSString?
    var type : Int32 = -1
    var sequenceID : Int32 = -1
    
    try readMessageBeginReturningName(&name, type: &type, sequenceID: &sequenceID)
    
    return (name as String!, TMessageType(rawValue: type)!, Int(sequenceID))
  }
  
  public func writeMessageBeginWithName(name: String, type: TMessageType, sequenceID: Int) throws {
    try writeMessageBeginWithName(name, type: type.rawValue, sequenceID: Int32(sequenceID))
  }
  
  public func readStructBegin() throws -> (String?) {
    
    var name : NSString? = nil
    
    try readStructBeginReturningName(&name)
    
    return (name as String?)
  }
  
  public func readFieldBegin() throws -> (String?, TType, Int) {
    
    var name : NSString? = nil
    var type : Int32 = -1
    var fieldID : Int32 = -1
    
    try readFieldBeginReturningName(&name, type: &type, fieldID: &fieldID)
    
    return (name as String?, TType(rawValue: type)!, Int(fieldID))
  }
  
  public func writeFieldBeginWithName(name: String, type: TType, fieldID: Int) throws {
    try writeFieldBeginWithName(name, type: type.rawValue, fieldID: Int32(fieldID))
  }
  
  public func readMapBegin() throws -> (TType, TType, Int32) {
    
    var keyType : Int32 = -1
    var valueType : Int32 = -1
    var size : Int32 = 0
    
    try readMapBeginReturningKeyType(&keyType, valueType: &valueType, size: &size)
    
    return (TType(rawValue: keyType)!, TType(rawValue: valueType)!, size)
  }
  
  public func writeMapBeginWithKeyType(keyType: TType, valueType: TType, size: Int) throws {
    try writeMapBeginWithKeyType(keyType.rawValue, valueType: valueType.rawValue, size: Int32(size))
  }
  
  public func readSetBegin() throws -> (TType, Int32) {
    
    var elementType : Int32 = -1
    var size : Int32 = 0
    
    try readSetBeginReturningElementType(&elementType, size: &size)
    
    return (TType(rawValue: elementType)!, size)
  }
  
  public func writeSetBeginWithElementType(elementType: TType, size: Int) throws {
    try writeSetBeginWithElementType(elementType.rawValue, size: Int32(size))
  }
  
  public func readListBegin() throws -> (TType, Int32) {
    
    var elementType : Int32 = -1
    var size : Int32 = 0
    
    try readListBeginReturningElementType(&elementType, size: &size)
    
    return (TType(rawValue: elementType)!, size)
  }
  
  public func writeListBeginWithElementType(elementType: TType, size: Int) throws {
    try writeListBeginWithElementType(elementType.rawValue, size: Int32(size))
  }
  
  public func writeFieldValue<T: TSerializable>(value: T, name: String, type: TType, id: Int32) throws {
    try writeFieldBeginWithName(name, type: type.rawValue, fieldID: id)
    try writeValue(value)
    try writeFieldEnd()
  }
  
  public func readValue<T: TSerializable>() throws -> T {
    return try T.readValueFromProtocol(self)
  }
  
  public func writeValue<T: TSerializable>(value: T) throws {
    try T.writeValue(value, toProtocol: self)
  }
  
  public func readResultMessageBegin() throws {
    
    let (_, type, _) = try readMessageBegin();
    
    if type == .EXCEPTION {
      let x = try readException()
      throw x
    }
    
    return
  }
  
  public func validateValue(value: Any?, named name: String) throws {
    
    if value == nil {
      throw NSError(
        domain: TProtocolErrorDomain,
        code: Int(TProtocolError.Unknown.rawValue),
        userInfo: [TProtocolErrorFieldNameKey: name])
    }
    
  }
  
  public func readException() throws -> ErrorType {
    
    var reason : String?
    var type = TApplicationError.Unknown
    
    try readStructBegin()
    
    fields: while (true) {
      
      let (_, fieldType, fieldID) = try readFieldBegin()
      
      switch (fieldID, fieldType) {
      case (_, .STOP):
        break fields
        
      case (1, .STRING):
        reason = try readValue() as String
        
      case (2, .I32):
        let typeVal = try readValue() as Int32
        if let tmp = TApplicationError(rawValue: typeVal) {
          type = tmp
        }
        
      case let (_, unknownType):
        try skipType(unknownType)
      }
      
      try readFieldEnd()
    }
    
    try readStructEnd()
    
    return NSError(type:type, reason:reason ?? "")
  }
  
  public func writeExceptionForMessageName(name: String, sequenceID: Int, ex: NSError) throws {
    try writeMessageBeginWithName(name, type: .EXCEPTION, sequenceID: sequenceID)
    try ex.write(self)
    try writeMessageEnd()
  }
  
  public func skipType(type: TType) throws {
    try TProtocolUtil.skipType(type.rawValue, onProtocol: self)
  }
  
}