 		return value(forField: field)
 	}
 	
+	public func address(forField field: String) -> Address? {
+		return value(forField: field)
+	}
+	
+	public func subqueryResult(forField field: String) -> QueryResult<Record>? {
+		return value(forField: field)
+	}
+	
+	/// Returns the value for the given field
+	public func value<Value: Decodable>(forField field: String) -> Value? {
+		if mutableFields.keys.contains(field) {
+			return mutableFields[field] as? Value
+		}
+		else {
+			if let c = container, let key = RecordCodingKey(stringValue: field) {
+				return try? c.decode(Value.self, forKey: key)
+			}
+			else {
+				return nil
+			}
+		}
+	}
+	
 	/// Sets the value for the given field
-	public mutating func setValue(_ value: Codable?, forField field: String) {
+	public mutating func setValue(_ value: Encodable?, forField field: String) {
 		mutableFields.updateValue(value, forKey: field)
 	}
 	
-	public init(fields: [String: Codable?] = [String: Codable?]()) {
+	public init(type: String, id: String? = nil, fields: [String: Encodable?] = [String: Encodable?]()) {
+		self.type = type
+		self.id = id 
 		self.mutableFields = fields 
 	}
 }

extension Record: Codable {
	
	fileprivate struct RecordCodingKey: CodingKey {
		
		var stringValue: String
		var intValue: Int? = nil
		
		init?(stringValue: String) {
			self.stringValue = stringValue
		}
		
		init?(intValue: Int) {
			return nil
		}
	}
	
	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 {

//  Copyright (c) 2017. All rights reserved.
//

/// Represents a generic record of a Salesforce object
public struct Record {
	
	/// Salesforce type corresponding to this record
	public var type: String
	
	/// Salesforce record ID
	public var id: String?
	
	fileprivate var container: KeyedDecodingContainer<RecordCodingKey>?
	fileprivate var mutableFields: [String: Encodable?]
	
	/// Subscript by field name; read only
	subscript<Value: Decodable>(field: String) -> Value? {
		return value(forField: field)
	}
	
	/// Returns the value for the given field as a String
