我正在尝试从服务器端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的相关部分是:
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中,我有这样的功能:
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是这样的:
// 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...
}当tenureType和propertyType被定义为String?时,一切都可以正常工作,但我认为使用枚举来指定tenureType和propertyType的可能值是很有必要的。
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问题是,当我尝试为tenureType和propertyType使用枚举时,应用程序构建得很好,但当我运行vapor服务器时,我得到了这个错误。
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$当我将tenureType和propertyType指定为可选时(我认为它们应该是可选的),错误值是1-当它们不是可选的时候,错误值是0。
我应该补充说,这个错误只在我第一次运行服务器时发生,即:当它创建(空)数据库时。如果我第一次使用定义为String?的tenureType和propertyType运行它,它运行正常,然后我可以将它们更改为枚举,它继续正常工作。
发布于 2019-12-17 01:26:36
您需要使您的enum符合SQLiteEnumType和ReflectionDecodable。您还需要为每种情况提供字符串值。以您的TenureType为例,尝试:
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...
}https://stackoverflow.com/questions/59350921
复制相似问题