 	
 	fileprivate var attributes: RecordAttributes?
 	fileprivate var container: KeyedDecodingContainer<RecordCodingKey>?
-	fileprivate var mutableFields: [String: Any?]
+	fileprivate var mutableFields: [String: Codable?]
+	
+	
 	
 	/// Record ID
 	public var id: String? {

		return attributes?.path
	}
	
	subscript<T: Decodable>(fieldName: String) -> T? {
		
		get {
			if mutableFields.keys.contains(fieldName) {
				return mutableFields[fieldName] as? T
			}
			else {
				if let c = container, let key = RecordCodingKey(stringValue: fieldName) {
					return try? c.decode(T.self, forKey: key)
				}
				else {
					return nil
				}
			}
		}
		
		set {
			mutableFields[fieldName] = newValue
		}
	}
	
	init(fields: [String: Any?] = [String: Any?]()) {
		self.mutableFields = fields 
	}
}

	public init(from decoder: Decoder) throws {
		let cont = try decoder.container(keyedBy: RecordCodingKey.self)
		self.container = cont
		self.attributes = try cont.decodeIfPresent(RecordAttributes.self, forKey: RecordCodingKey(stringValue: "attributes")!)
		self.mutableFields = [String: Codable?]()
	}
	
	public func encode(to encoder: Encoder) throws {
		var container = encoder.container(keyedBy: RecordCodingKey.self)
		for field in mutableFields {
			let codingKey = RecordCodingKey(stringValue: field.key)!
			if let value = mutableFields[field.key], value != nil {
				if let v = value as? Bool {
					try container.encode(v, forKey: codingKey)
				}
				else if let v = value as? Date {
					try container.encode(v, forKey: codingKey)
				}
				else if let v = value as? Double {
					try container.encode(v, forKey: codingKey)
				}
				else if let v = value as? Float {
					try container.encode(v, forKey: codingKey)
				}
				else if let v = value as? Int {
					try container.encode(v, forKey: codingKey)
				}
				else if let v = value as? String {
					try container.encode(v, forKey: codingKey)
				}
				else if let v = value as? UInt {
					try container.encode(v, forKey: codingKey)
				}
				else if let v = value as? URL {
					try container.encode(v, forKey: codingKey)
				}
				else {
					// Encode as a string if nothing else
					try container.encode(String(describing: value), forKey: codingKey)
				}
			}
			else {
				try container.encodeNil(forKey: codingKey)
			}
		}
	}
}
