Skip to content

Commit d8d0302

Browse files
committed
Add a little bit of code doc
1 parent 005a981 commit d8d0302

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

Sources/JSONAPI/Resource/Attribute.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
public protocol AttributeType: Codable {
99
}
1010

11+
// MARK: TransformedAttribute
12+
/// A TransformedAttribute takes a Codable type and attempts to turn it into another type.
1113
public struct TransformedAttribute<RawValue: Codable, Transformer: JSONAPI.Transformer>: AttributeType where Transformer.From == RawValue {
1214
private let rawValue: RawValue
1315

@@ -19,6 +21,15 @@ public struct TransformedAttribute<RawValue: Codable, Transformer: JSONAPI.Trans
1921
}
2022
}
2123

24+
// MARK: ValidatedAttribute
25+
/// A ValidatedAttribute does not transform its raw value, but it throws
26+
/// an error if the raw value does not match expectations.
27+
public typealias ValidatedAttribute<RawValue: Codable, Validator: JSONAPI.Validator> = TransformedAttribute<RawValue, Validator> where RawValue == Validator.From
28+
29+
// MARK: Attribute
30+
/// An Attribute simply represents a type that can be encoded and decoded.
31+
public typealias Attribute<T: Codable> = TransformedAttribute<T, IdentityTransformer<T>>
32+
2233
extension TransformedAttribute where Transformer: ReversibleTransformer {
2334
public init(transformedValue: Transformer.To) throws {
2435
self.value = transformedValue
@@ -43,10 +54,6 @@ extension TransformedAttribute where Transformer == IdentityTransformer<RawValue
4354
}
4455
}
4556

46-
public typealias ValidatedAttribute<RawValue: Codable, Validator: JSONAPI.Validator> = TransformedAttribute<RawValue, Validator> where RawValue == Validator.From
47-
48-
public typealias Attribute<T: Codable> = TransformedAttribute<T, IdentityTransformer<T>>
49-
5057
// MARK: - Codable
5158
extension TransformedAttribute {
5259
public init(from decoder: Decoder) throws {

0 commit comments

Comments
 (0)