question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support union-like classes in projenrc.json

See original GitHub issue

Union-like values are instances of classes that behave like unions in other languages. In TypeScript, the constructor is made private, and the only way to instantiate the class is by calling one of its static methods, each of which may represent a different kind of value or simply a different way to initialize the object.

Working backwards, we might imagine if a project’s constructor has an option that is typed as a union-like :

export class ReleaseTrigger {
  private constructor(options: ReleaseTriggerOptions = {}) {
    // ...
  }
  
  public static manual(options: ManualOptions) {
    return new ReleaseTrigger(...);
  }
  
  public static scheduled(options: ScheduledOptions) {
    return new ReleaseTrigger(...);
  }
}

Then the user should be able to configure it in their projenrc.json like this:

{
  ...
  releaseTrigger: {
    manual: {
      // manual options
      // these should decompose into
      // object/array/boolean/number/string/enum
    }
  }
  // or
  releaseTrigger: {
    scheduled: {
      // scheduled options
    }
  }
  

Open questions:

  • Does jsii support union-like classes explicitly? What type information is provided / how easy is it to retrieve the information for the different options?
  • What should happen if one of the methods expects a value that can’t be deconstructed? e.g. MyUnionLike.fromChoiceA(project: Project, props: ChoiceAOptions). Does this imply fromChoiceA will not be available for use in projenrc.json?
  • ~How much extra effort is required to support union-likes for other projenrc types (e.g. projenrc.java)?~ Actually I think we already have this for free from jsii.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
eladbcommented, Sep 13, 2021

Does jsii support union-like classes explicitly? What type information is provided / how easy is it to retrieve the information for the different options?

No explicit support for union-like classes at the moment, but those types are jsii-compatible (just classes with a bunch of static methods).

What should happen if one of the methods expects a value that can’t be deconstructed

I think it implies it won’t be usable in JSON. I am wondering if we should prohibit such APIs (basically add a test that validates that all APIs are deconstructable).

How much extra effort is required to support union-likes for other projenrc types

Indeed, union-likes are compatible with JSII and can be used from all JSII languages. That’s part of the reason we used this pattern.

Prior art

I am wondering if we should vend a “declarative constructs” library that supports all of this once and can be used by all CDKs. A related capability is producing a JSON schema for these files (see https://github.com/projen/projen/issues/1017).

0reactions
github-actions[bot]commented, Mar 15, 2022

This issue is now marked as stale because it hasn’t seen activity for a while. Add a comment or it will be closed soon. If you wish to exclude this issue from being marked as stale, add the “backlog” label.

Read more comments on GitHub >

github_iconTop Results From Across the Web

API Reference | projen
Projenrc, Sets up a project to use JSON for projenrc. ... AutoDiscoverBase, Base class for auto-discovering and creating project subcomponents.
Read more >
projen/projen: A new generation of project generators - GitHub
projen synthesizes project configuration files such as package.json ... strongly-typed class and execute projen to update their project configuration files.
Read more >
How to simplify your next software project set-up - TIQQE
I would recommend to use the current long-term support of Node.js, ... are supported as well, including Typescript, JSON, Python, Java, etc.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found