Skip to content

Commit 45e21ff

Browse files
committed
Initial commit
0 parents  commit 45e21ff

File tree

11 files changed

+453
-0
lines changed

11 files changed

+453
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Xcode
2+
#
3+
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
4+
5+
## User settings
6+
xcuserdata/
7+
8+
## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
9+
*.xcscmblueprint
10+
*.xccheckout
11+
12+
## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
13+
build/
14+
DerivedData/
15+
*.moved-aside
16+
*.pbxuser
17+
!default.pbxuser
18+
*.mode1v3
19+
!default.mode1v3
20+
*.mode2v3
21+
!default.mode2v3
22+
*.perspectivev3
23+
!default.perspectivev3
24+
25+
## Obj-C/Swift specific
26+
*.hmap
27+
28+
## App packaging
29+
*.ipa
30+
*.dSYM.zip
31+
*.dSYM
32+
33+
## Playgrounds
34+
timeline.xctimeline
35+
playground.xcworkspace
36+
37+
# Swift Package Manager
38+
#
39+
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
40+
# Packages/
41+
# Package.pins
42+
# Package.resolved
43+
# *.xcodeproj
44+
#
45+
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
46+
# hence it is not needed unless you have added a package configuration file to your project
47+
# .swiftpm
48+
49+
.build/
50+
51+
# CocoaPods
52+
#
53+
# We recommend against adding the Pods directory to your .gitignore. However
54+
# you should judge for yourself, the pros and cons are mentioned at:
55+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
56+
#
57+
# Pods/
58+
#
59+
# Add this line if you want to avoid checking in source code from the Xcode workspace
60+
# *.xcworkspace
61+
62+
# Carthage
63+
#
64+
# Add this line if you want to avoid checking in source code from Carthage dependencies.
65+
# Carthage/Checkouts
66+
67+
Carthage/Build/
68+
69+
# Accio dependency management
70+
Dependencies/
71+
.accio/
72+
73+
# fastlane
74+
#
75+
# It is recommended to not store the screenshots in the git repo.
76+
# Instead, use fastlane to re-generate the screenshots whenever they are needed.
77+
# For more information about the recommended setup visit:
78+
# https://docs.fastlane.tools/best-practices/source-control/#source-control
79+
80+
fastlane/report.xml
81+
fastlane/Preview.html
82+
fastlane/screenshots/**/*.png
83+
fastlane/test_output
84+
85+
# Code Injection
86+
#
87+
# After new code Injection tools there's a generated folder /iOSInjectionProject
88+
# https://github.com/johnno1962/injectionforxcode
89+
90+
iOSInjectionProject/

.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Igor
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Package.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// swift-tools-version: 5.7
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "some-codable-swift",
8+
platforms: [.macOS(.v12), .iOS(.v15), .watchOS(.v8), .tvOS(.v15)],
9+
products: [
10+
// Products define the executables and libraries a package produces, and make them visible to other packages.
11+
.library(
12+
name: "some-codable-swift",
13+
targets: ["some-codable-swift"]),
14+
],
15+
dependencies: [
16+
// Dependencies declare other packages that this package depends on.
17+
// .package(url: /* package url */, from: "1.0.0"),
18+
],
19+
targets: [
20+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
21+
// Targets can depend on other targets in this package, and on products in packages this package depends on.
22+
.target(
23+
name: "some-codable-swift",
24+
dependencies: []),
25+
.testTarget(
26+
name: "some-codable-swiftTests",
27+
dependencies: ["some-codable-swift"]),
28+
]
29+
)

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Some codable
2+
## Subsidiary package
3+
4+
##SomeEncodable
5+
A type that can encode itself to an external representation from different types.
6+
7+
```swift
8+
struct User: Encodable{
9+
let name: String
10+
}
11+
let arr : [SomeEncodable] = [
12+
.init(User(name: "User")), 640, 20.65, "DPMSolverMultistep"
13+
]
14+
15+
if let encode = try? JSONEncoder().encode(arr){
16+
let str = String(decoding: encode, as: UTF8.self)
17+
print(str)
18+
}
19+
```
20+
*[{"name":"User"},640,20.649999999999999,"DPMSolverMultistep"]*
21+
22+
```swift
23+
struct User: Encodable{
24+
let name: String
25+
}
26+
let dic : [String: SomeEncodable] = [
27+
"data" : .init(User(name: "User")), "width" : 640, "num_inference_steps" : 20.65
28+
]
29+
30+
if let encode = try? JSONEncoder().encode(dic){
31+
let str = String(decoding: encode, as: UTF8.self)
32+
print(str)
33+
}
34+
35+
```
36+
37+
*{"width":640,"data":{"name":"User"},"num_inference_steps":20.649999999999999}*
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//
2+
// IEncodable.swift
3+
//
4+
//
5+
// Created by Igor on 10.03.2023.
6+
//
7+
8+
import Foundation
9+
10+
/// A type that can encode itself to an external representation from different types.
11+
public protocol IEncodable: Encodable {
12+
13+
/// wrapped value
14+
var value: Any { get }
15+
16+
init<T>(_ value: T?)
17+
18+
}
19+
20+
public extension IEncodable{
21+
22+
/// Encodes the output from upstream using a specified encoder.
23+
func encode(to encoder: Encoder) throws {
24+
/// Returns the data stored in this decoder as represented in a container appropriate for holding a single primitive value.
25+
var container = encoder.singleValueContainer()
26+
27+
switch value {
28+
/// let empty : SomeEncodable = nil
29+
case is Void: try container.encodeNil()
30+
31+
case let string as String: try container.encode(string)
32+
33+
case let bool as Bool: try container.encode(bool)
34+
35+
case let int as Int: try container.encode(int)
36+
case let uInt as UInt: try container.encode(uInt)
37+
38+
case let double as Double: try container.encode(double)
39+
40+
/// let date : SomeEncodable = .init(Date())
41+
case let date as Date: try container.encode(date.ISO8601Format())
42+
43+
/// let date : SomeEncodable = .init(URL(string: "https://www.google.com/"))
44+
case let url as URL: try container.encode(url)
45+
46+
/// let dic : [SomeEncodable] = [
47+
/// 1, "Name", "Age", true, 6.56, nil
48+
/// ]
49+
case let array as [Any?]: try container.encode(array.map { SomeEncodable($0) })
50+
51+
/// let dic : [String: SomeEncodable] = [
52+
/// "id" : 1,
53+
/// "data" : nil
54+
/// ]
55+
case let dictionary as [String: Any?]: try container.encode(dictionary.mapValues { SomeEncodable($0) })
56+
57+
/// struct User: Encodable{
58+
/// let name: String
59+
/// }
60+
/// let dic : [String: SomeEncodable] = [
61+
/// "data" : .init(User(name: "test"))
62+
/// ]
63+
64+
case let encodable as Encodable: try encodable.encode(to: encoder)
65+
66+
default:
67+
let ctx = EncodingError.Context(codingPath: container.codingPath, debugDescription: "Can't encode value")
68+
throw EncodingError.invalidValue(value, ctx)
69+
}
70+
}
71+
}
72+
73+
74+
75+
76+
77+
78+
79+
80+
81+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// IHashable.swift
3+
//
4+
//
5+
// Created by Igor on 10.03.2023.
6+
//
7+
8+
import Foundation
9+
10+
11+
protocol IHashable: Hashable{
12+
13+
associatedtype ValueType: Hashable
14+
15+
var value: Any { get }
16+
17+
}
18+
19+
extension IHashable{
20+
/// Hashes the essential components of this value by feeding them into the given hasher.
21+
public func hash(into hasher: inout Hasher) {
22+
23+
switch value {
24+
/// Type 'Void' cannot conform to 'Hashable'
25+
26+
case _ as Void: hasher.combine(NSNull())
27+
28+
case let value as String: hasher.combine(value)
29+
30+
case let value as Bool: hasher.combine(value)
31+
32+
case let value as Int: hasher.combine(value)
33+
case let value as UInt: hasher.combine(value)
34+
35+
case let value as Float: hasher.combine(value)
36+
case let value as Double: hasher.combine(value)
37+
38+
case let value as Date: hasher.combine(value)
39+
40+
case let value as URL: hasher.combine(value)
41+
42+
case let value as [String: ValueType]: hasher.combine(value)
43+
case let value as [ValueType]:hasher.combine(value)
44+
45+
default: break
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)