Skip to content

Commit

Permalink
Rename memberwise
Browse files Browse the repository at this point in the history
* Change it to `defaultCodable`
* Make it the default instead of `semverString`
  • Loading branch information
chriseplettsonos committed Nov 13, 2023
1 parent 802f676 commit 9048ff1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,20 @@ let dict = [ // [{major 3, minor 0, patch 0,...
]

// SemanticVersion is Codable
// Note: the strategy defaults to `.semverString`
// Note: the strategy defaults to `.defaultCodable`
let defaultEncoder = JSONEncoder()
defaultEncoder.semanticVersionEncodingStrategy = .defaultCodable
let defaultDecoder = JSONDecoder()
defaultDecoder.semanticVersionDecodingStrategy = .defaultCodable
let defaultData = try defaultEncoder.encode(v123) // 58 bytes
let defaultDecoded = try defaultDecoder.decode(SemanticVersion.self, from: defaultData) // 1.2.3
defaultDecoded == v123 // true

let stringEncoder = JSONEncoder()
stringEncoder.semanticVersionEncodingStrategy = .semverString
let stringDecoder = JSONDecoder()
stringDecoder.semanticVersionDecodingStrategy = .semverString
let stringData = try stringEncoder.encode(v123) // 7 bytes -> "1.2.3", including quotes
let stringDecoded = try stringDecoder.decode(SemanticVersion.self, from: stringData) // 1.2.3
stringDecoded == v123 // true

let memberwiseEncoder = JSONEncoder()
memberwiseEncoder.semanticVersionEncodingStrategy = .memberwise
let memberwiseDecoder = JSONDecoder()
memberwiseDecoder.semanticVersionDecodingStrategy = .memberwise
let memberwiseData = try memberwiseEncoder.encode(v123) // 58 bytes
let memberwiseDecoded = try memberwiseDecoder.decode(SemanticVersion.self, from: memberwiseData) // 1.2.3
memberwiseDecoded == v123 // true
```
10 changes: 5 additions & 5 deletions Sources/SemanticVersion/SemanticVersion+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import Foundation

public enum SemanticVersionStrategy {
/// Encode/decode the `SemanticVersion` as a structure to/from a JSON object
case memberwise
case defaultCodable
/// Encode/decode the `SemanticVersion` to/fromfrom a string that conforms to the
/// semantic version 2.0 specification at https://semver.org.
case semverString
}

extension JSONEncoder {
/// The strategy to use in decoding semantic versions. Defaults to `.semverString`.
/// The strategy to use in decoding semantic versions. Defaults to `.defaultCodable`.
public var semanticVersionEncodingStrategy: SemanticVersionStrategy {
get { userInfo.semanticDecodingStrategy }
set { userInfo.semanticDecodingStrategy = newValue }
Expand All @@ -41,7 +41,7 @@ extension JSONDecoder {
private extension [CodingUserInfoKey: Any] {
var semanticDecodingStrategy: SemanticVersionStrategy {
get {
(self[.semanticVersionStrategy] as? SemanticVersionStrategy) ?? .semverString
(self[.semanticVersionStrategy] as? SemanticVersionStrategy) ?? .defaultCodable
}
set {
self[.semanticVersionStrategy] = newValue
Expand All @@ -64,7 +64,7 @@ extension SemanticVersion: Codable {

public init(from decoder: Decoder) throws {
switch decoder.userInfo.semanticDecodingStrategy {
case .memberwise:
case .defaultCodable:
let container = try decoder.container(keyedBy: CodingKeys.self)
self.major = try container.decode(Int.self, forKey: .major)
self.minor = try container.decode(Int.self, forKey: .minor)
Expand All @@ -87,7 +87,7 @@ extension SemanticVersion: Codable {

public func encode(to encoder: Encoder) throws {
switch encoder.userInfo.semanticDecodingStrategy {
case .memberwise:
case .defaultCodable:
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(major, forKey: .major)
try container.encode(minor, forKey: .minor)
Expand Down
14 changes: 7 additions & 7 deletions Tests/SemanticVersionTests/SemanticVersionCodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import XCTest
import SemanticVersion

final class SemanticVersionCodingTests: XCTestCase {
func test_semverString_is_default() throws {
XCTAssertEqual(.semverString, JSONEncoder().semanticVersionEncodingStrategy)
XCTAssertEqual(.semverString, JSONDecoder().semanticVersionDecodingStrategy)
func test_defaultCodable_is_default() throws {
XCTAssertEqual(.defaultCodable, JSONEncoder().semanticVersionEncodingStrategy)
XCTAssertEqual(.defaultCodable, JSONDecoder().semanticVersionDecodingStrategy)
}

func test_encodable_semverString() throws {
Expand All @@ -34,11 +34,11 @@ final class SemanticVersionCodingTests: XCTestCase {
XCTAssertEqual(actual, #""7.7.7-beta.423+build.17""#)
}

func test_encodable_memberwise() throws {
func test_encodable_defaultCodable() throws {
let encoder = JSONEncoder()
var actual: String

encoder.semanticVersionEncodingStrategy = .memberwise
encoder.semanticVersionEncodingStrategy = .defaultCodable

actual = String(data: try encoder.encode(SemanticVersion(1, 2, 3)), encoding: .utf8)!
XCTAssertTrue(actual.contains(#""major":1"#))
Expand Down Expand Up @@ -109,11 +109,11 @@ final class SemanticVersionCodingTests: XCTestCase {
}
}

func test_decodable_memberwise() throws {
func test_decodable_defaultCodable() throws {
let decoder = JSONDecoder()
var json: Data

decoder.semanticVersionDecodingStrategy = .memberwise
decoder.semanticVersionDecodingStrategy = .defaultCodable

json = """
{
Expand Down

0 comments on commit 9048ff1

Please sign in to comment.