Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement support for Codable types #92

Merged
merged 12 commits into from
Jan 11, 2024
Merged

Implement support for Codable types #92

merged 12 commits into from
Jan 11, 2024

Conversation

jessesquires
Copy link
Owner

@jessesquires jessesquires commented Jan 10, 2024

Closes #72

@jessesquires jessesquires changed the title Codable support Implement support for Codable types. Close #72 Jan 10, 2024
@jessesquires jessesquires changed the title Implement support for Codable types. Close #72 Implement support for Codable types Jan 10, 2024
}
}

/// :nodoc:
extension UserDefaultsSerializable where Self: RawRepresentable, Self.RawValue: UserDefaultsSerializable {
public var storedValue: RawValue.StoredValue { self.rawValue.storedValue }

public init(storedValue: RawValue.StoredValue) {
self = Self(rawValue: Self.RawValue(storedValue: storedValue))!
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An added benefit of this change is that the RawRepresentable implementation no longer needs this force-unwrap

Copy link
Collaborator

@nolanw nolanw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks legit!

I guess a saving grace is one still has to mark the Codable type with UserDefaultsSerializable before Foil will accept it, so that's an opportunity to wonder if it's a good idea.

I see that SwiftUI.AppStorage doesn't afford any conveniences for Codable types. No idea why, maybe it's dissuasion or maybe e.g. it didn't fit as nicely with that interface.

@jessesquires
Copy link
Owner Author

Thanks for the quick review @nolanw! 🙌🏼

I added a few more commits if you want to take another quick look.

I see that SwiftUI.AppStorage doesn't afford any conveniences for Codable types. No idea why, maybe it's dissuasion or maybe e.g. it didn't fit as nicely with that interface.

I think probably dissuasion. But curiously, it does not support raw value enums, which is frustrating.

Copy link
Collaborator

@nolanw nolanw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate the note about compactMap use :)

@jessesquires jessesquires merged commit 58a6979 into main Jan 11, 2024
8 of 9 checks passed
@jessesquires jessesquires deleted the jsq/codable-support branch January 11, 2024 19:23
@jessesquires
Copy link
Owner Author

thanks @nolanw ! 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature]: Be able to save Codable structs (or a workaround?)
2 participants