Comments (11)
Actually I fixed it with:
func decode<T>(_ type: OptionalDateValue<T>.Type, forKey key: Self.Key) throws -> OptionalDateValue<T> {
return try decodeIfPresent(type, forKey: key) ?? OptionalDateValue<T>(wrappedValue: nil)
}
from bettercodable.
Same problem trying to apply @LosslessValue
to an optional property.
from bettercodable.
As a workaround for now you can try:
struct S {
var timestamp: DateValue<TimestampStrategy>?
}
You lose the property wrapper, but you can still get get the Date?
from calling timestamp.wrappedValue
. I don't think there's a good workaround without introducing a new PW type like @OptionalDateValue
, unless anyone has suggestions. I'll think about it some.
from bettercodable.
@marksands do you think it's possible to implement your solution: @OptionalDateValue<TimestampStrategy> var timestamp: Date?
Tried to work on it but dealing with empty data failure triggered by the public init(from decoder: Decoder) throws
instantiation.
from bettercodable.
@AlexNsbmr You have to duplicate the strategy as well, which is why I don't want to pursue that implementation for this library. Here's a working example, you're welcome to use it https://gist.github.com/marksands/243cc6963aa4cf23a4d12ecc2b51f44a
from bettercodable.
@marksands thanks you for your answer and your exemple.
Sorry, I wasn't very precise about what was not working. Actually it's working with:
let jsonData = #"{"timestamp": null}"#.data(using: .utf8)!
But not when the key is missing with an empty body for exemple:
let jsonData = #"{}"#.data(using: .utf8)!
giving the following error:
Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "timestamp", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"timestamp\", intValue: nil) (\"timestamp\").", underlyingError: nil))
Maybe you have an idea how to achieve it but I don't think there is actually any solution
from bettercodable.
@AlexNsbmr in that case you can use the generic type by itself and fetch the internal wrappedValue
for the optional date. Something like this:
struct Fixture: Codable {
var timestamp: OptionalDateValue<OptionalTimestampStrategy>?
}
extension Fixture {
var timestampDate: Date? {
return timestamp?.wrappedValue
}
}
from bettercodable.
@marksands Yes it could work like that but we need to declare a second variable and the variable declaration is not homogeneous because we don't use the same notation.
Anyway, thank you for the suggestion and keep up the great work ! :)
Hope there will be some changes in Codable
or in WrapperProperty
in the future to allow such a notation.
from bettercodable.
@AlexNsbmr Can you share the full solution?
from bettercodable.
@guidedways yes sure, actually it's the same solution that @marksands suggested:
https://gist.github.com/marksands/243cc6963aa4cf23a4d12ecc2b51f44a
You just need to add #26 (comment) in addition to that
from bettercodable.
in my json sometimes exists timestamp
and sometimes does not exist.
i want to have timestamp
like this with Date()
as default value if does not exist or is null
:
struct Fixture: Codable {
@OptionalDateValue<OptionalTimestampStrategy> var timestamp: Date
}
how can i do this?
from bettercodable.
Related Issues (20)
- Rename RawValue to DefaultValue in DefaultCodingStrategy
- Composing `LossyArray` with `DefaultEmptyArray` HOT 2
- Any ideas why I keep getting this error with cocoapods?
- podspec file should change version to 0.3.0 HOT 2
- Default Empty Lossy Array HOT 3
- LosslessValue not working as excepted from Int to String value HOT 6
- Add DefaultNil similar to DefaultTrue/False HOT 2
- could increase log output ,when decode or encode error happened HOT 2
- Uncodable property wrapper HOT 1
- @DefaultFalse cannot deal string number HOT 2
- JSONEncoder's `dateEncodingStrategy` ignored for default `Date` HOT 1
- Xcode 13 linking against a dylib which is not safe for use in application extensions HOT 1
- Decoding Date? HOT 4
- Feature: Decode logical `Bool` into a real `Bool` HOT 1
- protocol CodableEnumeration for enum HOT 4
- Missing property in nested object fails the whole decoding of nested object
- How to deal with optional Date? HOT 2
- Are you prepared to support field mapping?
- Removed CocoaPod and add via SPM. Now getting "Framework not found BetterCodable" HOT 1
- Create a new release HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bettercodable.