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

metarpheus - tagged union generation issues #107

Open
4 tasks
Cardu opened this issue Jan 23, 2020 · 2 comments
Open
4 tasks

metarpheus - tagged union generation issues #107

Cardu opened this issue Jan 23, 2020 · 2 comments
Labels
BE maint bug Something isn't working

Comments

@Cardu
Copy link
Contributor

Cardu commented Jan 23, 2020

trying the new version of metarpheus in gj, we found following issues:

this

@JsonCodec sealed trait AnalyticsReadError
object AnalyticsReadError {
  case object BotNotFound extends AnalyticsReadError
}

produces the following non-transipiling code:

export const AnalyticsReadError = t.union([
  t.type({
    _type: t.literal('BotNotFound')
  }, 'BotNotFound')
], 'AnalyticsReadError')
final case class RuleConfig(
  ruleId: Id[Rule],
  params: Map[NonBlankString, NonBlankString],
)

export interface RuleConfig {
  ruleId: Id<Rule>,
  params: Record<NonBlankString, NonBlankString>
}
  • sealed trait with only object implementation produces empty union type
@JsonCodec sealed trait BotDeployStatusError

object BotDeployStatusError {
  object BotNotFound extends BotDeployStatusError
}

export const BotDeployStatusError = t.union([

], 'BotDeployStatusError')
  • sealed trait that are implemented by an object containing class implementing the same trait because they are a recursive structure, generates two different models, and the second is an unknown

In GJ we have this Tree structure defined as:

sealed trait AnalyzerTree
object AnalyzerTree {
  final case class Op(
    opType: AnalyzerOpType,
    children: NonEmptyList[AnalyzerTree],
  ) extends AnalyzerTree

  final case class Atom(ruleConfig: RuleConfig) extends AnalyzerTree

...<some other methods here>

export type AnalyzerTree =
  | {
  opType: AnalyzerOpType,
  children: NonEmptyList<AnalyzerTree>,
  _type: 'Op'
}
  | {
  ruleConfig: RuleConfig,
  _type: 'Atom'
}

export const AnalyzerTree = t.union([
  t.type({
    opType: AnalyzerOpType,
    children: NonEmptyList(AnalyzerTree),
    _type: t.literal('Op')
  }, 'Op'),
  t.type({
    ruleConfig: RuleConfig,
    _type: t.literal('Atom')
  }, 'Atom')
], 'AnalyzerTree')

export type AnalyzerTree = unknown;
export const AnalyzerTree = t.unknown;
@Cardu Cardu added the bug Something isn't working label Jan 23, 2020
@Cardu
Copy link
Contributor Author

Cardu commented Jan 23, 2020

Bonus point

  • type referring another type with same name but different package creates recursive code
package getjenny.backofficeapi.botimport

import getjenny.organization.Organization
import getjenny.core.model.Id
import io.circe.generic.JsonCodec

@JsonCodec sealed trait BotImportError

object BotImportError {
  final case class OrganizationNotFound(organizationId: Id[Organization]) extends BotImportError
  final case class BotCreateError(cause: getjenny.bot.BotCreateError) extends BotImportError

export interface BotCreateError {
  cause: BotCreateError
}

export const BotCreateError: t.RecursiveType<t.Type<BotCreateError>> = t.recursion('BotCreateError', () => t.type({
  cause: BotCreateError
}, 'BotCreateError'))

@tpetrucciani
Copy link
Contributor

I've moved the first two to new issues in metarpheus-io-ts.


For "sealed trait with only object implementation produces empty union type", I think the solution is just to make object BotNotFound a case object – non-case objects are IMO intentionally unsupported (I think it's the only non-case object implementing a sealed trait in GJ).

However, it makes sense to support sealed traits without implementations, which we do use (e.g. OrganizationListError in GJ), so I've mentioned them in buildo/metarpheus-io-ts#115.


For the last two, I'm not sure we want / need to solve them for now, but I haven't really thought about them yet, I'd give it another look before we decide what to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BE maint bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants