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

Adding mapping between enum keys and values #17

Merged
merged 9 commits into from
Jun 21, 2018

Conversation

rickyp-uber
Copy link
Collaborator

Currently for enum generation keys and values are just unions and there is no way to put this data back together. This change will create a map object to allow referencing of one value to the other.

@CLAassistant
Copy link

CLAassistant commented Jun 9, 2018

CLA assistant check
All committers have signed the CLA.

Can't actually use types in real code
@koulmomo
Copy link
Contributor

You should add a test

package.json Outdated
@@ -1,6 +1,6 @@
{
"name": "thrift2flow",
"version": "0.2.12",
"version": "0.2.13",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't bump package version, that will happen after the pull request is landed and a new version is released.

@rickyp-uber
Copy link
Collaborator Author

Unit test added

@rickyp-uber
Copy link
Collaborator Author

@keithkml mind taking a look?

}

return `export type ${this.transformName(def.id.name)}Values = ${this.generateEnumValues(
def
)};
export type ${this.transformName(def.id.name)} = ${this.generateEnumKeys(def)};`;
export type ${this.transformName(def.id.name)} = ${this.generateEnumKeys(def)};
export const ${this.transformName(def.id.name)}Map = ${this.generateEnumMap(def)};`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, this kinda defeats the purpose of the type name suffix, which will lead to linting errors. Actually it looks like the previous change which added xxxKeys broke it too, but I didn't notice. Easy solution is to move the Map and Keys suffix into the call to transformName

@rickyp-uber
Copy link
Collaborator Author

Example Input:

typedef MyEnum EnumTypedef

enum MyEnum {
  OK = 1
  ERROR = 2
}

struct MyStruct {
  1: MyEnum f_MyEnum
  2: EnumTypedef f_EnumTypedef
}

Output

// @flow

// Generated by thrift2flow at Mon Jun 18 2018 15:14:45 GMT-0700 (PDT)

export type EnumTypedefType = $Keys<typeof MyEnumType>;

export const MyEnumType = {
  OK: 1,
  ERROR: 2
};

export type MyStructType = {|
  f_MyEnum: $Keys<typeof MyEnumType>,
  f_EnumTypedef: EnumTypedefType
|};

@rickyp-uber
Copy link
Collaborator Author

this.generateImports(),
...this.thrift.asts[this.thrift.filename].definitions.map(this.convertDefinitionToCode)
...this.thrift.asts[this.thrift.filename].definitions.map((value, index, array) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this code would look a lot nicer (and this diff would be a lot smaller :) ) if you just stored this as a field, or even just declared a getter function for this value.

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

Successfully merging this pull request may close these issues.

None yet

4 participants