Skip to content

Commit

Permalink
Merge pull request #82 from cipi1965/master
Browse files Browse the repository at this point in the history
Fix enums
  • Loading branch information
cipi1965 authored Aug 19, 2019
2 parents 91659f6 + fa2f58f commit 046ef33
Show file tree
Hide file tree
Showing 3 changed files with 3,400 additions and 3,398 deletions.
42 changes: 22 additions & 20 deletions Rapier/Sources/RapierCLI/Generators/TelegramBotSDKGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,44 +190,46 @@ class TelegramBotSDKGenerator: CodeGenerator {
let type = fieldInfo.type
let isOptional = fieldInfo.isOptional

let jsonName = fieldName.replacingOccurrences(of: "_string", with: "")

switch (type, isOptional) {
case ("String", _), ("Int", _), ("Int64", _), ("Float", _), ("Bool", _):
var swiftyJsonPropertyType = fieldInfo.type.lowercased()
let swiftyJsonPropertyType = fieldInfo.type.lowercased()

return """
public var \(fieldName.camelized()): \(type)\(isOptional ? "?" : "") {
get { return internalJson["\(fieldName)"].\(swiftyJsonPropertyType)\(isOptional ? "" : "Value") }
set { internalJson["\(fieldName)"].\(swiftyJsonPropertyType)\(isOptional ? "" : "Value") = newValue }
get { return internalJson["\(jsonName)"].\(swiftyJsonPropertyType)\(isOptional ? "" : "Value") }
set { internalJson["\(jsonName)"].\(swiftyJsonPropertyType)\(isOptional ? "" : "Value") = newValue }
}\n\n
"""
case ("Date", true):
return """
public var \(fieldName.camelized()): Date? {
get {
guard let date = internalJson["\(fieldName)"].double else { return nil }
guard let date = internalJson["\(jsonName)"].double else { return nil }
return Date(timeIntervalSince1970: date)
}
set {
internalJson["\(fieldName)"].double = newValue?.timeIntervalSince1970
internalJson["\(jsonName)"].double = newValue?.timeIntervalSince1970
}
}\n\n
"""
case ("Date", false):
return """
public var \(fieldName.camelized()): Date {
get { return Date(timeIntervalSince1970: internalJson["\(fieldName)"].doubleValue) }
set { internalJson["\(fieldName)"].double = newValue.timeIntervalSince1970 }
get { return Date(timeIntervalSince1970: internalJson["\(jsonName)"].doubleValue) }
set { internalJson["\(jsonName)"].double = newValue.timeIntervalSince1970 }
}\n\n
"""
case (_, _):
if fieldInfo.isArrayOfArray {
if fieldInfo.isOptional {
return """
public var \(fieldName.camelized()): [[\(fieldInfo.type)]] {
get { return internalJson["\(fieldName)"].twoDArrayValue() }
get { return internalJson["\(jsonName)"].twoDArrayValue() }
set {
if newValue.isEmpty {
json["\(fieldName)"] = JSON.null
json["\(jsonName)"] = JSON.null
return
}\n"\
var rowsJson = [JSON]()
Expand All @@ -241,14 +243,14 @@ class TelegramBotSDKGenerator: CodeGenerator {
}
rowsJson.append(JSON(colsJson))
}
internalJson["\(fieldName)"] = JSON(rowsJson)
internalJson["\(jsonName)"] = JSON(rowsJson)
}
}\n\n
"""
} else {
return """
public var \(fieldName.camelized()): [[\(fieldInfo.type)]] {
get { return internalJson["\(fieldName)"].twoDArrayValue() }
get { return internalJson["\(jsonName)"].twoDArrayValue() }
set {
var rowsJson = [JSON]()
rowsJson.reserveCapacity(newValue.count)
Expand All @@ -261,7 +263,7 @@ class TelegramBotSDKGenerator: CodeGenerator {
}
rowsJson.append(JSON(colsJson))
}
internalJson["\(fieldName)"] = JSON(rowsJson)
internalJson["\(jsonName)"] = JSON(rowsJson)
}
}\n\n
"""
Expand All @@ -270,15 +272,15 @@ class TelegramBotSDKGenerator: CodeGenerator {
if fieldInfo.isOptional {
return """
public var \(fieldName.camelized()): [\(fieldInfo.type)] {
get { return internalJson["\(fieldName)"].customArrayValue() }
set { internalJson["\(fieldName)"] = newValue.isEmpty ? JSON.null : JSON.initFrom(newValue) }
get { return internalJson["\(jsonName)"].customArrayValue() }
set { internalJson["\(jsonName)"] = newValue.isEmpty ? JSON.null : JSON.initFrom(newValue) }
}\n\n
"""
} else {
return """
public var \(fieldName.camelized()): [\(fieldInfo.type)] {
get { return internalJson["\(fieldName)"].customArrayValue() }
set { internalJson["\(fieldName)"] = JSON.initFrom(newValue) }
get { return internalJson["\(jsonName)"].customArrayValue() }
set { internalJson["\(jsonName)"] = JSON.initFrom(newValue) }
}\n\n
"""
}
Expand Down Expand Up @@ -317,19 +319,19 @@ class TelegramBotSDKGenerator: CodeGenerator {
return """
public var \(fieldName.camelized()): \(fieldInfo.type)? {
get {
let value = internalJson["\(fieldName)"]
let value = internalJson["\(jsonName)"]
return value.isNullOrUnknown ? nil : \(fieldInfo.type)(internalJson: value)
}
set {
internalJson["\(fieldName)"] = newValue?.internalJson ?? JSON.null
internalJson["\(jsonName)"] = newValue?.internalJson ?? JSON.null
}
}\n\n
"""
} else {
return """
public var \(fieldName.camelized()): \(fieldInfo.type) {
get { return \(fieldInfo.type)(internalJson: internalJson["\(fieldName)"]) }
set { internalJson["\(fieldName)"] = JSON(newValue.json) }
get { return \(fieldInfo.type)(internalJson: internalJson["\(jsonName)"]) }
set { internalJson["\(jsonName)"] = JSON(newValue.json) }
}\n\n
"""
}
Expand Down
Loading

0 comments on commit 046ef33

Please sign in to comment.