Skip to content

Commit

Permalink
Add convert from UPPERCASE decoding key strategy (#214)
Browse files Browse the repository at this point in the history
* Add convert from UPPERCASE decoding key strategy

This will convert from ALL_CAPS_SNAKE_CASE to camelCase.

Previously, there was only snake_case, kebab-case, and first-letter-only Capitalised. To do ALL_CAPS, one would need to have used a custom key decoding strategy.

* Update Tests/XMLCoderTests/Minimal/KeyedTests.swift

Co-authored-by: Max Desiatov <max@desiatov.com>
  • Loading branch information
huwr and MaxDesiatov committed Jul 29, 2021
1 parent a469f60 commit 338d503
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Sources/XMLCoder/Decoder/XMLDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ open class XMLDecoder {
/// Convert from "CodingKey" to "codingKey"
case convertFromCapitalized

/// Convert from "CODING_KEY" to "codingKey"
case convertFromUppercase

/// Provide a custom conversion from the key in the encoded XML to the
/// keys specified by the decoded types.
/// The full path to the current decoding position is provided for
Expand All @@ -177,6 +180,10 @@ open class XMLDecoder {
return result
}

static func _convertFromUppercase(_ stringKey: String) -> String {
_convert(stringKey.lowercased(), usingSeparator: "_")
}

static func _convertFromSnakeCase(_ stringKey: String) -> String {
return _convert(stringKey, usingSeparator: "_")
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/XMLCoder/Decoder/XMLDecoderImplementation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,8 @@ extension XMLDecoderImplementation {
return XMLDecoder.KeyDecodingStrategy._convertFromSnakeCase
case .convertFromCapitalized:
return XMLDecoder.KeyDecodingStrategy._convertFromCapitalized
case .convertFromUppercase:
return XMLDecoder.KeyDecodingStrategy._convertFromUppercase
case .convertFromKebabCase:
return XMLDecoder.KeyDecodingStrategy._convertFromKebabCase
case .useDefaultKeys:
Expand Down
19 changes: 19 additions & 0 deletions Tests/XMLCoderTests/Minimal/KeyedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,25 @@ class KeyedTests: XCTestCase {
XCTAssertThrowsError(try decoder.decode(ContainerCamelCase.self, from: xmlData))
}

func testConvertFromAllCaps() throws {
let decoder = XMLDecoder()
decoder.keyDecodingStrategy = .convertFromUppercase

let xmlString =
"""
<CONTAINER TEST_ATTRIBUTE="test_container">
<VAL_UE>
<FOO>12</FOO>
</VAL_UE>
</CONTAINER>
"""
let xmlData = xmlString.data(using: .utf8)!

let decoded = try decoder.decode(ContainerCamelCase.self, from: xmlData)

XCTAssertEqual(decoded.valUe, ["foo": 12])
}

func testCustomDecoderConvert() throws {
let decoder = XMLDecoder()
decoder.keyDecodingStrategy = .custom { keys in
Expand Down

0 comments on commit 338d503

Please sign in to comment.