Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tattn committed Mar 1, 2018
0 parents commit eda8329
Show file tree
Hide file tree
Showing 26 changed files with 2,394 additions and 0 deletions.
60 changes: 60 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
### https://raw.github.com/github/gitignore/e6dd3a81e6037cc923e503e372c80326f65ccb25/Global/macos.gitignore

*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk


### https://raw.github.com/github/gitignore/e6dd3a81e6037cc923e503e372c80326f65ccb25/Global/xcode.gitignore

# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## Build generated
build/
DerivedData/

## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata/

## Other
*.moved-aside
*.xccheckout
*.xcscmblueprint
UserInterfaceState.xcuserstate

#Carthage/Build
Carthage

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Tatsuya Tanaka

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
508 changes: 508 additions & 0 deletions MoreCodable.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

199 changes: 199 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
<h1 align="center">MoreCodable</h1>

<h5 align="center">MoreCodable expands the possibilities of "Codable".</h5>

<div align="center">
<a href="https://github.com/Carthage/Carthage">
<img src="https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat" alt="Carthage compatible" />
</a>
<a href="https://developer.apple.com/swift">
<img src="https://img.shields.io/badge/Swift-4-F16D39.svg" alt="Swift Version" />
</a>
<a href="./LICENSE">
<img src="https://img.shields.io/badge/license-MIT-green.svg?style=flat-square" alt="license:MIT" />
</a>
</div>

<br />


# Installation

## Carthage

```ruby
github "tattn/MoreCodable"
```


# Feature

## DictionaryEncoder / DictionaryDecoder

```swift
struct User: Codable {
let id: Int
let name: String
}

let encoder = DictionaryEncoder()
let user = User(id: 123, name: "tattn")
let dictionary: [String: Any] = try! encoder.encode(user) // => {"id": 123, "name": "tattn"}
```

```swift
let decoder = DictionaryDecoder()
let user = try decoder.decode(User.self, from: dictionary)
```

## URLQueryItemsEncoder / URLQueryItemsDecoder

```swift
struct Parameter: Codable {
let query: String
let offset: Int
let limit: Int
}
let parameter = Parameter(query: "ねこ", offset: 10, limit: 20)
let encoder = URLQueryItemsEncoder()
let params: [URLQueryItem] = try! encoder.encode(parameter)

var components = URLComponents(string: "https://example.com")
components?.queryItems = params
components?.url // https://example.com?query=%E3%81%AD%E3%81%93&offset=10&limit=20
```

```swift
let decoder = URLQueryItemsDecoder()
let parameter = try decoder.decode(Parameter.self, from: params)
```

## RuleBasedCodingKey

```swift
struct User: Codable {
let userId: String
let name: String

enum CodingKeys: String, RuleBasedCodingKey {
case userId
case name

func codingKeyRule(key: String) -> String {
return key.uppercased() // custom rule
}
}
}

let json = """
{"USERID": "abc", "NAME": "tattn"}
""".data(using: .utf8)!

let user = try! JSONDecoder().decode(User.self, from: json) // => User(userId: "abc", name: "tattn")
```

### SnakeCaseCodingKey

```swift
struct User: Codable {
let userId: String
let name: String

enum CodingKeys: String, SnakeCaseCodingKey {
case userId
case name
}
}

let json = """
{"user_id": "abc", "name": "tattn"}
""".data(using: .utf8)!

let user = try! JSONDecoder().decode(User.self, from: json) // ok
```

### UpperCamelCaseCodingKey

```swift
struct User: Codable {
let userId: String
let name: String

enum CodingKeys: String, UpperCamelCaseCodingKey {
case userId
case name
}
}

let json = """
{"UserId": "abc", "Name": "tattn"}
""".data(using: .utf8)!

let user = try! JSONDecoder().decode(User.self, from: json) // ok
```

## Failable<Wrapped>

```swift
let json = """
[
{"name": "Taro", "age": 20},
{"name": "Hanako"}
]
""".data(using: .utf8)! // Hanako has no "age"

struct User: Codable {
let name: String
let age: Int
}

let users = try! JSONDecoder().decode([Failable<User>].self,
from: json)

// success
XCTAssertEqual(users[0].value?.name, "Taro")
XCTAssertEqual(users[0].value?.age, 20)
XCTAssertNil(users[1].value)
```

## StringTo<T>

```swift
let json = """
{
"int": "100",
"articleId": "abc"
}
""".data(using: .utf8)!

struct Root: Codable {
let int: StringTo<Int>
let articleId: StringTo<ArticleId>

struct ArticleId: LosslessStringConvertible, Codable {
var description: String

init?(_ description: String) {
self.description = description
}
}
}

let root = try! JSONDecoder().decode(Root.self, from: json)

// success
XCTAssertEqual(root.int.value, 100)
XCTAssertEqual(root.articleId.value.description, "abc")
```

# Contributing

1. Fork it!
2. Create your feature branch: `git checkout -b my-new-feature`
3. Commit your changes: `git commit -am 'Add some feature'`
4. Push to the branch: `git push origin my-new-feature`
5. Submit a pull request :D

# License

MoreCodable is released under the MIT license. See LICENSE for details.
31 changes: 31 additions & 0 deletions Sources/AnyCodingKey.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// AnyCodingKey.swift
// MoreCodable
//
// Created by Tatsuya Tanaka on 20180211.
// Copyright © 2018年 tattn. All rights reserved.
//

import Foundation

struct AnyCodingKey : CodingKey {
public var stringValue: String
public var intValue: Int?

public init?(stringValue: String) {
self.stringValue = stringValue
self.intValue = nil
}

public init?(intValue: Int) {
self.stringValue = "\(intValue)"
self.intValue = intValue
}

init(index: Int) {
self.stringValue = "Index \(index)"
self.intValue = index
}

static let `super` = AnyCodingKey(stringValue: "super")!
}
Loading

0 comments on commit eda8329

Please sign in to comment.