Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codable encoder key strategy update #31

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Examples/moltin iOS Example/Models/CustomProduct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CustomProduct: Product {
var backgroundColor: UIColor?

enum ProductCodingKeys: String, CodingKey {
case backgroundColor = "background_colour"
case backgroundColor = "backgroundColour"
}

required init(from decoder: Decoder) throws {
Expand Down
4 changes: 2 additions & 2 deletions Examples/moltin iOS Example/Models/ProductCategory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class ProductCategory: moltin.Category {
var backgroundImage: String?

enum ProductCategoryCodingKeys: String, CodingKey {
case backgroundColor = "background_colour"
case backgroundImage = "background_image"
case backgroundColor = "backgroundColour"
case backgroundImage
}

required init(from decoder: Decoder) throws {
Expand Down
4 changes: 2 additions & 2 deletions Examples/moltin tvOS Example/CategoriesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class ProductCategory: moltin.Category {
var backgroundImage: String?

enum ProductCategoryCodingKeys: String, CodingKey {
case backgroundColor = "background_colour"
case backgroundImage = "background_image"
case backgroundColor = "backgroundColour"
case backgroundImage
}

required init(from decoder: Decoder) throws {
Expand Down
17 changes: 0 additions & 17 deletions Sources/SDK/Models/Address.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,6 @@ open class Address: Codable {
/// The country for this address
public var country: String?

enum CodingKeys: String, CodingKey {
case firstName = "first_name"
case lastName = "last_name"
case companyName = "company_name"
case line1 = "line_1"
case line2 = "line_2"

case id
case type
case name
case instructions
case city
case county
case postcode
case country
}

/// Create a new address with first name and last name
public init(
withFirstName firstName: String,
Expand Down
28 changes: 0 additions & 28 deletions Sources/SDK/Models/Cart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import Foundation
public class CartMeta: Codable {
/// The display price information for this cart
public let displayPrice: DisplayPrices

enum CodingKeys: String, CodingKey {
case displayPrice = "display_price"
}
}

/// The display price for a `CartItem`
Expand All @@ -31,21 +27,12 @@ public struct CartItemDisplayPrices: Codable {
public let withTax: CartItemDisplayPrice
/// The display price for this cart item without tax
public let withoutTax: CartItemDisplayPrice

enum CodingKeys: String, CodingKey {
case withTax = "with_tax"
case withoutTax = "without_tax"
}
}

/// The meta information for this `CartItem`
public class CartItemMeta: Codable {
/// The display price for this cart item
public let displayPrice: CartItemDisplayPrices

enum CodingKeys: String, CodingKey {
case displayPrice = "display_price"
}
}

/// Represents a `Cart` in Moltin
Expand Down Expand Up @@ -84,21 +71,6 @@ open class CartItem: Codable {
public let links: [String: String]
/// The meta information for this cart
public let meta: CartItemMeta

enum CodingKeys: String, CodingKey {
case productId = "product_id"
case unitPrice = "unit_price"

case id
case type
case name
case description
case sku
case quantity
case value
case links
case meta
}
}

/// Represents various types of cart items
Expand Down
16 changes: 0 additions & 16 deletions Sources/SDK/Models/Currency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,4 @@ open class Currency: Codable {
public let links: [String: String]
/// The meta information for this currency
public let meta: CurrencyMeta

enum CodingKeys: String, CodingKey {
case exchangeRate = "exchange_rate"
case decimalPoint = "decimal_point"
case thousandSeparator = "thousand_separator"
case decimalPlaces = "decimal_places"

case id
case type
case code
case format
case `default`
case enabled
case links
case meta
}
}
12 changes: 0 additions & 12 deletions Sources/SDK/Models/File.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,4 @@ open class File: Codable {
public let links: [String: String]
/// The meta information for this file
public let meta: FileMeta

enum CodingKeys: String, CodingKey {
case id
case type
case fileName = "file_name"
case mimeType = "mime_type"
case fileSize = "file_size"
case `public`
case link
case links
case meta
}
}
15 changes: 0 additions & 15 deletions Sources/SDK/Models/Flow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,6 @@ open class Field: Codable {
public let relationships: Relationships?
/// The meta information for this Field
public let meta: FieldMeta?

enum CodingKeys: String, CodingKey {
case fieldType = "field_type"

case id
case type
case slug
case name
case description
case required
case unique
case enabled
case relationships
case meta
}
}

/// Represents a `Flow` in Moltin
Expand Down
19 changes: 0 additions & 19 deletions Sources/SDK/Models/Order.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ public class OrderMeta: Codable {
public let displayPrice: DisplayPrices
/// The timestamps for an order
public let timestamps: Timestamps

enum CodingKeys: String, CodingKey {
case displayPrice = "display_price"
case timestamps
}
}

/// Represents the relationships for an `Order`
Expand Down Expand Up @@ -52,20 +47,6 @@ open class Order: Codable {
public let meta: OrderMeta
/// The relationships for this order
public let relationships: OrderRelationships?

enum CodingKeys: String, CodingKey {
case id
case type
case status
case payment
case shipping
case customer
case shippingAddress = "shipping_address"
case billingAddress = "billing_address"
case links
case meta
case relationships
}
}

/// Denotes a successful order returned from the payment gateway
Expand Down
5 changes: 0 additions & 5 deletions Sources/SDK/Models/Prices.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,4 @@ public struct DisplayPrices: Codable {
public let withTax: DisplayPrice
/// The display price without tax
public let withoutTax: DisplayPrice

enum CodingKeys: String, CodingKey {
case withTax = "with_tax"
case withoutTax = "without_tax"
}
}
32 changes: 0 additions & 32 deletions Sources/SDK/Models/Product.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ public struct ProductPrice: Codable {
public let currency: String
/// Whether this price includes tax
public let includesTax: Bool

enum CodingKeys: String, CodingKey {
case includesTax = "includes_tax"

case amount
case currency
}
}

/// Represents stock levels on a `Product`
Expand Down Expand Up @@ -62,15 +55,6 @@ public class ProductMeta: Codable {
public let variations: [ProductVariation]?
/// The variation matrix of this product
public let variationMatrix: [[String: String]]?

enum CodingKeys: String, CodingKey {
case displayPrice = "display_price"
case variationMatrix = "variation_matrix"

case timestamps
case stock
case variations
}
}

/// Represents a `Product` in moltin
Expand Down Expand Up @@ -111,22 +95,6 @@ open class Product: Codable, HasRelationship {
/// The collections this product belongs to
public var collections: [Collection]?

enum CodingKeys: String, CodingKey {
case manageStock = "manage_stock"
case commodityType = "commodity_type"

case id
case type
case name
case slug
case sku
case description
case price
case status
case meta
case relationships
}

required public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let includes: IncludesContainer = decoder.userInfo[.includes] as? IncludesContainer ?? [:]
Expand Down
14 changes: 0 additions & 14 deletions Sources/SDK/Models/Relationship.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,4 @@ open class Relationships: Codable {
public var cartItem: RelationshipSingle?
/// The `Product` relationships
public var products: RelationshipMany?

enum CodingKeys: String, CodingKey {
case mainImage = "main_image"
case cartItem = "cart_item"

case files
case categories
case collections
case brands
case flow
case items
case customer
case products
}
}
5 changes: 0 additions & 5 deletions Sources/SDK/Models/Timestamps.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,4 @@ open class Timestamps: Codable {
public let createdAt: Date
/// When the resource was updated
public let updatedAt: Date?

enum CodingKeys: String, CodingKey {
case createdAt = "created_at"
case updatedAt = "updated_at"
}
}
5 changes: 4 additions & 1 deletion Sources/SDK/Requests/MoltinRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ public class MoltinRequest {
public init(withConfiguration configuration: MoltinConfig) {
self.config = configuration
self.http = MoltinHTTP(withSession: URLSession.shared)
self.parser = MoltinParser(withDecoder: JSONDecoder.dateFormattingDecoder())
self.query = MoltinQuery()
self.auth = MoltinAuth(withConfiguration: self.config)

let encoder = JSONDecoder.dateFormattingDecoder()
encoder.keyDecodingStrategy = .convertFromSnakeCase
self.parser = MoltinParser(withDecoder: encoder)
}

// MARK: - Default Calls
Expand Down
2 changes: 2 additions & 0 deletions Sources/SDK/Utils/CodableExtract.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ extension Decodable {
let itemData = try JSONSerialization.data(withJSONObject: item, options: [])

let decoder = JSONDecoder.dateFormattingDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
return try decoder.decode(T.self, from: itemData)
}

Expand All @@ -59,6 +60,7 @@ extension Decodable {
let itemData = try JSONSerialization.data(withJSONObject: items, options: [])

let decoder = JSONDecoder.dateFormattingDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
return try decoder.decode([T].self, from: itemData)
}

Expand Down
8 changes: 4 additions & 4 deletions Tests/moltin iOS Tests/AuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class AuthRequestTests: XCTestCase {

func testAuthAuthenticatesSuccessfullyAndPassesThrough() {
let (_, productRequest) = MockFactory.mockedProductRequest(withJSON: MockProductDataFactory.multiProductData)

let expectationToFulfill = expectation(description: "ProductRequest calls the method and runs the callback closure")

_ = productRequest.all { (result) in
switch result {
case .success(_):
Expand All @@ -58,10 +58,10 @@ class AuthRequestTests: XCTestCase {
}
break
}

expectationToFulfill.fulfill()
}

waitForExpectations(timeout: 1) { error in
if let error = error {
XCTFail("waitForExpectationsWithTimeout errored: \(error)")
Expand Down
Loading