Skip to content

Commit 7a1827b

Browse files
committed
Rename bytes-from-string decoders
1 parent 618d19d commit 7a1827b

File tree

7 files changed

+105
-28
lines changed

7 files changed

+105
-28
lines changed

Sources/Configuration/Documentation.docc/Documentation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,8 @@ Any package can implement a ``ConfigProvider``, making the ecosystem extensible
456456
### Value conversion
457457
- ``ExpressibleByConfigString``
458458
- ``ConfigBytesFromStringDecoder``
459-
- ``Base64BytesFromStringDecoder``
460-
- ``HexByteFromStringDecoder``
459+
- ``BytesFromBase64StringDecoder``
460+
- ``BytesFromHexStringDecoder``
461461

462462
### Contributing
463463
- <doc:Development>

Sources/Configuration/Documentation.docc/Reference/Base64BytesFromStringDecoder.md

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# ``Configuration/ConfigBytesFromBase64StringDecoder``
2+
3+
## Topics
4+
5+
### Creating bytes from a base64 string
6+
7+
- ``init()``
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# ``Configuration/ConfigBytesFromHexStringDecoder``
2+
3+
## Topics
4+
5+
### Creating bytes from a hex string decoder
6+
7+
- ``init()``

Sources/Configuration/Documentation.docc/Reference/HexByteFromStringDecoder.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

Sources/Configuration/ValueCoders/ConfigBytesFromStringDecoder.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import Foundation
2222
///
2323
/// This protocol defines the interface for converting string-based configuration
2424
/// values into binary data. Different implementations can support various encoding
25-
/// formats such as Base64, hexadecimal, or other custom encodings.
25+
/// formats such as base64, hexadecimal, or other custom encodings.
2626
///
2727
/// ## Usage
2828
///
@@ -32,7 +32,7 @@ import Foundation
3232
///
3333
/// ```swift
3434
/// let decoder: ConfigBytesFromStringDecoder = .base64
35-
/// let bytes = decoder.decode("SGVsbG8gV29ybGQ=") // "Hello World" in Base64
35+
/// let bytes = decoder.decode("SGVsbG8gV29ybGQ=") // "Hello World" in base64
3636
/// ```
3737
public protocol ConfigBytesFromStringDecoder: Sendable {
3838

@@ -48,17 +48,17 @@ public protocol ConfigBytesFromStringDecoder: Sendable {
4848
func decode(_ value: String) -> [UInt8]?
4949
}
5050

51-
/// A decoder that converts Base64-encoded strings into byte arrays.
51+
/// A decoder that converts base64-encoded strings into byte arrays.
5252
///
53-
/// This decoder interprets string configuration values as Base64-encoded data
53+
/// This decoder interprets string configuration values as base64-encoded data
5454
/// and converts them to their binary representation.
55-
public struct Base64BytesFromStringDecoder: Sendable {
55+
public struct ConfigBytesFromBase64StringDecoder: Sendable {
5656

57-
/// Creates a new Base64 decoder.
57+
/// Creates a new base64 decoder.
5858
public init() {}
5959
}
6060

61-
extension Base64BytesFromStringDecoder: ConfigBytesFromStringDecoder {
61+
extension ConfigBytesFromBase64StringDecoder: ConfigBytesFromStringDecoder {
6262
// swift-format-ignore: AllPublicDeclarationsHaveDocumentation
6363
public func decode(_ value: String) -> [UInt8]? {
6464
guard let data = Data(base64Encoded: value) else {
@@ -68,9 +68,9 @@ extension Base64BytesFromStringDecoder: ConfigBytesFromStringDecoder {
6868
}
6969
}
7070

71-
extension ConfigBytesFromStringDecoder where Self == Base64BytesFromStringDecoder {
71+
extension ConfigBytesFromStringDecoder where Self == ConfigBytesFromBase64StringDecoder {
7272

73-
/// A decoder that interprets string values as Base64-encoded data.
73+
/// A decoder that interprets string values as base64-encoded data.
7474
public static var base64: Self { .init() }
7575
}
7676

@@ -85,13 +85,13 @@ extension ConfigBytesFromStringDecoder where Self == Base64BytesFromStringDecode
8585
/// The decoder expects strings with an even number of characters, where each
8686
/// pair of characters represents one byte. For example, "48656C6C6F" represents
8787
/// the bytes for "Hello".
88-
public struct HexByteFromStringDecoder: Sendable {
88+
public struct ConfigBytesFromHexStringDecoder: Sendable {
8989

9090
/// Creates a new hexadecimal decoder.
9191
public init() {}
9292
}
9393

94-
extension HexByteFromStringDecoder: ConfigBytesFromStringDecoder {
94+
extension ConfigBytesFromHexStringDecoder: ConfigBytesFromStringDecoder {
9595
// swift-format-ignore: AllPublicDeclarationsHaveDocumentation
9696
public func decode(_ value: String) -> [UInt8]? {
9797
if value.count % 2 != 0 {
@@ -113,7 +113,7 @@ extension HexByteFromStringDecoder: ConfigBytesFromStringDecoder {
113113
}
114114
}
115115

116-
extension ConfigBytesFromStringDecoder where Self == HexByteFromStringDecoder {
116+
extension ConfigBytesFromStringDecoder where Self == ConfigBytesFromHexStringDecoder {
117117

118118
/// A decoder that interprets string values as hexadecimal-encoded data.
119119
public static var hex: Self { .init() }
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftConfiguration open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the SwiftConfiguration project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftConfiguration project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import Testing
16+
import Foundation
17+
@testable import Configuration
18+
19+
struct ConfigBytesFromStringDecoderTests {
20+
21+
@available(Configuration 1.0, *)
22+
@Test func base64DecoderValidStrings() {
23+
let decoder = ConfigBytesFromBase64StringDecoder()
24+
25+
// Test "Hello World" in base64
26+
let helloWorldB64 = "SGVsbG8gV29ybGQ="
27+
let expectedHelloWorld: [UInt8] = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]
28+
#expect(decoder.decode(helloWorldB64) == expectedHelloWorld)
29+
30+
// Test empty string
31+
#expect(decoder.decode("") == [])
32+
}
33+
34+
@available(Configuration 1.0, *)
35+
@Test func base64StaticConvenienceMethod() {
36+
let decoder: any ConfigBytesFromStringDecoder = .base64
37+
// "Hello" in base64
38+
let testString = "SGVsbG8="
39+
let expected: [UInt8] = [72, 101, 108, 108, 111]
40+
#expect(decoder.decode(testString) == expected)
41+
}
42+
43+
@available(Configuration 1.0, *)
44+
@Test func hexDecoderValidStrings() {
45+
let decoder = ConfigBytesFromHexStringDecoder()
46+
47+
// Test "Hello" in hex (uppercase)
48+
let helloHexUpper = "48656C6C6F"
49+
let expectedHello: [UInt8] = [72, 101, 108, 108, 111]
50+
#expect(decoder.decode(helloHexUpper) == expectedHello)
51+
52+
// Test "Hello" in hex (lowercase)
53+
let helloHexLower = "48656c6c6f"
54+
#expect(decoder.decode(helloHexLower) == expectedHello)
55+
56+
// Test mixed case
57+
let helloHexMixed = "48656C6c6F"
58+
#expect(decoder.decode(helloHexMixed) == expectedHello)
59+
60+
// Test empty string
61+
#expect(decoder.decode("") == [])
62+
63+
// Test single byte values
64+
#expect(decoder.decode("00") == [0])
65+
#expect(decoder.decode("FF") == [255])
66+
#expect(decoder.decode("ff") == [255])
67+
}
68+
69+
@available(Configuration 1.0, *)
70+
@Test func hexStaticConvenienceMethod() {
71+
let decoder: any ConfigBytesFromStringDecoder = .hex
72+
// "Hello" in hex
73+
let testString = "48656C6C6F"
74+
let expected: [UInt8] = [72, 101, 108, 108, 111]
75+
#expect(decoder.decode(testString) == expected)
76+
}
77+
}

0 commit comments

Comments
 (0)