首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ubuntu上的Serverside Swift和Vapor

Ubuntu上的Serverside Swift和Vapor
EN

Stack Overflow用户
提问于 2019-12-16 13:22:01
回答 1查看 216关注 0票数 2

我正在尝试从服务器端Swift编写一个基于Project2的应用程序接口,作者是Paul Hudson https://www.hackingwithswift.com/store/server-side-swift

我正在运行:

Swift 5.0版(swift-5.0-RELEASE)目标:x86_64-未知-linux-gnu

蒸气工具箱: 3.1.10

在Ubuntu18.04.3LTS上(GNU/Linux4.15.0-66-generic x86_64)

这个想法是使用Fluent和SQLite来维护(CRUD)资产记录。

configure.swift的相关部分是:

代码语言:javascript
复制
let directoryConfig = DirectoryConfig.detect()
    services.register(directoryConfig)

    try services.register(FluentSQLiteProvider())

    var databaseConfig = DatabasesConfig()
    let db = try SQLiteDatabase(storage: .file(path: "\(directoryConfig.workDir)properties.db"))
    databaseConfig.add(database: db, as: .sqlite)
    services.register(databaseConfig)

    var migrationConfig = MigrationConfig()
    migrationConfig.add(model: Property.self, database: .sqlite)
    services.register(migrationConfig)

在Routes.swift中,我有这样的功能:

代码语言:javascript
复制
    router.post(Property.self, at: "properties", "create") { req, property -> Future<Property> in
        return property.save(on: req)
    }

    router.get("properties", "list") { req -> Future<[Property]> in
        return Property.query(on: req).all()
    }

Property.swift是这样的:

代码语言:javascript
复制
//  Property.swift
//
//  Created by Peter Matthews on 07/12/2019.
//

import Fluent
import FluentSQLite
import Foundation
import Vapor

enum TenureType: String, Codable {
    case freehold
    case cross_lease
    case unit_title
    //    etc...
}

enum PropertyType: String, Codable {
    case commercial
    case office
    case industrial
    case retail
    //    etc...
}

struct Property: Content, SQLiteUUIDModel, Migration {

    var id: UUID?
    var tenureType: TenureType?     //  var tenureType: String?
    var propertyType: PropertyType? //  var propertyType: String?
    //    etc...
}

tenureTypepropertyType被定义为String?时,一切都可以正常工作,但我认为使用枚举来指定tenureTypepropertyType的可能值是很有必要的。

代码语言:javascript
复制
Terminal output:

pm@PHQ:~/PropertyHQ$ vapor run
Running PropertyHQ ...
[ INFO ] Migrating 'sqlite' database (/home/pm/PropertyHQ/.build/checkouts/fluent/Sources/Fluent/Migration/MigrationConfig.swift:69)
[ INFO ] Migrations complete (/home/pm/PropertyHQ/.build/checkouts/fluent/Sources/Fluent/Migration/MigrationConfig.swift:73)
Running default command: .build/debug/Run serve

问题是,当我尝试为tenureTypepropertyType使用枚举时,应用程序构建得很好,但当我运行vapor服务器时,我得到了这个错误。

代码语言:javascript
复制
Terminal output:

pm@PHQ:~/PropertyHQ$ vapor run
Running PropertyHQ ...
[ INFO ] Migrating 'sqlite' database (/home/pm/PropertyHQ/.build/checkouts/fluent/Sources/Fluent/Migration/MigrationConfig.swift:69)
[ INFO ] Preparing migration 'Property' (/home/pm/PropertyHQ/.build/checkouts/fluent/Sources/Fluent/Migration/Migrations.swift:111)
⚠️ [DecodingError.dataCorrupted: Cannot initialize TenureType from invalid String value 1]
Error: Run failed.
pm@PHQ:~/PropertyHQ$

当我将tenureTypepropertyType指定为可选时(我认为它们应该是可选的),错误值是1-当它们不是可选的时候,错误值是0。

我应该补充说,这个错误只在我第一次运行服务器时发生,即:当它创建(空)数据库时。如果我第一次使用定义为String?tenureTypepropertyType运行它,它运行正常,然后我可以将它们更改为枚举,它继续正常工作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-17 01:26:36

您需要使您的enum符合SQLiteEnumTypeReflectionDecodable。您还需要为每种情况提供字符串值。以您的TenureType为例,尝试:

代码语言:javascript
复制
enum TenureType: String, Codable, SQLiteEnumType, ReflectionDecodable {
    static func reflectDecoded() throws -> (TenureType, TenureType) {
        return ( .freehold, .cross_lease )
    }
    case freehold = "freehold"
    case cross_lease = "cross_lease"
    case unit_title = "unit_title"
    //    etc...
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59350921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档