icerockdev / moko-kswift

Swift-friendly api generator for Kotlin/Native frameworks
https://moko.icerock.dev
Apache License 2.0
348 stars 21 forks source link

Not nested sealed subclasses #64

Closed ema987 closed 1 year ago

ema987 commented 1 year ago

Subclasses of sealed class declared outside of their parent body end up breaking Swift enums generation. This is similar to #43 , I'm opening a new issue to point out the bug is not related to sealed interfaces but to the nesting of the subclasses.

I took the NonGenericSealedClass in the example and moved out the subclasses to have the following:

sealed class ExternalNonGenericSealedClass

object ExternalNonGenericWithoutProperty : ExternalNonGenericSealedClass()

data class ExternalNonGenericWithProperty(val value: String) : ExternalNonGenericSealedClass()

Generated Swift code is this one:

public enum ExternalNonGenericSealedClassKs {

  case com/icerockdev/library/ExternalNonGenericWithoutProperty
  case com/icerockdev/library/ExternalNonGenericWithProperty(ExternalNonGenericWithProperty)

  public var sealed: ExternalNonGenericSealedClass {
    switch self {
    case .com/icerockdev/library/ExternalNonGenericWithoutProperty:
      return MultiPlatformLibrary.ExternalNonGenericWithoutProperty() as MultiPlatformLibrary.ExternalNonGenericSealedClass
    case .com/icerockdev/library/ExternalNonGenericWithProperty(let obj):
      return obj as! MultiPlatformLibrary.ExternalNonGenericSealedClass
    }
  }

  public init(_ obj: ExternalNonGenericSealedClass) {
    if obj is MultiPlatformLibrary.ExternalNonGenericWithoutProperty {
      self = .com/icerockdev/library/ExternalNonGenericWithoutProperty
    } else if let obj = obj as? MultiPlatformLibrary.ExternalNonGenericWithProperty {
      self = .com/icerockdev/library/ExternalNonGenericWithProperty(obj)
    } else {
      fatalError("ExternalNonGenericSealedClassKs not synchronized with ExternalNonGenericSealedClass class")
    }
  }

}

Expected code to be generated:

public enum ExternalNonGenericSealedClassKs {

  case externalNonGenericWithoutProperty
  case externalNonGenericWithProperty(ExternalNonGenericWithProperty)

  public var sealed: ExternalNonGenericSealedClass {
    switch self {
    case .externalNonGenericWithoutProperty:
      return MultiPlatformLibrary.ExternalNonGenericWithoutProperty() as MultiPlatformLibrary.ExternalNonGenericSealedClass
    case .externalNonGenericWithProperty(let obj):
      return obj as! MultiPlatformLibrary.ExternalNonGenericSealedClass
    }
  }

  public init(_ obj: ExternalNonGenericSealedClass) {
    if obj is MultiPlatformLibrary.ExternalNonGenericWithoutProperty {
      self = .externalNonGenericWithoutProperty
    } else if let obj = obj as? MultiPlatformLibrary.ExternalNonGenericWithProperty {
      self = .externalNonGenericWithProperty(obj)
    } else {
      fatalError("ExternalNonGenericSealedClassKs not synchronized with ExternalNonGenericSealedClass class")
    }
  }

}