Skip to content

Instantly share code, notes, and snippets.

@JohnCoates
JohnCoates / Future+NilMap.swift
Created March 10, 2019 23:54
Mapping on nil for Vapor 3
//
// Future+NilMap.swift
// Created on 3/10/19
//
import Async
public protocol OptionalConstrainable {
associatedtype Element
var asOptional: Optional<Element> { get }
@vzsg
vzsg / 1_Vapor+Proto.swift
Created March 3, 2019 11:19
Using SwiftProtobuf with Vapor 3
import Vapor
import SwiftProtobuf
import Foundation
extension Request {
public func decodeMessage<M: SwiftProtobuf.Message>(_ type: M.Type = M.self) throws -> M {
let data = http.body.data ?? Data()
if http.contentType == MediaType.json {
return try M(jsonUTF8Data: data)
@vzsg
vzsg / 1_Fetch.swift
Last active June 25, 2021 10:37
A solution for the N+1 problem when fetching children for parents (Fluent 3)
import Fluent
func fetchChildren<Parent, ParentID, Child: Model, Result>(
of parents: [Parent],
idKey: KeyPath<Parent, ParentID?>,
via reference: KeyPath<Child, ParentID>,
on conn: DatabaseConnectable,
combining: @escaping (Parent, [Child]) -> Result) -> Future<[Result]> where ParentID: Hashable & Encodable {
let parentIDs = parents.compactMap { $0[keyPath: idKey] }
let children = Child.query(on: conn)
@MihaelIsaev
MihaelIsaev / PostgreSQLQueryCodableExample.swift
Created May 29, 2018 01:13
Vapor3 PostgreSQL extension to decode [[PostgreSQLColumn: PostgreSQLData]] with Codable struct
import PostgreSQL
typealias PostgreSQLQueryRow = [PostgreSQLColumn: PostgreSQLData]
extension Dictionary where Key == PostgreSQLColumn, Value == PostgreSQLData {
func decode<T>(_ key: String) throws -> T where T: PostgreSQLDataConvertible {
guard let v = try firstValue(forColumn: key)?.decode(T.self) else {
throw PostgreSQLError(identifier: "decodingError", reason: "Unable to decode \"\(key)\" column ", source: .capture())
}
return v
@zthxxx
zthxxx / Activate Office 2019 for macOS VoL.md
Last active April 20, 2024 09:46
crack activate Office on mac with license file
@nickcheng
nickcheng / appdocdir.zsh
Last active May 15, 2023 15:28
Open your app's Documents folder on iOS simulator.
function myappdocdir() {
devid=$(xcrun simctl list devices | grep Booted | sed -n 's/^.*\([A-F0-9]\{8\}-\([A-F0-9]\{4\}-\)\{3\}[A-F0-9]\{12\}\).*$/\1/p')
for folder in ~/Library/Developer/CoreSimulator/Devices/$devid/data/Containers/Data/Application/*; do
if [[ -a $folder/.com.apple.mobile_container_manager.metadata.plist ]]; then
if [[ 'com.apple.phone' = $(/usr/libexec/PlistBuddy -c 'Print :MCMMetadataIdentifier' $folder/.com.apple.mobile_container_manager.metadata.plist) ]]; then
echo $folder
break
fi
fi
done
@jampajeen
jampajeen / LC_CTYPE.txt
Created November 21, 2015 13:02
Centos warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
vi /etc/environment
add these lines...
LANG=en_US.utf-8
LC_ALL=en_US.utf-8
@patricklynch
patricklynch / gist:689525d466c4ada42b8e
Last active October 30, 2021 05:23
The 6 ways to unwrap optionals in Swift 2.0
// The 6 ways to uwnrap optionals in Swift 2.0
//
// Created by Patrick Lynch on 6/28/15.
// Copyright © 2015 Patrick Lynch. All rights reserved.
import Foundation
// 1. Force Unwrapping
// 2. Optional Binding
// 3. Optional Chaining
@joashp
joashp / PushNotifications.php
Last active July 28, 2023 12:33
Simple PHP script to send Android Push Notification, iOS Push Notification and Windows Phone 8 Push Notification
<?php
// Server file
class PushNotifications {
// (Android)API access key from Google API's Console.
private static $API_ACCESS_KEY = 'AIzaSyDG3fYAj1uW7VB-wejaMJyJXiO5JagAsYI';
// (iOS) Private key's passphrase.
private static $passphrase = 'joashp';
// (Windows Phone 8) The name of our push channel.
private static $channelName = "joashp";