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

Fixes #4370 - History metadata enums and struct access #4371

Merged
merged 9 commits into from Jul 30, 2021

Conversation

nbhasin2
Copy link
Collaborator

Enums and Structs inside a library don't have accessible initializer and require public inits

@codecov-commenter
Copy link

codecov-commenter commented Jul 29, 2021

Codecov Report

Merging #4371 (5df754d) into main (b5d0bc3) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #4371   +/-   ##
=======================================
  Coverage   75.94%   75.94%           
=======================================
  Files          46       46           
  Lines        4191     4191           
=======================================
  Hits         3183     3183           
  Misses       1008     1008           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b5d0bc3...5df754d. Read the comment docs.

Copy link
Contributor

@grigoryk grigoryk left a comment

Choose a reason for hiding this comment

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

This is generally fine, but I'd like to know why you need these things before landing :)

@@ -10,6 +10,15 @@ import Foundation
public enum DocumentType: Int32 {
case regular = 0
case media = 1
case unknown = -1

public init(rawValue: Int32) {
Copy link
Contributor

Choose a reason for hiding this comment

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

So what's your use case for this? Can't you just do DocumentType.media when creating instances? I suppose this may be helpful when deserializing, but I don't think you'd need to persist a DocumentType yourself manually.

Also, these numbers aren't entirely random - they need to align w/ what other platforms are using. And currently we don't have a -1 present elsewhere. So unless you absolutely need this change, I'd remove at least the unknown case.

@@ -28,6 +43,16 @@ public enum HistoryMetadataObservation {
case titleObservation(String)
case viewTimeObservation(Int32)
case documentTypeObservation(DocumentType)
case unknown
Copy link
Contributor

Choose a reason for hiding this comment

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

What is an unknown observation, and why do you need it? Does seem necessary to me.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I really don't need unknown but removing that causes swift errors for our initializers so I added additional case for default as unknonw. Happy to change / rename it as needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

What about function overloading? Separate init functions for each data type? Then you can remove unknown, i think.


public init(value: Any) {
switch value {
case let title as String: self = .titleObservation(title)
Copy link
Contributor

Choose a reason for hiding this comment

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

What if we have string observations that aren't about title, or Int32 that aren't time? Also, what's the use case here for the init, can't you just do HistoryMetadataObservation.titleObservation("hello")?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The major issue is that we need some sort of initializer for using it on Firefox iOS side otherwise structs and enums are inaccessible. This is just something how swift hasn't gotten to or a limitation I would say as all this is extra work 😓

Copy link
Contributor

@grigoryk grigoryk Jul 29, 2021

Choose a reason for hiding this comment

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

Oh wow, so that's actually a thing! https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html#//apple_ref/doc/uid/TP40014097-CH41-ID3 (memberwise init section). That's pretty annoying :)

@@ -910,6 +910,8 @@ public class PlacesWriteConnection: PlacesReadConnection {
msg.documentType = documentType.rawValue
case let .titleObservation(title):
msg.title = title
case .unknown:
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like a bug that may be easy to miss - e.g. somehow you end up init-ing HistoryMetadataObservation wrong, then pass it along here, and never notice that nothing actually happens.

Partially why I don't like being able to represent invalid states! E.g. if unknown isn't a thing, you can't pass invalid data into this method at all.

grigoryk
grigoryk previously approved these changes Jul 29, 2021
Copy link
Contributor

@grigoryk grigoryk left a comment

Choose a reason for hiding this comment

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

If you can remove illegal states (e.g. unknown case) by adding overloading your init methods, please do so :)

@mergify mergify bot dismissed grigoryk’s stale review July 29, 2021 20:52

The pull request has been modified, dismissing previous reviews.

grigoryk
grigoryk previously approved these changes Jul 29, 2021
@mergify mergify bot dismissed grigoryk’s stale review July 29, 2021 23:34

The pull request has been modified, dismissing previous reviews.

Copy link
Contributor

@rfk rfk left a comment

Choose a reason for hiding this comment

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

r+ based on conversation in slack

@rfk rfk merged commit 2d832d9 into mozilla:main Jul 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants