Skip to content

Instantly share code, notes, and snippets.

View jordanebelanger's full-sized avatar
🚊

Jordane Belanger jordanebelanger

🚊
  • Montreal, Quebec
View GitHub Profile
@jordanebelanger
jordanebelanger / bitbucket-terra-fail
Created November 10, 2022 16:07
GCP Cloud build trigger fail with Bitbucket source type
│ Error: Error creating Trigger: googleapi: Error 400: Invalid value at 'trigger.git_file_source.repo_type' (type.googleapis.com/google.devtools.cloudbuild.v1.GitFileSource.RepoType), "BITBUCKET"
│ Details:
│ [
│ {
│ "@type": "type.googleapis.com/google.rpc.BadRequest",
│ "fieldViolations": [
│ {
│ "description": "Invalid value at 'trigger.git_file_source.repo_type' (type.googleapis.com/google.devtools.cloudbuild.v1.GitFileSource.RepoType), \"BITBUCKET\"",
│ "field": "trigger.git_file_source.repo_type"
│ }
@jordanebelanger
jordanebelanger / stackdriverpino.ts
Last active June 2, 2023 21:09
stackdriverlogger.ts
import * as P from 'pino'
import {FastifyReply, FastifyRequest} from 'fastify'
const enum PINO_LEVELS {
trace = 10,
debug = 20,
info = 30,
warn = 40,
error = 50,
fatal = 60,
@jordanebelanger
jordanebelanger / logger.ts
Last active June 2, 2023 21:09
Fastify GCP Stackdriver logging agent compatible pino logger
import * as P from 'pino'
import {FastifyReply, FastifyRequest} from 'fastify'
const enum PINO_LEVELS {
trace = 10,
debug = 20,
info = 30,
warn = 40,
error = 50,
fatal = 60,
public struct ResultCodable<CodableType>: Codable where CodableType: Codable {
private enum EncodingError: Swift.Error, CustomStringConvertible {
case encodedErrorResult(encodingError: Swift.Error)
var description: String {
switch self {
case .encodedErrorResult(let encodingError):
return "Cannot encode `Result.failure` which resulted from decoding error: '\(encodingError)'"
}
@jordanebelanger
jordanebelanger / bytebuffer-serialization.swift
Last active January 21, 2021 15:44
Manually encode and decode a codable type to and from a bytebuffer for redis usage
import Vapor
import RedisKit
struct MySession {
let token: RedisKey
let email: String
let userId: Int64
let isAdmin: Bool
let ipAddress: String?
@jordanebelanger
jordanebelanger / Nullable.swift
Last active June 18, 2024 10:33
Codable enum that will differentiate between an explicitly null value vs the value being missing, useful for PATCH request etc
enum Nullable<T>: Codable where T: Codable {
case some(_ value: T)
case null
case undefined
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if container.decodeNil() {
self = .null
} else {
import class Dispatch.DispatchQueue
import NIO
public extension DispatchQueue {
/// `DispatchQueue.async` but returning a NIO future wrapping the closure return.
func async<Value>(on eventLoop: EventLoop, use closure: @escaping () throws -> Value) -> EventLoopFuture<Value> {
let promise = eventLoop.makePromise(of: Value.self)
async {
do {
promise.succeed(try closure())
/// Raw representable `Codable` property wrapper used to instantiate to a `RawRepresentable` type if the `rawValue`decoded is supported
/// by the generic `RawRepresentableValue` type. This can be useful when you want to decode a set of constant values
/// specified inside `RawRepresentable` enum yet default to the `rawValue` if the decoded `rawValue` is unsupported.
///
/// For example, using this property wrapper you could decode an enum specifying a list of error code such as:
///
/// enum ErrorCode: String, Codable {
/// case notFound, unauthorized, badRequest
/// }
///
import NIO
import Logging
extension EventLoopFuture {
func whenFailureLog(
level: Logger.Level = .error,
_ message: @escaping @autoclosure () -> Logger.Message,
using logger: Logger,
errorDescriptionMetadataKey: String = "reason",
metadata: @escaping @autoclosure () -> Logger.Metadata? = nil,
@jordanebelanger
jordanebelanger / LoggingSystem+StackdriverLogging.swift
Last active June 15, 2020 14:56
Bootstrapping StackdriverLogging in a Vapor 4 application, the `LoggingSystem+StackdriverLogging.swift` file should go in your App target
import Vapor
import StackdriverLogging
extension LoggingSystem {
/// Prepare the `StackdriverLogHandlerFactory` and bootstrap the `LoggingSystem` to use `StackdriverLogHandler`s.
///
/// - Returns: A `StackdriverLoggerLifecycleHandler` that must be passed to the `Vapor.Application` `lifecyle` service
/// for proper shutdown of the NIO dependencies used by the logger when the `Application` shuts down.
public static func bootstrapStackdriverLogging(for environment: inout Environment,
with configuration: StackdriverLoggingConfiguration) throws -> StackdriverLoggerLifecycleHandler {