Giter Club home page Giter Club logo

Comments (14)

soumyamahunt avatar soumyamahunt commented on August 15, 2024 5

@ashleydw @Willian-Zhang new macro Inherits has been added (only in main branch, not released yet) to workaround the issue until this is fixed from compiler.

from metacodable.

ashleydw avatar ashleydw commented on August 15, 2024 1

Here's a repo: https://github.com/ashleydw/metacodable-min-ex/blob/main/test/Item.swift

If you build you should get the same error on Item.swift. Commenting out @Codable and CodedAt should build successfully and run.

from metacodable.

soumyamahunt avatar soumyamahunt commented on August 15, 2024 1

Thanks @ashleydw for providing sample, I can reproduce your issue. The issue doesn't seem to be combining @Codable with SwiftData class, in fact the project builds successfully if you comment out following lines:

https://github.com/ashleydw/metacodable-min-ex/blob/c0bd383995ca3c12938f704b79429e2c9c7b0c9b/test/ContentView.swift#L6
https://github.com/ashleydw/metacodable-min-ex/blob/c0bd383995ca3c12938f704b79429e2c9c7b0c9b/test/ContentView.swift#L11-L18
https://github.com/ashleydw/metacodable-min-ex/blob/c0bd383995ca3c12938f704b79429e2c9c7b0c9b/test/ContentView.swift#L50
https://github.com/ashleydw/metacodable-min-ex/blob/c0bd383995ca3c12938f704b79429e2c9c7b0c9b/test/ContentView.swift#L58

Seems like with these line present Swift is not providing protocol conformance data to @Codable. It would be better if you create a discussion on Swift forum or Apple developer forum as the issue doesn't seem to be MetaCodable related.

from metacodable.

Willian-Zhang avatar Willian-Zhang commented on August 15, 2024 1

@soumyamahunt great insight!
@soumyamahunt @ashleydw Are you available to fire a bug report or discussion as you said to apple by any chance?
I don't think I'm qualified enough for this as I'm new to SwiftLang let alone any insight on how swift macro works.

If not, do you think there is way to go around with this problem? I am very interested in this cross usage on SwiftData+MetaCodable, as it would vastly simplify my code in a project that I started recently. (Actually I'm current on a track giving up on #Preview capability just to have this working)

from metacodable.

Willian-Zhang avatar Willian-Zhang commented on August 15, 2024 1

having Inherits tested, I can confirm this solved my problem perfectly.

from metacodable.

soumyamahunt avatar soumyamahunt commented on August 15, 2024 1

@rursache you have to remove the manual Codable conformance you are adding, due to this addition compiler is also generating Codable implementation which is conflicting with this library's generated implementation.

from metacodable.

soumyamahunt avatar soumyamahunt commented on August 15, 2024

If a SwiftData model annotation is added to a class, the build will fail with "Method does not override any method from its superclass".

It can be reproduced using a clean SwiftData project, adding the SPM package from 1.2.1, and the following class:

import Foundation
import SwiftData
import MetaCodable
import HelperCoders

protocol MyProtocol {
    
}

@Codable
@Model
final class Item : MyProtocol {
    @CodedAt("timestamp")
    var timestamp: Date? = nil
    
    init() { }
}

Are you sure you are able to reproduce this with this code snippet, for me this code snippet doesn't produce error. Can you provide a sample project for this?

I have however managed to produce one expansion with the error:

image

Can you provide sample project for this, I can think of one scenario where this error would come:

Note

If you are manually conforming to Codable protocol in the class, please remove the conformance as @Codable macro itself generates the conformance.

Anyway I can't do much here with the screenshots, please provide a minimal reproducible sample.

from metacodable.

Willian-Zhang avatar Willian-Zhang commented on August 15, 2024

Same issue here,

problem magically disappear when #Preview section commented out of some random View (no @ State, no @Query no anything), other #Preview that contains a @Query not affected by this

from metacodable.

Willian-Zhang avatar Willian-Zhang commented on August 15, 2024

And it happens to me when I move @Bindable var someEnvObject = someEnvObject in scope of a body of a View that takes and EnvroementObject (check below for doc), to 1 ZStack level inside...

// @Bindable var someEnvObject = someEnvObject <- from here
ZStack {
  @Bindable var someEnvObject = someEnvObject <- to here
  // some other code using `someEnvObject`
}
image

(Apple doc)[https://developer.apple.com/documentation/swiftui/bindable]

from metacodable.

soumyamahunt avatar soumyamahunt commented on August 15, 2024

@ashleydw @Willian-Zhang has any of you created discussion on Swift forum or Apple developer forum?

I am afraid this issue is not related to MetaCodanle, taking the discussion to Swift team/Apple is the only solution to this.

from metacodable.

Willian-Zhang avatar Willian-Zhang commented on August 15, 2024

@ashleydw @Willian-Zhang has any of you created discussion on Swift forum or Apple developer forum?

I am afraid this issue is not related to MetaCodanle, taking the discussion to Swift team/Apple is the only solution to this.

I haven't, I wasn't able to identify where the problem lies to.
The only thing I can be sure about is that it happens with this use case and reporting this exact error from @Codable generated code.

from metacodable.

soumyamahunt avatar soumyamahunt commented on August 15, 2024

@ashleydw @Willian-Zhang has any of you created discussion on Swift forum or Apple developer forum?
I am afraid this issue is not related to MetaCodanle, taking the discussion to Swift team/Apple is the only solution to this.

I haven't, I wasn't able to identify where the problem lies to. The only thing I can be sure about is that it happens with this use case and reporting this exact error from @Codable generated code.

Simple overview of the class implementation generation by @Codable:

-> Does Class conform to `Decodable`/`Encodable`? (passed from Swift compiler to `MetaCodable`)
    -> Yes
        -> Are `Decodable`/`Encodable` conformance implemented i.e. `init(from:)`/`encode(to:)` method implementation exist?
            -> Yes
                -> Skip generating implementation
            -> No
                 -> Assume that super class conforms to `Decodable`/`Encodable` and provide generated implementation.
    -> No
        -> No super class conforms to `Decodable`/`Encodable` and provide generated implementation.

The strange thing here is without those lines the Swift compiler invokes the following flow:

-> Does Class conform to `Decodable`/`Encodable`? (passed from Swift compiler to `MetaCodable`)
    -> No
        -> No super class conforms to `Decodable`/`Encodable` and provide generated implementation.

and with those lines present the Swift compiler invokes the following flow:

-> Does Class conform to `Decodable`/`Encodable`? (passed from Swift compiler to `MetaCodable`)
    -> Yes
        -> Are `Decodable`/`Encodable` conformance implemented i.e. `init(from:)`/`encode(to:)` method implementation exist?
            -> No
                 -> Assume that super class conforms to `Decodable`/`Encodable` and provide generated implementation.

which is a strange behaviour from compiler, as of why compiler provides wrong data in some cases, only Swift compiler team can answer.

from metacodable.

rursache avatar rursache commented on August 15, 2024

not sure what i'm doing wrong. using the main branch and this code:

@Codable
@Model
@Inherits(decodable: false, encodable: false)
final class TestModel: Codable, Identifiable {
  var id: Int = 0
  var url: String = ""
  var name: String = ""
  
  init(id: Int, url: String, name: String) {
    self.id = id
    self.url = url
    self.name = name
  }
}

Fails with
Redundant conformance of 'EpisodeModel' to protocol 'Decodable'
Redundant conformance of 'EpisodeModel' to protocol 'Encodable'

EDIT: solved by removing the Codable, Identifiable conformance, my bad!

from metacodable.

ashleydw avatar ashleydw commented on August 15, 2024

@soumyamahunt I can confirm it works in my project, thanks!

from metacodable.

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.