Giter Club home page Giter Club logo

Comments (5)

stephencelis avatar stephencelis commented on June 16, 2024

Hi @buscarini! Thanks for the report and sorry about that! Do you have some code that illustrates the issue?

from swift-tagged.

buscarini avatar buscarini commented on June 16, 2024

Yes, I was preparing a pull request, but here you go:

func testDecodableCustomDates() {
    let decoder = JSONDecoder()
    decoder.dateDecodingStrategy = .custom { decoder in
      let seconds = try decoder.singleValueContainer().decode(Int.self)
      return Date(timeIntervalSince1970: TimeInterval(seconds))
    }
    
    XCTAssertEqual(
      [Date(timeIntervalSince1970: 1)],
      try decoder.decode([Date].self, from: Data("[1]".utf8))
    )
    
    XCTAssertEqual(
      [Tagged<Tag, Date>(rawValue: Date(timeIntervalSince1970: 1))],
      try decoder.decode([Tagged<Tag, Date>].self, from: Data("[1]".utf8))
    )
  }

Unfortunately, I've found out that changing this back makes another test fail. In that case wrapped optionals fail to decode if the json value is null. I haven't been able to find a solution to both issues for now.

from swift-tagged.

stephencelis avatar stephencelis commented on June 16, 2024

@buscarini If I define decode as the following, both appear to work:

extension Tagged: Decodable where RawValue: Decodable {
  public init(from decoder: Decoder) throws {
    do {
      self.init(rawValue: try decoder.singleValueContainer().decode(RawValue.self))
    } catch {
      self.init(rawValue: try .init(from: decoder))
    }
  }
}

I'm not sure what the repercussions are, and it does indeed feel like a shortcoming or bug of Decodable. Any thoughts? Would we need to handle Encodable in a similar way?

from swift-tagged.

buscarini avatar buscarini commented on June 16, 2024

:D Funny, I just made a pull request with the same solution. I think this should work fine, and I think it's the only reasonable solution for now. I even checked swift source code, and it seems initializing the date with the decoder directly doesn't hit the date decoding strategy, as this is done by the decoder before getting to the actual type. And solving the optional issue with the singleValueContainer would require to have different code depending on the type, which shouldn't even be possible in a generic function.

As for the encodable protocol, I think it's going to have the same issue. I'll try to get some free time to put that in the pull request too.

from swift-tagged.

buscarini avatar buscarini commented on June 16, 2024

It seems that the encoding strategy doesn't require this, and just reverting the commit is enough to get it working.

from swift-tagged.

Related Issues (15)

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.