Skip to content

Commit

Permalink
Introduce spread (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii authored Oct 22, 2022
1 parent e0c4ef3 commit b5b570d
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,36 @@
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "GrainDescriptor"
BuildableName = "GrainDescriptor"
BlueprintName = "GrainDescriptor"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "GrainDescriptorTests"
BuildableName = "GrainDescriptorTests"
BlueprintName = "GrainDescriptorTests"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
Expand Down Expand Up @@ -41,6 +71,15 @@
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "GrainDescriptor"
BuildableName = "GrainDescriptor"
BlueprintName = "GrainDescriptor"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
Expand Down
6 changes: 3 additions & 3 deletions Sources/GrainDescriptor/Entrypoint.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@_exported import Alamofire
import Alamofire
@_implementationOnly import Darwin.C
@_exported import Foundation
@_exported import TSCBasic
import Foundation
import TSCBasic

public struct Serialization {

Expand Down
16 changes: 16 additions & 0 deletions Sources/GrainDescriptor/Source.swift
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,14 @@ public struct GrainArray: GrainView {

}

public struct GrainObjectSpread: GrainView {

public typealias Body = Never

public var members: [GrainMember]

}

public struct GrainObject: GrainView {

public typealias Body = Never
Expand All @@ -426,6 +434,10 @@ public struct GrainObject: GrainView {
self.members = members()
}

public var spread: GrainObjectSpread {
.init(members: members)
}

public func encode(to encoder: Encoder) throws {
try members.forEach {
try $0.encode(to: encoder)
Expand Down Expand Up @@ -469,6 +481,10 @@ public struct GrainObject: GrainView {
return [element]
}

public static func buildExpression(_ element: GrainObjectSpread) -> [Element] {
element.members
}

public static func buildExpression<C: Sequence>(_ elements: C) -> [C.Element] where C.Element == Element {
Swift.Array(elements)
}
Expand Down
100 changes: 100 additions & 0 deletions Tests/GrainDescriptorTests/APNs.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@

import XCTest
import GrainDescriptor

// MARK: - Variables

let param_a = GrainMember("param_a") {
"111"
}

let simulatorTarget = GrainMember("Simulator Target Bundle") {
"app.muukii.myapp"
}

final class APNsTests: XCTestCase {

func test_aggregation() {

compare(
GrainObject {
GrainMember("a") {
["a", "b"]
}
GrainMember("a") {
["a", "b", "c"]
}
},
"""
{
"a" : [
"a",
"b",
"c"
]
}
"""
)

}

func test_spread() {

let object1 = GrainObject {
GrainMember("a") { 1 }
}

let object2 = GrainObject {
GrainMember("a") { 1 }
GrainMember("b") { 1 }
}

compare(
GrainObject {
object1.spread
object2.spread
},
"""
{
"a" : 1,
"b" : 1
}
"""
)

}

}
//
//struct APNS<Content: GrainView>: GrainView {
//
// let content: Content
//
// init(@GrainBuilder _ content: () -> Content) {
// self.content = content()
// }
//
// var body: some GrainView {
// GrainObject {
// content
// }
// }
//
//}

struct APS: GrainView {

let alert: String

var body: some GrainView {

GrainObject {
// GrainMember("alert") {
// alert
// }
}

}
}


34 changes: 19 additions & 15 deletions Tests/GrainDescriptorTests/Tests.swift
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import XCTest

@testable import GrainDescriptor
import GrainDescriptor

final class JSONDSLTests: XCTestCase {
let encoder: JSONEncoder = {
let e = JSONEncoder()
e.outputFormatting = .prettyPrinted
return e
}()

let encoder = JSONEncoder()
func toString(_ j: some GrainView) -> String {
let data = try! encoder.encode(j)
return String(data: data, encoding: .utf8)!
}

func toString(_ j: some GrainView) -> String {
let data = try! encoder.encode(j)
return String(data: data, encoding: .utf8)!
}
func compare(
_ j: some GrainView,
_ expects: String,
file: StaticString = #filePath,
line: UInt = #line
) {
XCTAssertEqual(toString(j), expects, file: file, line: line)
}

func compare(
_ j: some GrainView,
_ expects: String,
file: StaticString = #filePath,
line: UInt = #line
) {
XCTAssertEqual(toString(j), expects, file: file, line: line)
}
final class JSONDSLTests: XCTestCase {

override func setUp() {
encoder.outputFormatting = .prettyPrinted
Expand Down

0 comments on commit b5b570d

Please sign in to comment.