Coder Social home page Coder Social logo

Comments (11)

AlexNsbmr avatar AlexNsbmr commented on May 22, 2024 2

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.

igorkulman avatar igorkulman commented on May 22, 2024

Same problem trying to apply @LosslessValue to an optional property.

from bettercodable.

marksands avatar marksands commented on May 22, 2024

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.

AlexNsbmr avatar AlexNsbmr commented on May 22, 2024

@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.

marksands avatar marksands commented on May 22, 2024

@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.

AlexNsbmr avatar AlexNsbmr commented on May 22, 2024

@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.

marksands avatar marksands commented on May 22, 2024

@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.

AlexNsbmr avatar AlexNsbmr commented on May 22, 2024

@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.

guidedways avatar guidedways commented on May 22, 2024

@AlexNsbmr Can you share the full solution?

from bettercodable.

AlexNsbmr avatar AlexNsbmr commented on May 22, 2024

@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.

SajjadKharrazi avatar SajjadKharrazi commented on May 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.