Skip to content

Commit 09798c0

Browse files
Update README.md
1 parent 75143cc commit 09798c0

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

README.md

+99
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,42 @@
22

33
ImmutableSwift is a tool that generates swift model that supports immutability, coding(Coding and NSCoding), value comparions, hashing and copying. Directly inspired by [facebook/remodel](https://github.com/facebook/remodel).
44

5+
TLDR: generate this
6+
```
7+
import Foundation
8+
9+
class Friend : Hashable, Codable, NSCopying{
10+
let name : String
11+
let daySinceFirstMet : Int
12+
13+
init(name:String, daySinceFirstMet:Int) {
14+
self.name = name
15+
self.daySinceFirstMet = daySinceFirstMet
16+
}
17+
18+
static func == (lhs: Friend, rhs: Friend) -> Bool {
19+
return lhs.name == rhs.name && lhs.daySinceFirstMet == rhs.daySinceFirstMet
20+
}
21+
22+
func hash(into hasher: inout Hasher) {
23+
hasher.combine(name)
24+
hasher.combine(daySinceFirstMet)
25+
}
26+
27+
func copy(with zone: NSZone? = nil) -> Any {
28+
let copy = Friend(name:name,daySinceFirstMet:daySinceFirstMet)
29+
return copy
30+
}
31+
}
32+
```
33+
from this
34+
```
35+
Friend {
36+
String name
37+
Int daySinceFirstMet
38+
}
39+
```
40+
541
## Download
642
You can download the latest release from here: https://github.com/hackthehackerman/ImmutableSwift/releases
743

@@ -20,6 +56,7 @@ Friend {
2056
Int daySinceFirstMet
2157
}
2258
```
59+
2360
Once you created the schema file, you can generate the desired swift model with
2461
```
2562
./ImmutableSwift Path/To/ModelDirectory/Friend.value
@@ -31,3 +68,65 @@ To generate models for every .value files in a directory, subtitute the file pat
3168
./ImmutableSwift Path/To/ModelDirectory/
3269
```
3370

71+
## Import
72+
ImmutableSwift assumes that every types that a model depends on reside in the same module. If a model depends on another module, simply add the required module on top of the schema. For example:
73+
```
74+
import PhoneNumber
75+
Friend {
76+
String name
77+
Int daySinceFirstMet
78+
PhoneNumber number
79+
}
80+
```
81+
The syntax for import are the same as swift's import syntax. You can import a module with `import module`, import a submodule with `improt module.submodule` or import a specific kind of symbol with `import kind module.symbole`
82+
83+
## Comments
84+
ImmutableSwift supports adding comments to the generated models.
85+
```
86+
import PhoneNumber
87+
Friend {
88+
# example comment
89+
String name
90+
# at the moment, ImmutableSwift only support comments inside the model bracket (between brackets)
91+
Int daySinceFirstMet
92+
# comment must be in its own line, and starts with a pound sign #
93+
PhoneNumber number
94+
}
95+
```
96+
97+
## AccessControl
98+
ImmutableSwift supports defining optional access levels for the generated model. At the moment, it only supports `public` and `internal`. This is mainly to help generate models in a module that are meant to be imported. For example:
99+
```
100+
public Friend {
101+
String name
102+
Int daySinceFirstMet
103+
}
104+
```
105+
106+
## NSCoding
107+
In some projects, immutable data model might need to support NSCoding. To have ImmutableSwift generates associated methods that support NSCoding, include the plugin `ISCoding` in the model's schema.
108+
```
109+
# here, ISCoding plugin is responsible for generating the encode and init method required by NSCoding protocol
110+
# ISCopying plugin is responsible for generating the copy method required by the NSCopying protocol
111+
public Friend (ISCoding, ISCopying){
112+
String name
113+
Int daySinceFirstMet
114+
}
115+
```
116+
117+
## Plugins
118+
Similar to [facebook/remodel](https://github.com/facebook/remodel), ImmutableSwift uses a simple plugin system. The plugin system are designed to encapsulate cohesive generation logics, and extend the functionality of the code generator. You can find a list of plugins here: https://github.com/hackthehackerman/ImmutableSwift/tree/master/Sources/ImmutableSwift/generating/plugins.
119+
120+
To specify plugins for a specific model, add the list after the model name. Noted that, if you don't specific a list of plugins to used, ImmutableSwift will use a default list of plugins: [ISCodable, ISHashable, ISCopying].
121+
```
122+
# ISCodable is responsible for generating codes for the Codable protocol
123+
# ISHashable is responsible for generating the == and hash function for the Hashable protocol
124+
# ISCopying plugin is responsible for generating the copy method required by the NSCopying protocol
125+
public Friend (ISCodable, ISHashable, ISCopying){
126+
String name
127+
Int daySinceFirstMet
128+
}
129+
```
130+
131+
## Contributing
132+
Pull requests are very welcomed!

0 commit comments

Comments
 (0)