Skip to content

Instantly share code, notes, and snippets.

@lukluca
Created March 23, 2024 17:35
Show Gist options
  • Save lukluca/82484b40a8b48ee96e734529f3d0bdbb to your computer and use it in GitHub Desktop.
Save lukluca/82484b40a8b48ee96e734529f3d0bdbb to your computer and use it in GitHub Desktop.
Swift DecodableConfiguration
@propertyWrapper struct DecodableConfiguration<T, ConfigurationProvider> : Decodable where T : DecodableWithConfiguration, ConfigurationProvider : DecodingConfigurationProviding, T.DecodingConfiguration == ConfigurationProvider.DecodingConfiguration {
var wrappedValue: T
init(wrappedValue: T) {
self.wrappedValue = wrappedValue
}
init(wrappedValue: T, from configurationProvider: ConfigurationProvider.Type) {
self.wrappedValue = wrappedValue
}
/// Creates a new instance by decoding from the given decoder.
///
/// This initializer throws an error if reading from the decoder fails, or
/// if the data read is corrupted or otherwise invalid.
///
/// - Parameter decoder: The decoder to read data from.
init(from decoder: any Decoder) throws {
self.wrappedValue = try T(from: decoder, configuration: ConfigurationProvider.decodingConfiguration)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment