Skip to content

Commit

Permalink
- Moved Tree to Amplify
Browse files Browse the repository at this point in the history
- some remaining code clean up
  • Loading branch information
lawmicha committed Feb 11, 2020
1 parent 7b9d5be commit f3082c2
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 48 deletions.
20 changes: 16 additions & 4 deletions Amplify.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@
219A888123EB629800BBC5F2 /* ModelBasedGraphQLDocumentDecorator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 219A888023EB629800BBC5F2 /* ModelBasedGraphQLDocumentDecorator.swift */; };
219A888523EB897700BBC5F2 /* GraphQLRequest+AnyModelWithSync.swift in Sources */ = {isa = PBXBuildFile; fileRef = 219A888423EB897700BBC5F2 /* GraphQLRequest+AnyModelWithSync.swift */; };
219A888723EB89C200BBC5F2 /* GraphQLRequestAnyModelWithSyncTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 219A888623EB89C200BBC5F2 /* GraphQLRequestAnyModelWithSyncTests.swift */; };
219A88ED23F3309800BBC5F2 /* Tree.swift in Sources */ = {isa = PBXBuildFile; fileRef = 219A88EC23F3309800BBC5F2 /* Tree.swift */; };
219A88EF23F3358F00BBC5F2 /* TreeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 219A88EE23F3358F00BBC5F2 /* TreeTests.swift */; };
219A88F123F3379900BBC5F2 /* GraphQLDocumentInput.swift in Sources */ = {isa = PBXBuildFile; fileRef = 219A88F023F3379900BBC5F2 /* GraphQLDocumentInput.swift */; };
21D1CE8C2334233F0003BAA8 /* AuthError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21D1CE8B2334233F0003BAA8 /* AuthError.swift */; };
21D79FDA237617C60057D00D /* SubscriptionEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21D79FD9237617C60057D00D /* SubscriptionEvent.swift */; };
21D79FE12377BF4B0057D00D /* AuthProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21D79FE02377BF4B0057D00D /* AuthProvider.swift */; };
Expand Down Expand Up @@ -218,7 +221,7 @@
B9FAA1272388BE91009414B4 /* List+LazyLoad.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9FAA1262388BE91009414B4 /* List+LazyLoad.swift */; };
B9FAA13A238BBADE009414B4 /* List+Combine.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9FAA139238BBADE009414B4 /* List+Combine.swift */; };
B9FAA13C238BBE67009414B4 /* DataStoreCallback+Combine.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9FAA13B238BBE67009414B4 /* DataStoreCallback+Combine.swift */; };
B9FAA175238EFC5A009414B4 /* String+Casing.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9FAA174238EFC59009414B4 /* String+Casing.swift */; };
B9FAA175238EFC5A009414B4 /* String+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9FAA174238EFC59009414B4 /* String+Extensions.swift */; };
B9FAA180238FBB5D009414B4 /* Model+Array.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9FAA17F238FBB5D009414B4 /* Model+Array.swift */; };
B9FB05F82383740D00DE1FD4 /* DataStoreStatement.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9FB05F72383740D00DE1FD4 /* DataStoreStatement.swift */; };
FA0173352375F8A5005DDDFC /* LoggingError.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA0173342375F8A5005DDDFC /* LoggingError.swift */; };
Expand Down Expand Up @@ -606,6 +609,9 @@
219A888023EB629800BBC5F2 /* ModelBasedGraphQLDocumentDecorator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ModelBasedGraphQLDocumentDecorator.swift; sourceTree = "<group>"; };
219A888423EB897700BBC5F2 /* GraphQLRequest+AnyModelWithSync.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "GraphQLRequest+AnyModelWithSync.swift"; sourceTree = "<group>"; };
219A888623EB89C200BBC5F2 /* GraphQLRequestAnyModelWithSyncTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GraphQLRequestAnyModelWithSyncTests.swift; sourceTree = "<group>"; };
219A88EC23F3309800BBC5F2 /* Tree.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tree.swift; sourceTree = "<group>"; };
219A88EE23F3358F00BBC5F2 /* TreeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TreeTests.swift; sourceTree = "<group>"; };
219A88F023F3379900BBC5F2 /* GraphQLDocumentInput.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GraphQLDocumentInput.swift; sourceTree = "<group>"; };
21D1CE8B2334233F0003BAA8 /* AuthError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthError.swift; sourceTree = "<group>"; };
21D79FD9237617C60057D00D /* SubscriptionEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubscriptionEvent.swift; sourceTree = "<group>"; };
21D79FE02377BF4B0057D00D /* AuthProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthProvider.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -841,7 +847,7 @@
B9FAA1262388BE91009414B4 /* List+LazyLoad.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "List+LazyLoad.swift"; sourceTree = "<group>"; };
B9FAA139238BBADE009414B4 /* List+Combine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "List+Combine.swift"; sourceTree = "<group>"; };
B9FAA13B238BBE67009414B4 /* DataStoreCallback+Combine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DataStoreCallback+Combine.swift"; sourceTree = "<group>"; };
B9FAA174238EFC59009414B4 /* String+Casing.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Casing.swift"; sourceTree = "<group>"; };
B9FAA174238EFC59009414B4 /* String+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Extensions.swift"; sourceTree = "<group>"; };
B9FAA17F238FBB5D009414B4 /* Model+Array.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Model+Array.swift"; sourceTree = "<group>"; };
B9FB05F72383740D00DE1FD4 /* DataStoreStatement.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataStoreStatement.swift; sourceTree = "<group>"; };
BAFD88194E245D6B82399825 /* Pods-Amplify-AmplifyAWSPlugins-AWSPinpointAnalyticsPlugin-AWSPinpointAnalyticsPluginTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Amplify-AmplifyAWSPlugins-AWSPinpointAnalyticsPlugin-AWSPinpointAnalyticsPluginTests.release.xcconfig"; path = "Target Support Files/Pods-Amplify-AmplifyAWSPlugins-AWSPinpointAnalyticsPlugin-AWSPinpointAnalyticsPluginTests/Pods-Amplify-AmplifyAWSPlugins-AWSPinpointAnalyticsPlugin-AWSPinpointAnalyticsPluginTests.release.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1223,6 +1229,7 @@
2129BE192394806B006363A1 /* Support */ = {
isa = PBXGroup;
children = (
219A88F023F3379900BBC5F2 /* GraphQLDocumentInput.swift */,
212CE6FD23E9E5A2007D8E71 /* GraphQLDocumentnputValue.swift */,
2129BE1B2394806B006363A1 /* Model+GraphQL.swift */,
212CE71023E9EA6A007D8E71 /* ModelField+GraphQL.swift */,
Expand Down Expand Up @@ -2036,7 +2043,8 @@
216879FD23636A0A004A056E /* RepeatingTimer.swift */,
FAE4145E23999BC900CE94C2 /* Result+Void.swift */,
FA56F72422B14B6A0039754A /* Resumable.swift */,
B9FAA174238EFC59009414B4 /* String+Casing.swift */,
B9FAA174238EFC59009414B4 /* String+Extensions.swift */,
219A88EC23F3309800BBC5F2 /* Tree.swift */,
);
path = Support;
sourceTree = "<group>";
Expand Down Expand Up @@ -2379,6 +2387,7 @@
FACD264A2386E8F10068FBE6 /* JSONValue+SubscriptTests.swift */,
FA09B9422321CB0C000E064D /* JSONValueTests.swift */,
FA176EDC2385943000C5C5F9 /* NotificationListeningAnalyticsPlugin.swift */,
219A88EE23F3358F00BBC5F2 /* TreeTests.swift */,
);
path = CoreTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -3426,6 +3435,7 @@
21420A8F237222A900FA140C /* AWSIAMConfiguration.swift in Sources */,
2129BE1723948065006363A1 /* GraphQLRequest+Model.swift in Sources */,
219A888523EB897700BBC5F2 /* GraphQLRequest+AnyModelWithSync.swift in Sources */,
219A88F123F3379900BBC5F2 /* GraphQLDocumentInput.swift in Sources */,
21420AA0237222A900FA140C /* AWSAuthorizationType.swift in Sources */,
21420A9F237222A900FA140C /* AWSMobileClientAdapter.swift in Sources */,
212CE6FC23E9E523007D8E71 /* SelectionSet.swift in Sources */,
Expand Down Expand Up @@ -3513,6 +3523,7 @@
FAC23544227A055200424678 /* ConfigurationError.swift in Sources */,
95DAAB44237E639E0028544F /* ConvertResult.swift in Sources */,
FAC428A4235F802A0000F221 /* AmplifyAPICategory+InterceptorBehavior.swift in Sources */,
219A88ED23F3309800BBC5F2 /* Tree.swift in Sources */,
95DAAB47237E639E0028544F /* IdentifyLabelsResult.swift in Sources */,
FAC23547227A055200424678 /* Plugin.swift in Sources */,
B9FAA180238FBB5D009414B4 /* Model+Array.swift in Sources */,
Expand All @@ -3531,7 +3542,7 @@
95DAAB28237E63370028544F /* Celebrity.swift in Sources */,
FA0933812384749A00C2FD5F /* LoggingCategory+Logger.swift in Sources */,
FA6BC87F235F5DAE0001A882 /* APICategoryInterceptorBehavior.swift in Sources */,
B9FAA175238EFC5A009414B4 /* String+Casing.swift in Sources */,
B9FAA175238EFC5A009414B4 /* String+Extensions.swift in Sources */,
95DAAB25237E63370028544F /* Entity.swift in Sources */,
FA09337C23844E9F00C2FD5F /* GraphQLOperationRequest.swift in Sources */,
FAC23545227A055200424678 /* AmplifyConfiguration.swift in Sources */,
Expand Down Expand Up @@ -3768,6 +3779,7 @@
FACD264D2386E8F10068FBE6 /* JSONValue+KeyPathTests.swift in Sources */,
FA9FB782232AA26500C04D32 /* DefaultHubPluginCustomChannelTests.swift in Sources */,
FAC23567227A056600424678 /* APICategoryClientRESTTests.swift in Sources */,
219A88EF23F3358F00BBC5F2 /* TreeTests.swift in Sources */,
FACA35EB2326B217000E74F6 /* AmplifyConfigurationInitializationTests.swift in Sources */,
FAD3937A23820CDB00463F5E /* DataStoreCategoryClientAPITests.swift in Sources */,
FAC23599227A598B00424678 /* DefaultHubPluginTests.swift in Sources */,
Expand Down
File renamed without changes.
25 changes: 25 additions & 0 deletions Amplify/Core/Support/Tree.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Copyright 2018-2020 Amazon.com,
// Inc. or its affiliates. All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import Foundation

/// A Tree data type with a `value` of some type `E` and `children` subtrees.
public class Tree<E> {
public var value: E
public var children: [Tree<E>] = []
public weak var parent: Tree<E>?

public init(value: E) {
self.value = value
}

/// Add a child to the tree's children and set a weak reference from the child to the parent (`self`)
public func addChild(settingParentOf child: Tree) {
children.append(child)
child.parent = self
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -273,5 +273,4 @@ class GraphQLSyncBasedTests: XCTestCase {
wait(for: [completeInvoked], timeout: TestCommonConstants.networkTimeout)
return result
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public struct ConflictResolutionDecorator: ModelBasedGraphQLDocumentDecorator {
}

if let lastSync = lastSync, case .query = document.operationType {
inputs["lastSync"] = GraphQLDocumentInput(type: "AWSTimestamp", value: .scalarOrString(lastSync))
inputs["lastSync"] = GraphQLDocumentInput(type: "AWSTimestamp", value: .scalar(lastSync))
}

if let selectionSet = document.selectionSet {
Expand All @@ -54,11 +54,11 @@ public struct ConflictResolutionDecorator: ModelBasedGraphQLDocumentDecorator {
case .value:
break
case .model:
selectionSet.add(child: .init(value: .init(name: "_version", fieldType: .value)))
selectionSet.add(child: .init(value: .init(name: "_deleted", fieldType: .value)))
selectionSet.add(child: .init(value: .init(name: "_lastChangedAt", fieldType: .value)))
selectionSet.addChild(settingParentOf: .init(value: .init(name: "_version", fieldType: .value)))
selectionSet.addChild(settingParentOf: .init(value: .init(name: "_deleted", fieldType: .value)))
selectionSet.addChild(settingParentOf: .init(value: .init(name: "_lastChangedAt", fieldType: .value)))
case .pagination:
selectionSet.add(child: .init(value: .init(name: "startedAt", fieldType: .value)))
selectionSet.addChild(settingParentOf: .init(value: .init(name: "startedAt", fieldType: .value)))
}

selectionSet.children.forEach { child in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Amplify

/// Decorate the GraphQL document with the data from an instance of the model. This is added as a single parameter
/// called "input" that can be referenced by other decorators to append additional document inputs. This decorator
/// has a dependency on the `DirectiveDecorator` as it constructs the input's type using the document name.
/// constructs the input's type using the document name.
public struct ModelDecorator: ModelBasedGraphQLDocumentDecorator {

private let model: Model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import Foundation
import Amplify

/// Decorate the GraphQLDocument with the value of `Model.Identifier` for a "delete" mutation or "get" query. This
/// decorate has a dependency on `DirectiveDecorator` as it constructs the input's type using the document name for
/// mutations.
/// Decorate the GraphQLDocument with the value of `Model.Identifier` for a "delete" mutation or "get" query.
public struct ModelIdDecorator: ModelBasedGraphQLDocumentDecorator {

private let id: Model.Identifier
Expand All @@ -27,7 +25,7 @@ public struct ModelIdDecorator: ModelBasedGraphQLDocumentDecorator {
inputs["input"] = GraphQLDocumentInput(type: "\(document.name.pascalCased())Input!",
value: .object(["id": id]))
} else if case .query = document.operationType {
inputs["id"] = GraphQLDocumentInput(type: "ID!", value: .scalarOrString(id))
inputs["id"] = GraphQLDocumentInput(type: "ID!", value: .scalar(id))
}

return document.copy(inputs: inputs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public struct PaginationDecorator: ModelBasedGraphQLDocumentDecorator {
var inputs = document.inputs

if let limit = limit {
inputs["limit"] = GraphQLDocumentInput(type: "Int", value: .scalarOrString(limit))
inputs["limit"] = GraphQLDocumentInput(type: "Int", value: .scalar(limit))
} else {
inputs["limit"] = GraphQLDocumentInput(type: "Int", value: .scalarOrString(1_000))
inputs["limit"] = GraphQLDocumentInput(type: "Int", value: .scalar(1_000))
}

if let nextToken = nextToken {
inputs["nextToken"] = GraphQLDocumentInput(type: "String", value: .scalarOrString(nextToken))
inputs["nextToken"] = GraphQLDocumentInput(type: "String", value: .scalar(nextToken))
}

if let selectionSet = document.selectionSet {
Expand All @@ -47,8 +47,8 @@ public struct PaginationDecorator: ModelBasedGraphQLDocumentDecorator {
let paginatedNode = SelectionSetField(fieldType: .pagination)
let newRoot = SelectionSet(value: paginatedNode)
selectionSet.value.name = "items"
newRoot.add(child: selectionSet)
newRoot.add(child: SelectionSet(value: SelectionSetField(name: "nextToken", fieldType: .value)))
newRoot.addChild(settingParentOf: selectionSet)
newRoot.addChild(settingParentOf: SelectionSet(value: SelectionSetField(name: "nextToken", fieldType: .value)))
return newRoot
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ extension SingleDirectiveGraphQLDocument {
switch input.value.value {
case .object(let values):
variables.updateValue(values, forKey: input.key)
case .scalarOrString(let value):
case .scalar(let value):
variables.updateValue(value, forKey: input.key)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Copyright 2018-2020 Amazon.com,
// Inc. or its affiliates. All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import Foundation

/// Contains the `type` of the GraphQL document input parameter as a string value and `GraphQLDocumentInputValue`
public struct GraphQLDocumentInput {

public var type: String

public var value: GraphQLDocumentInputValue

public init(type: String, value: GraphQLDocumentInputValue) {
self.type = type
self.value = value
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,34 @@ import Foundation
/// A container to hold either an object or a value, useful for storing document inputs and allowing manipulation at
/// the first level of the object
public enum GraphQLDocumentInputValue {
case scalarOrString(Any)
case scalar(GraphQLDocumentValueRepresentable)
case object([String: Any?])
}

/// Contains the `type` of the GraphQL document input parameter as a string value and `GraphQLDocumentInputValue`
public struct GraphQLDocumentInput {
public protocol GraphQLDocumentValueRepresentable {
var graphQLDocumentValue: String { get }
}

extension Int: GraphQLDocumentValueRepresentable {
public var graphQLDocumentValue: String {
return "\(self)"
}
}

public var type: String
extension String: GraphQLDocumentValueRepresentable {
public var graphQLDocumentValue: String {
return self
}
}

public var value: GraphQLDocumentInputValue
extension Bool: GraphQLDocumentValueRepresentable {
public var graphQLDocumentValue: String {
return "\(self)"
}
}

public init(type: String, value: GraphQLDocumentInputValue) {
self.type = type
self.value = value
extension Decimal: GraphQLDocumentValueRepresentable {
public var graphQLDocumentValue: String {
return "\(self)"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,7 @@
import Foundation
import Amplify

public typealias SelectionSet = TreeNode<SelectionSetField>

public class TreeNode<E> {
var value: E
var children: [TreeNode<E>] = []
weak var parent: TreeNode<E>?

init(value: E) {
self.value = value
}

func add(child: TreeNode) {
children.append(child)
child.parent = self
}
}
public typealias SelectionSet = Tree<SelectionSetField>

public enum SelectionSetFieldType {
case pagination
Expand Down Expand Up @@ -54,13 +39,13 @@ extension SelectionSet {
if isRequiredAssociation, let associatedModel = field.associatedModel {
let child = SelectionSet(value: .init(name: field.name, fieldType: .model))
child.withModelFields(associatedModel.schema.graphQLFields)
self.add(child: child)
self.addChild(settingParentOf: child)
} else {
self.add(child: .init(value: .init(name: field.graphQLName, fieldType: .value)))
self.addChild(settingParentOf: .init(value: .init(name: field.graphQLName, fieldType: .value)))
}
}

add(child: .init(value: .init(name: "__typename", fieldType: .value)))
addChild(settingParentOf: .init(value: .init(name: "__typename", fieldType: .value)))
}

/// Generate the string value of the `SelectionSet` used in the GraphQL query document
Expand Down
29 changes: 29 additions & 0 deletions AmplifyTests/CoreTests/TreeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// Copyright 2018-2020 Amazon.com,
// Inc. or its affiliates. All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import XCTest
@testable import Amplify

class TreeTests: XCTestCase {

func testTreeWithChildren() {
let tree = Tree<Int>(value: 0)
let child1 = Tree<Int>(value: 1)
let child2 = Tree<Int>(value: 2)

tree.addChild(settingParentOf: child1)
tree.addChild(settingParentOf: child2)

XCTAssertNotNil(tree)
XCTAssertEqual(tree.value, 0)
XCTAssertEqual(tree.children.count, 2)
XCTAssertNotNil(child1.parent)
XCTAssertEqual(child1.parent?.value, tree.value)
XCTAssertNotNil(child2.parent)
XCTAssertEqual(child2.parent?.value, tree.value)
}
}

0 comments on commit f3082c2

Please sign in to comment.