Reputation: 23
I am trying to write an API based on Project2 from Server Side Swift by Paul Hudson https://www.hackingwithswift.com/store/server-side-swift
I am running:
Swift version 5.0 (swift-5.0-RELEASE) Target: x86_64-unknown-linux-gnu
and
Vapor Toolbox: 3.1.10
on Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-66-generic x86_64)
The idea is to use Fluent and SQLite to maintain (CRUD) property records.
The relevant part of configure.swift is:
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)
In Routes.swift I have this:
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()
}
And Property.swift is this:
// 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...
}
When tenureType
and propertyType
are defined as String?
everything works fine, but I think there is a strong case for using enums to specify the possible values for tenureType
and 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
The problem is that when I try to use enums for tenureType
and propertyType
, the app builds just fine but when I run the vapor server I get this error.
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$
When I specify tenureType
and propertyType
as optional (which I think they should be) the error value is 1 - when they are not optional the error value is 0.
I should add that this error only occurs when I run the server for the first time ie: when it creates the (empty) database. If I run it for the first time with tenureType
and propertyType
defined as String?
it runs OK and then I can change them to enums and it continues to work OK.
Upvotes: 2
Views: 294
Reputation: 5180
You need to conform your enum
to SQLiteEnumType
and ReflectionDecodable
. You also need to supply string values for each case. Using your TenureType
as an example, try:
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...
}
Upvotes: 4