Skip to content

Commit b10a33d

Browse files
authored
Merge pull request #54 from frajaona/fix-boolean-serialization
Fix boolean serialization
2 parents cba343a + 4493925 commit b10a33d

File tree

8 files changed

+45
-15
lines changed

8 files changed

+45
-15
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ notifications:
33
language: generic
44
env:
55
global:
6-
- SWIFT_VERSION=5.0
6+
- SWIFT_VERSION=5.1
77
matrix:
88
include:
99
- os: osx
1010
env: JOB=SwiftPM_OSX
11-
osx_image: xcode10.2
11+
osx_image: xcode11
1212
- os: linux
1313
env: JOB=SwiftPM_linux
14-
dist: trusty
14+
dist: bionic
1515
sudo: required
1616
install:
1717
- travis_retry eval "$(curl -sL https://gist.githubusercontent.com/kylef/5c0475ff02b7c7671d2a/raw/9f442512a46d7a2af7b850d65a7e9bd31edfb09b/swiftenv-install.sh)"

Package.resolved

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/GraphQL/Map/AnyCoder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2048,7 +2048,7 @@ extension _AnyDecoder {
20482048
fileprivate func unbox(_ value: Any, as type: Bool.Type) throws -> Bool? {
20492049
guard !(value is NSNull) else { return nil }
20502050

2051-
#if DEPLOYMENT_RUNTIME_SWIFT
2051+
#if DEPLOYMENT_RUNTIME_SWIFT || os(Linux)
20522052
// Bridging differences require us to split implementations here
20532053
guard let number = __SwiftValue.store(value) as? NSNumber else {
20542054
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)

Sources/GraphQL/Map/Map.swift

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@ public enum MapError : Error {
1515

1616
public enum Map {
1717
case null
18+
case bool(Bool)
1819
case number(Number)
1920
case string(String)
2021
case array([Map])
2122
case dictionary([String: Map])
2223

23-
public static func bool(_ value: Bool) -> Map {
24-
return .number(Number(value))
25-
}
26-
2724
public static func int(_ value: Int) -> Map {
2825
return .number(Number(value))
2926
}
@@ -47,7 +44,7 @@ extension Map {
4744
}
4845

4946
public init(_ bool: Bool) {
50-
self.init(Number(bool))
47+
self = .bool(bool)
5148
}
5249

5350
public init(_ int: Int) {
@@ -211,7 +208,7 @@ extension Map {
211208

212209
extension Map {
213210
public var bool: Bool? {
214-
return try? (get() as Number).boolValue
211+
return try? get()
215212
}
216213

217214
public var int: Int? {
@@ -246,6 +243,9 @@ extension Map {
246243
switch self {
247244
case .null:
248245
return false
246+
247+
case let .bool(value):
248+
return value
249249

250250
case let .number(number):
251251
return number.boolValue
@@ -321,6 +321,9 @@ extension Map {
321321
switch self {
322322
case .null:
323323
return "null"
324+
325+
case let .bool(value):
326+
return "\(value)"
324327

325328
case let .number(number):
326329
return number.stringValue
@@ -379,6 +382,8 @@ extension Map {
379382
switch self {
380383
case let .number(value as T):
381384
return value
385+
case let .bool(value as T):
386+
return value
382387
case let .string(value as T):
383388
return value
384389
case let .array(value as T):
@@ -608,6 +613,8 @@ extension Map : Codable {
608613
switch self {
609614
case .null:
610615
try container.encodeNil()
616+
case let .bool(value):
617+
try container.encode(value)
611618
case let .number(number):
612619
try container.encode(number.doubleValue)
613620
case let .string(string):
@@ -647,6 +654,8 @@ extension Map : Hashable {
647654
switch self {
648655
case .null:
649656
hasher.combine(0)
657+
case let .bool(value):
658+
hasher.combine(value)
650659
case let .number(number):
651660
hasher.combine(number)
652661
case let .string(string):
@@ -743,6 +752,8 @@ extension Map {
743752
switch map {
744753
case .null:
745754
return "null"
755+
case let .bool(value):
756+
return value.description
746757
case let .number(number):
747758
return number.description
748759
case .string(let string):

Sources/GraphQL/Map/MapCoder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2051,7 +2051,7 @@ extension _MapDecoder {
20512051
fileprivate func unbox(_ value: Any, as type: Bool.Type) throws -> Bool? {
20522052
guard !(value is NSNull) else { return nil }
20532053

2054-
#if DEPLOYMENT_RUNTIME_SWIFT
2054+
#if DEPLOYMENT_RUNTIME_SWIFT || os(Linux)
20552055
// Bridging differences require us to split implementations here
20562056
guard let number = __SwiftValue.store(value) as? NSNumber else {
20572057
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)

Sources/GraphQL/Map/MapSerialization.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public struct MapSerialization {
3636
switch map {
3737
case .null:
3838
return NSNull()
39+
case let .bool(value):
40+
return value as NSObject
3941
case var .number(number):
4042
return number.number
4143
case let .string(string):

Sources/GraphQL/Type/Introspection.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ let __Type: GraphQLObjectType = try! GraphQLObjectType(
229229
let fieldMap = type.fields
230230
var fields = Array(fieldMap.values).sorted(by: { $0.name < $1.name })
231231

232-
if !arguments["includeDeprecated"].bool! {
232+
if !(arguments["includeDeprecated"].bool ?? false) {
233233
fields = fields.filter({ !$0.isDeprecated })
234234
}
235235

@@ -240,7 +240,7 @@ let __Type: GraphQLObjectType = try! GraphQLObjectType(
240240
let fieldMap = type.fields
241241
var fields = Array(fieldMap.values).sorted(by: { $0.name < $1.name })
242242

243-
if !arguments["includeDeprecated"].bool! {
243+
if !(arguments["includeDeprecated"].bool ?? false) {
244244
fields = fields.filter({ !$0.isDeprecated })
245245
}
246246

@@ -285,7 +285,7 @@ let __Type: GraphQLObjectType = try! GraphQLObjectType(
285285

286286
var values = type.values
287287

288-
if !arguments["includeDeprecated"].bool! {
288+
if !(arguments["includeDeprecated"].bool ?? false) {
289289
values = values.filter({ !$0.isDeprecated })
290290
}
291291

Tests/LinuxMain.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ XCTMain([
66
testCase(StarWarsQueryTests.allTests),
77
testCase(StarWarsIntrospectionTests.allTests),
88
testCase(StarWarsValidationTests.allTests),
9-
testCase(MapTests.allTests),
109
testCase(LexerTests.allTests),
1110
testCase(ParserTests.allTests),
1211
testCase(SchemaParserTests.allTests),

0 commit comments

Comments
 (0)