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

combine enum's the RawValue type define and it's protocol as the one #6

Open
ZhipingYang opened this issue Aug 1, 2019 · 1 comment
Assignees
Labels
Code Design better architecture to discuss

Comments

@ZhipingYang
Copy link
Owner

ZhipingYang commented Aug 1, 2019

public protocol PrettyRawRepresentable: RawRepresentable where RawValue == String {
    var prettyRawValue: RawValue { get }
}
public extension PrettyRawRepresentable {
    var prettyRawValue: String {
        let paths = String(reflecting: self).split(separator: ".").dropFirst()
        if String(paths.last ?? "") != rawValue {
            return rawValue
        }
        return paths.joined(separator: "_")
    }
}

As we already defined PrettyRawRepresentable's RawValue == String,
but enum cannot write as blow, why?

enum Home: PrettyRawRepresentable {    
    case setting
} 
let str = Home.seeting.prettyRawValue // Home_setting

current

enum Home: String, PrettyRawRepresentable {
    case setting
}

let str = Home.seeting.prettyRawValue // Home_setting

wants

enum Home: XXX {
    case setting
}
let str = Home.seeting.prettyRawValue // Home_setting
  • XXX can be a protocol or a struct, but should better as a subprotocol of RawRepresentable
  • XXX method already be achieved (no more code as the blow)

see more: AccessibilityIdentifier

@ZhipingYang ZhipingYang added the Code Design better architecture to discuss label Aug 1, 2019
@ZhipingYang ZhipingYang self-assigned this Aug 1, 2019
@ZhipingYang ZhipingYang changed the title combine enum's the RawValue type define and it's protocol as one combine enum's the RawValue type define and it's protocol as the one Aug 1, 2019
@ZhipingYang
Copy link
Owner Author

ZhipingYang commented Aug 2, 2019

older test case

 struct AccessibilityID {
    enum Home: String { 
        case setting = "HomeSetting" 
    }
    enum Welcome: String, PrettyRawRepresentable { 
        case setting1
        case setting2 = "WelcomeSetting"
    }
 }
 let h_path = AccessibilityID.Home.setting.rawValue  // "HomeSetting"
 let w_path1 = AccessibilityID.Welcome.setting1.rawValue  // "setting1"
 let w_path1 = AccessibilityID.Welcome.setting1.prettyRawValue  // "AccessibilityID_Welcome_setting1" 
 let w_path1 = AccessibilityID.Welcome.setting2.prettyRawValue  // "WelcomeSetting"

new test case

 struct AccessibilityID {
    enum Home: String { 
        case setting = "HomeSetting" 
    }
    enum Welcome: XXX { 
        case setting1
        case setting2 = "WelcomeSetting"
    }
 }
 let path1 = AccessibilityID.Home.setting.rawValue  // "HomeSetting"
 let path2 = AccessibilityID.Welcome.setting1.rawValue  // "AccessibilityID_Welcome_setting1"
 let path3 = AccessibilityID.Welcome.setting2.rawValue  // "WelcomeSetting"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Code Design better architecture to discuss
Projects
None yet
Development

No branches or pull requests

1 participant