Skip to content

Commit

Permalink
further swift progress
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmossas committed Aug 9, 2024
1 parent 7f8daf7 commit 42849d4
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 77 deletions.
2 changes: 1 addition & 1 deletion languages/swift/swift-codegen-reference/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let package = Package(
targets: ["SwiftCodegenReference"]),
],
dependencies: [
.package(url: "https://github.com/tristanhimmelman/ObjectMapper.git", .upToNextMajor(from: "4.1.0")),
.package(url: "https://github.com/tristanhimmelman/ObjectMapper.git", .upToNextMajor(from: "4.1.0")),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public struct Book: ExampleClientModel, Equatable {
public var createdAt: Date = Date.now
public var updatedAt: Date = Date.now

public init() {}
public init(
id: String,
name: String,
Expand All @@ -55,6 +54,7 @@ public struct Book: ExampleClientModel, Equatable {
self.createdAt = createdAt
self.updatedAt = updatedAt
}
public init() {}
public init?(map: Map) {}

public mutating func mapping(map: Map) {
Expand Down Expand Up @@ -86,35 +86,33 @@ public struct BookParams: ExampleClientModel {
}
public init() {}
public init?(map: ObjectMapper.Map) {}

public mutating func mapping(map: Map) {
self.bookId <- map["bookId"]
}

public func toQueryString() -> String {
var __queryParts: [String] = []
__queryParts.append("bookId=\(self.bookId)")
return __queryParts.joined(separator: "&")
}
}

struct NestedObject: ExampleClientModel {
public struct NestedObject: ExampleClientModel {
public var id: String = ""
public var content: String = ""
init(
public init(
id: String,
content: String
) {
self.id = id
self.content = content
}
init() {}
init?(map: Map) {}
mutating func mapping(map: Map) {
public init() {}
public init?(map: Map) {}
public mutating func mapping(map: Map) {
self.id <- map["id"]
self.content <- map["content"]
}
func toQueryString() -> String {
public func toQueryString() -> String {
var __queryParts: [String] = []
__queryParts.append("id=\(self.id)")
__queryParts.append("content=\(self.content)")
Expand All @@ -123,31 +121,71 @@ struct NestedObject: ExampleClientModel {
}
}

struct ObjectWithEveryType: ExampleClientModel {
var string: String = ""
var boolean: Bool = false
var timestamp: Date = Date.now
var float32: Float32 = 0.0
var float64: Float64 = 0.0
var int8: Int8 = 0
var uint8: UInt8 = 0
var int16: Int16 = 0
var uint16: UInt16 = 0
var int32: Int32 = 0
var uint32: UInt32 = 0
var int64: Int64 = 0
var uint64: UInt64 = 0
var `enum`: Enumerator = Enumerator.foo
var object: NestedObject = NestedObject()
var array: [Bool] = []
var record: Dictionary<String, Bool> = Dictionary()
var discriminator: Discriminator = Discriminator.a(DiscriminatorA())
var any: Any? = nil

init() {}
init?(map: Map) {}

mutating func mapping(map: Map) {
public struct ObjectWithEveryType: ExampleClientModel {
public var string: String = ""
public var boolean: Bool = false
public var timestamp: Date = Date.now
public var float32: Float32 = 0.0
public var float64: Float64 = 0.0
public var int8: Int8 = 0
public var uint8: UInt8 = 0
public var int16: Int16 = 0
public var uint16: UInt16 = 0
public var int32: Int32 = 0
public var uint32: UInt32 = 0
public var int64: Int64 = 0
public var uint64: UInt64 = 0
public var `enum`: Enumerator = Enumerator.foo
public var object: NestedObject = NestedObject()
public var array: [Bool] = []
public var record: Dictionary<String, Bool> = Dictionary()
public var discriminator: Discriminator = Discriminator.a(DiscriminatorA())
public var any: Any? = nil

public init(
string: String,
boolean: Bool,
timestamp: Date,
float32: Float32,
float64: Float64,
int8: Int8,
uint8: UInt8,
int16: Int16,
uint16: UInt16,
int32: Int32,
uint32: UInt32,
int64: Int64,
uint64: UInt64,
`enum`: Enumerator,
object: NestedObject,
array: [Bool],
record: Dictionary<String, Bool>,
discriminator: Discriminator,
any: Any?
) {
self.string = string
self.boolean = boolean
self.timestamp = timestamp
self.float32 = float32
self.float64 = float64
self.int8 = int8
self.uint8 = uint8
self.int16 = int16
self.uint16 = uint16
self.int32 = int32
self.uint32 = uint32
self.int64 = int64
self.uint64 = uint64
self.`enum` = `enum`
self.object = object
self.array = array
self.record = record
self.discriminator = discriminator
self.any = any
}
public init() {}
public init?(map: Map) {}
public mutating func mapping(map: Map) {
self.string <- map["string"]
self.boolean <- map["boolean"]
self.timestamp <- (map["timestamp"], __dateTransformer)
Expand All @@ -161,14 +199,14 @@ struct ObjectWithEveryType: ExampleClientModel {
self.uint32 <- map["uint32"]
self.int64 <- map["int64"]
self.uint64 <- map["uint64"]
self.enum <- map["enum"] // TODO
self.`enum` <- map["enum"] // TODO
self.object <- map["object"] // TODO
self.array <- map["array"] // TODO
self.record <- map["record"] // TODO
self.discriminator <- map["discriminator"] // TODO
self.any <- map["any"] // TODO
}

func toQueryString() -> String {
public func toQueryString() -> String {
var __queryParts: [String] = []
__queryParts.append("string=\(self.string)")
__queryParts.append("boolean=\(self.boolean)")
Expand All @@ -183,16 +221,15 @@ struct ObjectWithEveryType: ExampleClientModel {
__queryParts.append("uint32=\(self.uint32)")
__queryParts.append("int64=\(self.int64)")
__queryParts.append("uint64=\(self.uint64)")
__queryParts.append("enum=\(self.enum.serialValue())")
__queryParts.append("enum=\(self.`enum`.serialValue())")
print("[WARNING] nested objects cannot be serialized to query params. Skipping field at /ObjectWithEveryType/object.")
print("[WARNING] arrays cannot be serialized to query params. Skipping field at /ObjectWithEveryType/array.")
print("[WARNING] nested objects cannot be serialized to query params. Skipping field at /ObjectWithEveryType/record.")
print("[WARNING] nested objects cannot be serialized to query params. Skipping field at /ObjectWithEveryType/discriminator")
print("[WARNING] any's cannot be serialized to query params. Skipping field at /ObjectWithEveryType/any")
return __queryParts.joined(separator: "&")
}

static func == (left: ObjectWithEveryType, right: ObjectWithEveryType) -> Bool {
public static func == (left: ObjectWithEveryType, right: ObjectWithEveryType) -> Bool {
return
left.string == right.string &&
left.boolean == right.boolean &&
Expand All @@ -207,7 +244,7 @@ struct ObjectWithEveryType: ExampleClientModel {
left.uint32 == right.uint32 &&
left.int64 == right.int64 &&
left.uint64 == right.uint64 &&
left.enum == right.enum &&
left.`enum` == right.`enum` &&
left.object == right.object &&
left.array == right.array &&
left.record == right.record &&
Expand All @@ -216,12 +253,12 @@ struct ObjectWithEveryType: ExampleClientModel {
}
}

enum Enumerator {
public enum Enumerator {
case foo
case bar
case baz

init(string: String) {
public init(string: String) {
switch(string) {
case "FOO":
self = .foo
Expand All @@ -237,7 +274,7 @@ enum Enumerator {
}
}

func serialValue() -> String {
public func serialValue() -> String {
switch (self) {
case .foo:
return "FOO"
Expand All @@ -249,13 +286,13 @@ enum Enumerator {
}
}

enum Discriminator: ExampleClientEnum {
public enum Discriminator: ExampleClientEnum {

case a(DiscriminatorA)
case b(DiscriminatorB)
case c(DiscriminatorC)

func toJsonString() -> String {
public func toJsonString() -> String {
switch(self) {
case .a(let __innerVal):
return __innerVal.toJSONString() ?? ""
Expand All @@ -267,7 +304,7 @@ enum Discriminator: ExampleClientEnum {
}


func toQueryString() -> String {
public func toQueryString() -> String {
switch(self) {
case .a(let __innerVal):
return __innerVal.toQueryString()
Expand All @@ -279,51 +316,51 @@ enum Discriminator: ExampleClientEnum {
}
}

struct DiscriminatorA: ExampleClientModel {
public struct DiscriminatorA: ExampleClientModel {
let typeName: String = "A"
var id: String = ""
public var id: String = ""

init(
public init(
id: String
) {
self.id = id
}
init() {}
init?(map: Map) {}
public init() {}
public init?(map: Map) {}

mutating func mapping(map: Map) {
public mutating func mapping(map: Map) {
self.id <- map["id"]
}

func toQueryString() -> String {
public func toQueryString() -> String {
var __queryParts: [String] = []
__queryParts.append("type=A")
__queryParts.append("id=\(self.id)")
return __queryParts.joined(separator: "&")
}
}

struct DiscriminatorB: ExampleClientModel {
public struct DiscriminatorB: ExampleClientModel {
let typeName: String = "B"
var id: String = ""
var name: String = ""
public var id: String = ""
public var name: String = ""

init(
public init(
id: String,
name: String
) {
self.id = id
self.name = name
}
init() {}
init?(map: Map) {}
public init() {}
public init?(map: Map) {}

mutating func mapping(map: Map) {
public mutating func mapping(map: Map) {
self.id <- map["id"]
self.name <- map["name"]
}

func toQueryString() -> String {
public func toQueryString() -> String {
var __queryParts: [String] = []
__queryParts.append("type=B")
__queryParts.append("id=\(self.id)")
Expand All @@ -332,13 +369,13 @@ struct DiscriminatorB: ExampleClientModel {
}
}

struct DiscriminatorC: ExampleClientModel {
public struct DiscriminatorC: ExampleClientModel {
let typeName: String = "C"
var id: String = ""
var name: String = ""
var date: Date = Date.now
public var id: String = ""
public var name: String = ""
public var date: Date = Date.now

init(
public init(
id: String,
name: String,
date: Date
Expand All @@ -347,16 +384,16 @@ struct DiscriminatorC: ExampleClientModel {
self.name = name
self.date = date
}
init() {}
init?(map: Map) {}
public init() {}
public init?(map: Map) {}

mutating func mapping(map: Map) {
public mutating func mapping(map: Map) {
self.id <- map["id"]
self.name <- map["name"]
self.date <- (map["date"], __dateTransformer)
}

func toQueryString() -> String {
public func toQueryString() -> String {
var __queryParts: [String] = []
__queryParts.append("type=C")
__queryParts.append("id=\(self.id)")
Expand Down
11 changes: 6 additions & 5 deletions languages/swift/swift-codegen-reference/Tests/BookTests.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import XCTest
@testable import SwiftCodegenReference

let location = "../../../tests/test-files/Book.json"
let dateFormatter: ExampleClientDateFormatter = ExampleClientDateFormatter()
let targetDateStr = "2001-01-01T16:00:00.000Z"
let formatter: ExampleClientDateFormatter = ExampleClientDateFormatter()
let targetDate = dateFormatter.date(from: targetDateStr) ?? Date.now

final class BookTests: XCTestCase {
let location = "../../../tests/test-files/Book.json"
var control: Book = Book(
id: "1",
name: "The Adventures of Tom Sawyer",
createdAt: formatter.date(from: targetDateStr) ?? Date.now,
updatedAt: formatter.date(from: targetDateStr) ?? Date.now
createdAt: targetDate,
updatedAt: targetDate
)

func testFromJson() throws {
let bookJson: String? = try? NSString(contentsOfFile: location, encoding: NSUTF8StringEncoding) as String?
let bookJson: String? = try? String(contentsOfFile: location, encoding: .utf8)
let result1 = Book(JSONString: bookJson ?? "") ?? Book()
let result2: Book = Book(JSONString: control.toJSONString() ?? "") ?? Book()
XCTAssertEqual(result1, control)
Expand Down
Loading

0 comments on commit 42849d4

Please sign in to comment.