Skip to content

Instantly share code, notes, and snippets.

View mackoj's full-sized avatar
🤓
Neerding & Parenting

Jeffrey Macko mackoj

🤓
Neerding & Parenting
View GitHub Profile
@mackoj
mackoj / transmute.swift
Created April 10, 2024 08:30
Force AnyCodable to change a type of an object
import Foundation
import AnyCodable
// This is Fugly but it get the job done.
extension AnyCodable {
func transmuteThrow<Output: Codable>(_ encoder: JSONEncoder = JSONEncoder(), _ decoder: JSONDecoder = JSONDecoder()) throws -> Output {
let data = try encoder.encode(self)
return try decoder.decode(Output.self, from: data)
}

Playground

https://www.swift-linux.com/sextant/

Syntax

Depending on the client used JSONPath expressions do start with $. indicating the root element. Some clients omit the leading $..

$.store.book[0].title

@mackoj
mackoj / sdkExterneEtApiTierces.md
Created February 20, 2023 23:54 — forked from ThibaultFighieraPJ/sdkExterneEtApiTierces.md
Intégration de SDKs Externes et Api Tierce Android

SDK Externe et API Tierces

Objectif : Lister ce à quoi les SDK externes et API tierces doivent se conformer avant d'être intégrés dans l'application Android PagesJaunes.

Veuillez forker ce gist et mettre un ✅ devant les cases vous concernant. Renvoyez-nous le lien de votre document par email avec l'explication de ce que vous ne faites pas et pourquoi ? 🙂

Documentation

  • En ligne
    • Publique
  • Accès restreint
@mackoj
mackoj / PublisherToAsync.swift
Created October 4, 2022 09:09
This convert your Combine base code to Async/Await
import Fopundation
import Combine
extension AnyPublisher {
func async<NewOutput>(_ transform: @escaping (Output) throws -> NewOutput) async throws -> NewOutput {
try await withCheckedThrowingContinuation { continuation in
var cancellable: AnyCancellable?
cancellable = first()
.sink { result in
switch result {
@mackoj
mackoj / Codable+URL.swift
Last active October 19, 2022 21:15
This add the ability to use URL when decoding JSON directly in the model
import Foundation
// public struct Model: Codable {
// public let test: URL?
// public let test2: URL
// ...
// }
// ⚠️ This is not a universal solution tho in orfer to handle properlu fileURL URL should be init with `URL(fileURLWithPath:)` instead of URL(string:)
@mackoj
mackoj / PrefixUntil.swift
Last active June 29, 2022 09:17
This an operator for the swift-parsing library that add a parser that consumes a subsequence from the beginning of its input up to a given parser succeed.
import Foundation
import Parsing
/// A parser that consumes a subsequence from the beginning of its input up to a given parser succeed.
///
/// This parser behave a lot like `PrefixUpTo` but it can be slower because it can use a parser instead of
/// a static string and consume and return input up to a particular parser succeed.
///
/// If provided with a string that correspond to the begining of the parser it will be almost as fast as `PrefixUpTo` in most cases. The only case where it will be slow when providing a string is when there is a lot of false positive aka pattern that look like what the parser should valid but are not valid.
///
@mackoj
mackoj / SerializableCGColor.swift
Created June 8, 2022 17:58
Codable implémentation for Color, CGColor that respect ColorSpace
import CoreFoundation
import CoreGraphics
import Foundation
import SwiftUI
public let defaultBadColor = Color.green
public let defaultGoodColor = Color.red
public struct SerializableCGColorError: Error, LocalizedError {
let error: String
@mackoj
mackoj / NotCodable.swift
Created June 3, 2022 21:03
This PropertyWrapper allow a property to not be encoded with Codable without needed to create a custom CodingKeys.
import Foundation
/// This allow a property to no be encoded
@propertyWrapper
public struct NotCodable<Boxed> {
private var value: Boxed?
public init(wrappedValue: Boxed?) {
self.value = wrappedValue
}
@mackoj
mackoj / MockCodable.swift
Created June 3, 2022 20:59
This PropertyWrapper allow you to force a value when decoding a property
import Foundation
/// This allow to force a default value when using codable
/// This will force `isEditMode` to always have `false` when decoding it
/// @MockCodable(defaultValue: false) public var isEditMode: Bool = true
@propertyWrapper
public struct MockCodable<Value> {
private var value: Value
private var defaultValue: Value
@mackoj
mackoj / Bundle+decode.swift
Created May 19, 2022 14:08
Decode file from bundle
import Foundation
extension Bundle {
func decode<Output: Decodable>(
_ type: Output.Type,
filename: String,
withExtension ext: String,
decoder: any TopLevelDecoder = JSONDecoder()
) throw -> Output {
guard let fileURL = url(forResource: filename, withExtension: ext) else {