Skip to content

Instantly share code, notes, and snippets.

Avatar

Siemen Sikkema siemensikkema

View GitHub Profile
View V4Validations.swift
func createUser(request: Request) -> EventLoopFuture<HTTPResponseStatus> {
let password: String? = request.content["password"]
let email: String? = request.content["email"]
User.query(on: request.db).filter(\.email == email).count().map {
var validations = NewUser.validations()
validations.add("passwordAgain", as: String, is: .in(["password"])) // we need a .equals validator
if email != nil {
validations.add("email", result: UniqueEmailValidatorResult())
@siemensikkema
siemensikkema / Repositories.swift
Created Oct 8, 2019
Example of how we can use repositories in Vapor
View Repositories.swift
import FluentMySQL
import Vapor
// MARK: Models
final class User: MySQLModel {
var id: Int?
var active: Bool
@siemensikkema
siemensikkema / Dockerfile
Last active May 4, 2019
Run Vapor in Docker with Redis and MySQL
View Dockerfile
FROM swift:4.2
ADD ./ /app
WORKDIR /app
RUN swift build -c release
CMD ["./wait-for-it.sh", "-t", "30", "database:3306", "--", "./.build/release/Run", "serve", "--hostname", "0.0.0.0"]
@siemensikkema
siemensikkema / fixcodeproj.rb
Last active Nov 3, 2018
Post processing script for generated vapor Xcode project
View fixcodeproj.rb
#!/usr/bin/env ruby
require 'xcodeproj'
project_path = ARGV[0]
project = Xcodeproj::Project.open(project_path)
project.targets.each do |target|
# suppress warnings
if [
"Configs",
@siemensikkema
siemensikkema / objcio S01E06 Multiple Cell Types.swift
Last active Jun 16, 2019
Multiple Cell Types example based on episode 6 of talk.objc.io in response to question in episode 9
View objcio S01E06 Multiple Cell Types.swift
import UIKit
import XCPlayground
struct Episode {
var title: String
}
struct Season {
var number: Int
View Warnings.xcconfig
// XCConfig file to enable all Swift compiler warnings
// Original by Jon Reid (see below).
// Modified by Siemen Sikkema: removed non-swift warnings
// XcodeWarnings by Jon Reid, http://qualitycoding.org/about/
// Source: https://github.com/jonreid/XcodeWarnings
// Apple LLVM 7.0 - Warning Policies
//GCC_TREAT_WARNINGS_AS_ERRORS = YES
@siemensikkema
siemensikkema / Themeable.swift
Created Feb 12, 2016
Themeable ViewController demonstration
View Themeable.swift
import UIKit
protocol Themeable {
func applyTheme()
}
extension Themeable where Self: UIViewController {
func applyTheme() {
// beautiful!
navigationItem.leftBarButtonItem?.tintColor = .purpleColor()
View MIDIClientPOC.swift
import XCPlayground
import CoreMIDI
XCPSetExecutionShouldContinueIndefinitely(true)
func notifyProc(a: UnsafePointer<MIDINotification>, b: UnsafeMutablePointer<Void>) -> Void {
let notification = a[0]
notification.messageID
notification.messageSize
}
View TimeStringToDate.swift
import Foundation
/// converts a string representing an unlocalized date with fixed-format to NSDate using Unix methods (suggested in Apple's Date Formatting guide: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html)
public func timeStringToDate(timeString: NSString, formatString: NSString) -> NSDate {
var t = tm()
return withUnsafeMutablePointer(&t) { t -> NSDate in
strptime_l(timeString.UTF8String, formatString.UTF8String, t, nil)
return NSDate(timeIntervalSince1970: NSTimeInterval(mktime(t)))
}
}
View XCTAssertEqualOptional.swift
/// like XCTAssertEqual, but handles optional unwrapping
public func XCTAssertEqualOptional<T: Any where T: Equatable>(@autoclosure expression1: () -> T?, @autoclosure expression2: () -> T?, _ message: String? = nil, file: String = __FILE__, line: UInt = __LINE__) {
if let exp1 = expression1() {
if let exp2 = expression2() {
XCTAssertEqual(exp1, exp2, message ?? "", file: file, line: line)
} else {
XCTFail(message ?? "exp1 != nil, exp2 == nil", file: file, line: line)
}
} else if let exp2 = expression2() {
XCTFail(message ?? "exp1 == nil, exp2 != nil", file: file, line: line)
You can’t perform that action at this time.