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

Don't use enums, namespaces #80

Open
PuruVJ opened this issue Aug 19, 2024 · 0 comments
Open

Don't use enums, namespaces #80

PuruVJ opened this issue Aug 19, 2024 · 0 comments

Comments

@PuruVJ
Copy link
Collaborator

PuruVJ commented Aug 19, 2024

Some parts of our SDK uses enums and one specific part(UpdateStatus) uses namespaces as a runtime feature. These are non-standard TS-only features which are compiled to JS. The input and output are vastly different. This should be converted to a class like all other modules in client_api folder.

Compiled output:

var UpdateStatus;
((UpdateStatus2) => {
  function getAlgebraicType() {
    return AlgebraicType.createSumType([
      new SumTypeVariant("Committed", DatabaseUpdate.getAlgebraicType()),
      new SumTypeVariant(
        "Failed",
        AlgebraicType.createPrimitiveType(BuiltinType.Type.String)
      ),
      new SumTypeVariant("OutOfEnergy", AlgebraicType.createProductType([]))
    ]);
  }
  UpdateStatus2.getAlgebraicType = getAlgebraicType;
  function serialize(value) {
    switch (value.tag) {
      case "Committed":
        return { Committed: DatabaseUpdate.serialize(value.value) };
      case "Failed":
        return { Failed: value.value };
      case "OutOfEnergy":
        return { OutOfEnergy: [] };
      default:
        throw "unreachable";
    }
  }
  UpdateStatus2.serialize = serialize;
  UpdateStatus2.Committed = (value) => ({
    tag: "Committed",
    value
  });
  UpdateStatus2.Failed = (value) => ({ tag: "Failed", value });
  UpdateStatus2.OutOfEnergy = { tag: "OutOfEnergy", value: void 0 };
  function fromValue(value) {
    let sumValue = value.asSumValue();
    switch (sumValue.tag) {
      case 0:
        return {
          tag: "Committed",
          value: DatabaseUpdate.fromValue(sumValue.value)
        };
      case 1:
        return { tag: "Failed", value: sumValue.value.asString() };
      case 2:
        return { tag: "OutOfEnergy", value: void 0 };
      default:
        throw "unreachable";
    }
  }
  UpdateStatus2.fromValue = fromValue;
})(UpdateStatus || (UpdateStatus = {}));

enums deserve similar treatment

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