1

I'm starting to use the Kitura framework to migrate my current node.js backend to swift and I'm starting by implementing all the routes with mocked data.

Anyways, right now im at a very early stage and started to develop some mocked endpoints and deployed on IBM Cloud without any problems, they work perfectly. One example of a working endpoint is the following:

app.router.post("/getStats") { (request, response, next) in
    let mocks = Mocks()
    response.send(json: mocks.dashboard)
    next()
}

The Mocks() its a class that holds the dashboard struct with all fields predefined. Those fields are plain types (strings and numbers)

When I started to develop a more advanced endpoint, the workflow is the same, I register the route with the following code

app.router.post("/generateTest") { (request, response, next) in
    let mocks = Mocks()
    response.send(json: mocks.getTest())
    next()
}

Instead of a Codable struct with plain types, this mocked struct will have nested codable structs:

struct Test: Codable {
   var totalTime: Int
   var questions: [Question]
}

extension Test {
struct Question: Codable {
    let id: String?
    let pregunta: String
    var selectedAnswer: Answer? = nil
    let respuestas: [Answer]
    let idConcepto: Int
    let stats: Stats?
    let categorias: [String]
}

struct Answer: Codable {
    let respuesta: String
    let correcta: Bool
}

struct Stats: Codable {
    let globalStats: GlobalStats?
}

struct GlobalStats: Codable {
    let timesFailed, timesUnanswered, timesAnswered: Int?
}
}

Everything works fine when I build in localhost the project. When I navigate throught those endpoints in Postman, they return me the expected response.

The problem comes when I try to deploy this into IBM Cloud (also tried on Heroku with the same problem).

The problem it gives me its the following

/tmp/app/Sources/Application/Mocks/UserMock.swift:29:44: error: type of expression is ambiguous without more context
              let questionFirst: Test.Question = Test.Question(id: "1", pregunta: "UNO", respuestas: answers, idConcepto: 1, stats: nil, categorias: cats)
                                                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I dont know how to solve this, Xcode didnt raise any errors and compiled the project locally witouth any problem, I tried to cast types and all possible combinations to generate the struct, the error is always the same.

Has somebody faced the same problem? Is this a bug with Kitura or am I missing something? In the documentation they didn't say anything about nested Structs, the only thing I found relating to nested Struct, is that Kitura ORM doesnt support nested structs when working with ORM and SQL database, which is not my case.

I would be very happy if we can find a solution

Thanks for your help

andrewleung
  • 552
  • 6
  • 18
FlashHardcore
  • 11
  • 1
  • 2

1 Answers1

0

I managed to solve the problem. As i suspected, it was a dumb thing. In Test.Question struct, the field selectedAnswer was initialized with a default value equal to nil

When i created the struct i didnt wrote that field because it has already a default value, but it looks like Kitura or deploying Swift to a server didnt support this. I instantiate the structure with the default value right now and works without any problem.

I hope this can help anyone in a future with similar problem

FlashHardcore
  • 11
  • 1
  • 2