Skip to content

Commit

Permalink
feat: add result builder
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Oct 6, 2022
1 parent 03e4ff5 commit f674d7e
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 86 deletions.
5 changes: 3 additions & 2 deletions PlantUMLKeyboard/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ let package = Package(
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: "https://github.com/siteline/SwiftUI-Introspect.git", branch: "master" ),
.package(path: "../PlantUMLFramework" ),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "PlantUMLKeyboard",
dependencies: []),
dependencies: ["PlantUMLFramework"]),
.testTarget(
name: "PlantUMLKeyboardTests",
dependencies: ["PlantUMLKeyboard"]),
dependencies: ["PlantUMLKeyboard", "PlantUMLFramework"]),
]
)
197 changes: 113 additions & 84 deletions PlantUMLKeyboard/Sources/PlantUMLKeyboard/PlantUMLSymbol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import UIKit


struct Symbol : Identifiable, CustomStringConvertible {
var description: String {
return id
Expand Down Expand Up @@ -36,63 +35,83 @@ struct Symbol : Identifiable, CustomStringConvertible {
//
// MARK: COMMON DIAGRAMS
//
fileprivate let common_symbols = [

@Symbol.LineBuilder
fileprivate func _common_symbols() -> [[Symbol]] {

[
Symbol("title", "title my title"),
Symbol("header", "header my header"),
Symbol("footer", "footer my footer"),
],
[
Symbol("allow_mixing"),
Symbol( "hide empty members"),
Symbol( "skinparam shadowing false"),
],
[
Symbol("actor", "actor \"my actor\" as a1"),
]
Symbol.Line {
("title", "title my title")
("header", "header my header")
("footer", "footer my footer")
}

]
Symbol.Line {
("allow mixing", "allow_mixing")
"hide empty members"
("shadowing false", "skinparam shadowing false")
("linetype ortho", "skinparam linetype ortho")
("left to right", "left to right direction")
("top to bottom", "top to bottom direction")
}

Symbol.Line {
"[#red]"
"#line.dashed"
}

}

fileprivate let common_symbols = {
_common_symbols()
}()

//
// MARK: SEQUENCE DIAGRAMS
//

fileprivate let sequence_symbols = [
@Symbol.LineBuilder
fileprivate func _sequence_symbols() -> [[Symbol]] {

[
Symbol("autonumber")
],
[
Symbol("participant","participant \"my participant\" as p1"),
Symbol("boundary", "boundary \"my boundary\" as b1"),
Symbol("control", "control \"my control\" as c1"),
Symbol("entity", "entity \"my entity\" as e1"),
Symbol("database", "database \"my database\" as db1"),
Symbol("collections","collections \"my collections\" as cc1" ),
Symbol("queue", "queue \"my queue\" as q1")
],
Symbol.Line {
"autonumber"
}

Symbol.Line {
("actor", "actor \"my actor\" as a1")
("participant","participant \"my participant\" as p1")
("boundary", "boundary \"my boundary\" as b1")
("control", "control \"my control\" as c1")
("entity", "entity \"my entity\" as e1")
("database", "database \"my database\" as db1")
("collections","collections \"my collections\" as cc1" )
("queue", "queue \"my queue\" as q1")
}

[
Symbol("->x"),
Symbol("->"),
Symbol("->>"),
Symbol("-\\\\"),
Symbol("\\\\-"),
Symbol("//--"),
Symbol("->o"),
Symbol("o\\\\--"),
Symbol("<->"),
Symbol("<->o"),
],
Symbol.Line {
"->x"
"->"
"->>"
"-\\\\"
"\\\\-"
"//--"
"->o"
"o\\\\--"
"<->"
"<->o"

}

[
Symbol("[#red]"),
Symbol("note left", "note left /' of participant '/", ["this note is displayed left", "end note"]),
Symbol("note right", "note right /' of participant '/", ["this note is displayed right", "end note"]),
Symbol("note over", "note over participant1 /', participant2 '/", ["this note is displayed over participant1", "end note"]),
]
]
Symbol.Line {
"[#red]"
("note left", "note left /' of p1 '/", ["this note is displayed left", "end note"])
("note right", "note right /' of p1 '/", ["this note is displayed right", "end note"])
("note over", "note over p1 /', p2 '/", ["this note is displayed over participant1", "end note"])
}
}

fileprivate let sequence_symbols = {
_sequence_symbols()
}()

fileprivate let sequence_images:[[UIImage?]] = {

Expand All @@ -109,42 +128,52 @@ fileprivate let sequence_images:[[UIImage?]] = {
// MARK: DEPLOYMENT DIAGRAMS
//

fileprivate let deployment_symbols = [

@Symbol.LineBuilder
fileprivate func _deployment_symbols() -> [[Symbol]] {

[
Symbol("actor"),
Symbol("agent"),
Symbol("artifact"),
Symbol("boundary"),
Symbol("card"),
Symbol("circle"),
Symbol("cloud"),
Symbol("collections" ),
Symbol("component" ),
Symbol("control" ),
Symbol("person" ),
Symbol("queue" ),
Symbol("rectangle" ),
],
[
Symbol("database" ),
Symbol("entity" ),
Symbol("file" ),
Symbol("folder" ),
Symbol("frame" ),
Symbol("hexagon" ),
Symbol("interface" ),
Symbol("label" ),
Symbol("node" ),
Symbol("package" ),
Symbol("stack" ),
Symbol("storage" ),
Symbol("usecase" ),
],
Symbol.Line {
"actor"
"agent"
"artifact"
"boundary"
"card"
"circle"
"cloud"
"collections"
"component"
"control"
"person"
"queue"
("rectangle", "rectangle \"Rect1\\n\" as r1 {", ["}"])
}

Symbol.Line {
("database", "database db1")
"entity"
"file"
"folder"
"frame"
"hexagon"
"interface"
"label"
"node"
"package"
"stack"
"storage"
"usecase"
}

Symbol.Line {
"#line.dashed"
"#line.dotted"
}

[
]
]
}

fileprivate var deployment_symbols = {
_deployment_symbols()
}()

//
// MARK: SYMBOL GROUPS
Expand All @@ -153,7 +182,7 @@ enum PlantUMLSymbolGroup : String, CaseIterable {
case common = "Commons"
case sequence = "Sequence"
case deployment = "Deployment"

var symbols: [[ Symbol ]] {
switch self {
case .common:
Expand All @@ -168,7 +197,7 @@ enum PlantUMLSymbolGroup : String, CaseIterable {
var images: [[ UIImage? ]] {
switch self {
case .sequence:
return sequence_images
return [] // sequence_images
default:
return []
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// File.swift
//
//
// Created by Bartolomeo Sorrentino on 06/10/22.
//

import Foundation

extension Symbol {

@resultBuilder
struct Builder {
typealias PART2 = (String, String)
typealias PART3 = (String, String, [String])

static func buildBlock(_ parts: Any...) -> [Symbol] {

parts.compactMap { elem in

if let sym = elem as? Symbol {
return sym
}
else if let str = elem as? String {
return Symbol(str)
}
else if let part2 = elem as? PART2 {
return Symbol(part2.0, part2.1)
}
else if let part3 = elem as? PART3 {
return Symbol(part3.0, part3.1, part3.2)
}

return nil
}
}
}

struct Line {

var content: [Symbol]

init( @Builder content: () -> [Symbol] ) {
self.content = content()
}
}

@resultBuilder
struct LineBuilder {

static func buildBlock(_ parts: Line...) -> [[Symbol]] {
parts.compactMap { $0.content }
}
}

}


Original file line number Diff line number Diff line change
@@ -1,11 +1,48 @@
import XCTest
@testable import PlantUMLKeyboard


final class PlantUMLKeyboardTests: XCTestCase {

@Symbol.LineBuilder func makeDeploymentSymbols() -> [[Symbol]] {

Symbol.Line {
"actor"
"agent"
"artifact"
"boundary"
"card"
"circle"
"cloud"
"collections"
"component"
"control"
"person"
("queue", "queue as q1")
"rectangle"
}

Symbol.Line {}

}

func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct
// results.
// XCTAssertEqual(PlantUMLKeyboardView().text, "Hello, World!")


let symbols = makeDeploymentSymbols()

XCTAssertEqual( symbols.count, 2 )

XCTAssertEqual( symbols[0].count, 13 )
XCTAssertEqual( symbols[1].count, 0 )

XCTAssertEqual( symbols[0][11].value, "queue as q1" )

print( symbols )

}
}

0 comments on commit f674d7e

Please sign in to comment.