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

EnumCase.rawValue returns "-" for negative enum rawValues #1010

Closed
S-V opened this issue Nov 14, 2021 · 0 comments
Closed

EnumCase.rawValue returns "-" for negative enum rawValues #1010

S-V opened this issue Nov 14, 2021 · 0 comments

Comments

@S-V
Copy link

S-V commented Nov 14, 2021

Hi,
we've noticed a regression in Sourcery 1.6.0 when dealing with negative raw enum values.
It's a legacy project that was previously using Sourcery 1.2.1.

What version of Swift are you using (swift --version)?

Apple Swift version 5.5.1 (swiftlang-1300.0.31.4 clang-1300.0.29.6)

What did you do?

My template (simplified from https://github.com/eugeneego/legacy/blob/master/Sources/Templates/Transformer.swifttemplate):

<%_

func enumRawValue(for enumCase: EnumCase) -> String {
    return enumCase.rawValue ?? "\"\(enumCase.name)\""
}
-%>

<%
for enumType in types.enums {
-%>

// swiftlint:disable all
struct TestTransformer {
    typealias Source = Any
    typealias Destination = Test

    func transform(source value: Source) -> TransformerResult<Destination> {
        guard let rawValue = transformer.transform(source: value).value else { return .failure(.transform) }

        switch rawValue {
            <%_ for enumCase in enumType.cases { -%>
            case <%= enumRawValue(for: enumCase) %>:
                return .success(.<%= enumCase.name %>)
            <%_ } -%>
            default:
                return defaultCaseHandler();
        }
    }

    func transform(destination value: Destination) -> TransformerResult<Source> {
        switch value {
            <%_ for enumCase in enumType.cases { -%>
            case .<%= enumCase.name %>:
                return transformer.transform(destination: <%= enumRawValue(for: enumCase) %>)
            <%_ } -%>
        }
    }
}

<%
}
-%>

My enum:

// sourcery: transformer
struct TestStruct {
    var state: StateType

    // sourcery: enumTransformer
    enum StateType: Int {
        // sourcery: defaultCase
        case unsupported = -1
        case success = 0
        case failure = 1
        case cancelled = 2
    }
}

What did you expect to see?

// swiftlint:disable all
struct TestTransformer {
    typealias Source = Any
    typealias Destination = Test

    func transform(source value: Source) -> TransformerResult<Destination> {
        guard let rawValue = transformer.transform(source: value).value else { return .failure(.transform) }

        switch rawValue {
            case -1:
                return .success(.unsupported)
            case 0:
                return .success(.success)
            case 1:
                return .success(.failure)
            case 2:
                return .success(.cancelled)
            default:
                return defaultCaseHandler();
        }
    }

    func transform(destination value: Destination) -> TransformerResult<Source> {
        switch value {
            case .unsupported:
                return transformer.transform(destination: -1)
            case .success:
                return transformer.transform(destination: 0)
            case .failure:
                return transformer.transform(destination: 1)
            case .cancelled:
                return transformer.transform(destination: 2)
        }
    }
}

What did you see instead?

Notice "case -" and "(destination: -)" - these do not compile and we have to correct them manually.

// swiftlint:disable all
struct TestTransformer {
    typealias Source = Any
    typealias Destination = Test

    func transform(source value: Source) -> TransformerResult<Destination> {
        guard let rawValue = transformer.transform(source: value).value else { return .failure(.transform) }

        switch rawValue {
            case -:
                return .success(.unsupported)
            case 0:
                return .success(.success)
            case 1:
                return .success(.failure)
            case 2:
                return .success(.cancelled)
            default:
                return defaultCaseHandler();
        }
    }

    func transform(destination value: Destination) -> TransformerResult<Source> {
        switch value {
            case .unsupported:
                return transformer.transform(destination: -)
            case .success:
                return transformer.transform(destination: 0)
            case .failure:
                return transformer.transform(destination: 1)
            case .cancelled:
                return transformer.transform(destination: 2)
        }
    }
}
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

No branches or pull requests

1 participant