Skip to content

Commit

Permalink
Update README.md with proper format (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
christiancabarrocas authored Oct 15, 2024
1 parent 058c44b commit 8a77878
Showing 1 changed file with 140 additions and 128 deletions.
268 changes: 140 additions & 128 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# Sage Swift Kit
[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

Expand All @@ -8,48 +9,44 @@ Two different type of tools are provided: general macros and codable macros.

#### Wip Macro
Mark work in progress code and convert it into an XCode warning.
/*:
wip(feature: String, todo: String)
*/

wip(feature: String, todo: String)

Example:
/*:
\#wip(feature: "New login workflow", todo: "API connection")
*/

#wip(feature: "New login workflow", todo: "API connection")


#### Debug print
Print while debugging safely. It will just add #if DEBUG.
/*:
debug_print(_ description: String)
*/

debug_print(_ description: String)


#### Default init
Provide init func to a struct.
/*:
DefaultInit
*/
Example:
/*:
@DefaultInit
struct Vehicle {
let wheels: Int
let maxSpeed: Int
let name: String
}
*/

@DefaultInit
struct Vehicle {
let wheels: Int
let maxSpeed: Int
let name: String
}

will be expanded to
/*:
struct Vehicle {
let wheels: Int
let maxSpeed: Int
let name: String

func init(wheels: Int, maxSpeed: Int, name: String) {
self.wheels = wheels
self.maxSpeed = maxSpeed
self.name = name

struct Vehicle {
let wheels: Int
let maxSpeed: Int
let name: String
func init(wheels: Int, maxSpeed: Int, name: String) {
self.wheels = wheels
self.maxSpeed = maxSpeed
self.name = name
}
}
}
*/

## Codable Macros
A set of macros for workinf easily with Codable, reducing the code needed.
Expand All @@ -60,138 +57,153 @@ You also need to add conformance to Codable in an extension of the object withou
Allows you to create a custom key for decoding.

Example:
/*:
@CustomCodable
struct Vehicle {
let wheels: Int
@CustomCodableKey("speedAllowed")
let maxSpeed: Int
let name: String
}

extension Vehicle: Codable {}
*/

@CustomCodable
struct Vehicle {
let wheels: Int
@CustomCodableKey("speedAllowed")
let maxSpeed: Int
let name: String
}

extension Vehicle: Codable {}

#### Custom Default Value - CustomDefault(Any)
Allows you to add a default value in case there is no value founded while decoding

Example:
/*:
@CustomCodable
struct Vehicle {
let wheels: Int
@CustomDefault(150)
let maxSpeed: Int
let name: String
}

extension Vehicle: Codable {}
*/

@CustomCodable
struct Vehicle {
let wheels: Int
@CustomDefault(150)
let maxSpeed: Int
let name: String
}

extension Vehicle: Codable {}

#### Custom Date - CustomDate(dateFormat: String, defaultValue: Date? = nil)
Allows you to decode Strings into Dates with default values

Example:
/*:
@CustomCodable
struct Vehicle {
let wheels: Int
@CustomDate(dateFormat: "YYYYY-mm-dd", defaultValue: Date())
let designed: Date
let maxSpeed: Int
let name: String
}

extension Vehicle: Codable {}
*/

@CustomCodable
struct Vehicle {
let wheels: Int
@CustomDate(dateFormat: "YYYYY-mm-dd", defaultValue: Date())
let designed: Date
let maxSpeed: Int
let name: String
}

extension Vehicle: Codable {}

#### Custom Date - CustomDate(dateFormat: String, defaultValue: Date? = nil)
Allows you to decode Strings into Dates with default values

Example:
/*:
@CustomCodable
struct Vehicle {
let wheels: Int
@CustomDate(dateFormat: "YYYYY-mm-dd", defaultValue: Date())
let designed: Date
let maxSpeed: Int
let name: String
}

extension Vehicle: Codable {}
*/

@CustomCodable
struct Vehicle {
let wheels: Int
@CustomDate(dateFormat: "YYYYY-mm-dd", defaultValue: Date())
let designed: Date
let maxSpeed: Int
let name: String
}

extension Vehicle: Codable {}

#### Custom Hashable - CustomHashable(parameters: [String])
Allows you to add Hashable conformance providing the properties you want to use.

Example:
/*:
@CustomCodable
CustomHashable(parameters: ["wheels", "name"])
struct Vehicle {
let wheels: Int
let designed: Date
let maxSpeed: Int
let name: String
}

extension Vehicle: Codable {}
*/

@CustomCodable
CustomHashable(parameters: ["wheels", "name"])
struct Vehicle {
let wheels: Int
let designed: Date
let maxSpeed: Int
let name: String
}

extension Vehicle: Codable {}

will expand to:
/*:
extension Vehicle: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(wheels)
hasher.combine(name)

extension Vehicle: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(wheels)
hasher.combine(name)
}
}
}
*/

#### Custom Equatable - CustomEquatable(parameters: [String])
Allows you to add Equatable conformance providing the properties you want to use.

Example:
/*:
@CustomCodable
CustomEquatable(parameters: ["wheels", "maxSpeed"])
struct Vehicle {
let wheels: Int
let designed: Date
let maxSpeed: Int
let name: String
}

extension Vehicle: Codable {}
*/

@CustomCodable
CustomEquatable(parameters: ["wheels", "maxSpeed"])
struct Vehicle {
let wheels: Int
let designed: Date
let maxSpeed: Int
let name: String
}

extension Vehicle: Codable {}

will expand to:
/*:
extension Vehicle: Equatable {
public static func == (lhs: Vehicle, rhs: Vehicle) -> Bool {
lhs.wheels == rhs.wheel && lhs.maxSpeed == rhs.maxSpeed

extension Vehicle: Equatable {
public static func == (lhs: Vehicle, rhs: Vehicle) -> Bool {
lhs.wheels == rhs.wheel && lhs.maxSpeed == rhs.maxSpeed
}
}
}
*/

#### Custom URL - CustomDate(dateFormat: String, defaultValue: Date? = nil)
Allows you to decode Strings into optional URL

Example:
/*:
@CustomCodable
struct Vehicle {
let wheels: Int
let designed: Date
let maxSpeed: Int
let name: String
@CustomURL
let website: URL?
}

extension Vehicle: Codable {}
*/

@CustomCodable
struct Vehicle {
let wheels: Int
let designed: Date
let maxSpeed: Int
let name: String
@CustomURL
let website: URL?
}

extension Vehicle: Codable {}

Of course you can combine all of them:

@CustomCodable
@DefaultInit
@CustomHashable(["wheels", "name"])
@CustomEquatable(["wheels", "designed"])
struct Vehicle {
@CustomCodableKey("number_of_wheels")
let wheels: Int
@CustomDate(dateFormat: "YYYYY-mm-dd", defaultValue: Date())
let designed: Date
@CustomDefault(150)
let maxSpeed: Int
let name: String
@CustomURL
let website: URL?
}
extension Vehicle: Codable {}

## Contributing
We welcome contributions from the community! Please see our [Contributing Guidelines](CONTRIBUTING.md) for more information on how to get started.
Expand Down

0 comments on commit 8a77878

Please sign in to comment.