Skip to content

Commit

Permalink
renamed to ContextCodable, added README
Browse files Browse the repository at this point in the history
  • Loading branch information
ypopovych committed Jul 7, 2023
1 parent ab1e135 commit c80b0f8
Show file tree
Hide file tree
Showing 10 changed files with 359 additions and 233 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
Pod::Spec.new do |s|
s.name = 'ConfigurationCodable.swift'
s.name = 'ContextCodable.swift'
s.version = '0.1.0'
s.summary = 'Backport of CodableWithConfiguration to old OS versions and Linux'

s.description = <<-DESC
CodableWithConfiguration ported to older versions of Apple operating systems and Linux
DESC

s.homepage = 'https://github.com/tesseract-one/ConfigurationCodable.swift'
s.homepage = 'https://github.com/tesseract-one/ContextCodable.swift'

s.license = { :type => 'Apache-2.0', :file => 'LICENSE' }
s.author = { 'Tesseract Systems, Inc.' => '[email protected]' }
s.source = { :git => 'https://github.com/tesseract-one/ConfigurationCodable.swift.git', :tag => s.version.to_s }
s.source = { :git => 'https://github.com/tesseract-one/ContextCodable.swift.git', :tag => s.version.to_s }

s.ios.deployment_target = '11.0'
s.osx.deployment_target = '10.12'
Expand All @@ -20,12 +20,12 @@ Pod::Spec.new do |s|

s.swift_version = '5.4'

s.module_name = 'ConfigurationCodable'
s.module_name = 'ContextCodable'

s.source_files = 'Sources/ConfigurationCodable/**/*.swift'
s.source_files = 'Sources/ContextCodable/**/*.swift'

s.test_spec 'Tests' do |test_spec|
test_spec.platforms = {:ios => '11.0', :osx => '10.12', :tvos => '11.0'}
test_spec.source_files = 'Tests/ConfigurationCodableTests/*.swift'
test_spec.source_files = 'Tests/ContextCodableTests/*.swift'
end
end
12 changes: 6 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
import PackageDescription

let package = Package(
name: "ConfigurationCodable",
name: "ContextCodable",
platforms: [.macOS(.v10_10), .iOS(.v9), .tvOS(.v9), .watchOS(.v2)],
products: [
.library(
name: "ConfigurationCodable",
targets: ["ConfigurationCodable"])
name: "ContextCodable",
targets: ["ContextCodable"])
],
dependencies: [],
targets: [
.target(
name: "ConfigurationCodable",
name: "ContextCodable",
dependencies: []),
.testTarget(
name: "ConfigurationCodableTests",
dependencies: ["ConfigurationCodable"])
name: "ContextCodableTests",
dependencies: ["ContextCodable"])
]
)
136 changes: 134 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,134 @@
# ConfigurationCodable.swift
Backport of CodableWithConfiguration to old OS versions and Linux
# ContextCodable.swift

![🐧 linux: ready](https://img.shields.io/badge/%F0%9F%90%A7%20linux-ready-red.svg)
[![GitHub license](https://img.shields.io/badge/license-Apache%202.0-lightgrey.svg)](https://raw.githubusercontent.com/tesseract-one/ContextCodable.swift/main/LICENSE)
[![Build Status](https://github.com/tesseract-one/ContextCodable.swift/workflows/Build%20%26%20Tests/badge.svg?branch=main)](https://github.com/tesseract-one/ContextCodable.swift/actions?query=workflow%3ABuild%20%26%20Tests+branch%3Amain)
[![GitHub release](https://img.shields.io/github/release/tesseract-one/ContextCodable.swift.svg)](https://github.com/tesseract-one/ContextCodable.swift/releases)
[![SPM compatible](https://img.shields.io/badge/SwiftPM-Compatible-brightgreen.svg)](https://swift.org/package-manager/)
[![CocoaPods version](https://img.shields.io/cocoapods/v/ContextCodable.swift.svg)](https://cocoapods.org/pods/ContextCodable.swift)
![Platform OS X | iOS | tvOS | watchOS | Linux](https://img.shields.io/badge/platform-Linux%20%7C%20OS%20X%20%7C%20iOS%20%7C%20tvOS%20%7C%20watchOS-orange.svg)

### Backport of CodableWithConfiguration to old OS versions and Linux

## Why?

Apple added type of `Codable` - `CodableWithConfiguration` which allows to provide context for encoding and decoding.

But this protocols are available only from macOS 12, and `JSONEncoder` and `JSONDecoder` support added only in macOS 15. Linux does not support them at all.

So we created this library, which enables this API on old Swift and platforms. It has some speed drawbacks, but it works!

## Getting started

### Installation

#### [Package Manager](https://swift.org/package-manager/)

Add the following dependency to your [Package.swift](https://github.com/apple/swift-package-manager/blob/master/Documentation/Usage.md#define-dependencies):

```swift
.package(url: "https://github.com/tesseract-one/ContextCodable.swift.git", from: "0.1.0")
```

Run `swift build` and build your app.

#### [CocoaPods](http://cocoapods.org/)

Add the following to your [Podfile](http://guides.cocoapods.org/using/the-podfile.html):

```rb
pod 'ContextCodable.swift', '~> 0.1.0'
```

Then run `pod install`

### Examples

#### Encoding
```swift
import Foundation
import ContextCodable

struct SomeEncodable: ContextEncodable {
typealias EncodingContext = (top: String, internal: Date)

let internal: Internal

struct Internal: ContextEncodable {
typealias EncodingContext = Date

let value: Bool

func encode(to encoder: Encoder, context: EncodingContext) throws {
var container = encoder.unkeyedContainer()
try container.encode(value)
try container.encode(context)
}
}

enum CodingKeys: CodingKey {
case `internal`
case context
}

func encode(to encoder: Encoder, context: EncodingContext) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.internal, forKey: .internal, context: context.internal)
try container.encode(context.top, forKey: .context)
}
}

let value = SomeEncodable(internal: SomeEncodable.Internal(value: true))
let encoded = try JSONEncoder().encode(value, context: (top: "Test", internal: Date()))
print(String(data: encoded, encoding: .utf8)!)
```

#### Decoding
```swift
import Foundation
import ContextCodable

struct SomeDecodable: ContextDecodable {
typealias DecodingContext = (top: String, internal: Date)

let internal: Internal
let top: String

struct Internal: ContextDecodable {
typealias DecodingContext = Date

let value: Bool
let date: Date

init(from decoder: Decoder, context: DecodingContext) throws {
var container = try decoder.unkeyedContainer()
value = try container.decode(Bool.self)
date = context
}
}

enum CodingKeys: CodingKey {
case `internal`
case context
}

init(from decoder: Decoder, context: DecodingContext) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.internal = try decoder.decode(Internal.self, forKey: .internal, context: context.internal)
self.top = context.top
}
}

let json = Data("{\"internal\": [true]}".utf8)
let decoded = try JSONDecoder().decode(SomeDecodable.self, from: json, context: (top: "Test", internal: Date()))
print(decoded)
```

## Author

- [Tesseract Systems, Inc.](mailto:[email protected])
([@tesseract_one](https://twitter.com/tesseract_one))

## License

ContextCodable.swift is available under the Apache 2.0 license. See [the LICENSE file](./LICENSE) for more information.
96 changes: 0 additions & 96 deletions Sources/ConfigurationCodable/Decodable.swift

This file was deleted.

68 changes: 0 additions & 68 deletions Sources/ConfigurationCodable/Encodable.swift

This file was deleted.

Loading

0 comments on commit c80b0f8

Please sign in to comment.