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

🪟🧹 Improve connector form types #20057

Merged
merged 5 commits into from
Dec 7, 2022

Conversation

flash1293
Copy link
Contributor

@flash1293 flash1293 commented Dec 5, 2022

What

Improve some types in relation to #17352

#17352 itself can be tackled once #14250 is done.

How

  • All FormItem types inherit some properties from the underlying JSON schema, and the types should reflect that (also we need the extra default for the linked related task)
  • Remove custom pick implementation and use the lodash one to reduce the number of places where typing is lost in the intermediate processing (Object.fromEntries returns an object with almost no type information)

@octavia-squidington-iv octavia-squidington-iv added area/platform issues related to the platform area/frontend Related to the Airbyte webapp labels Dec 5, 2022
@flash1293 flash1293 marked this pull request as ready for review December 5, 2022 16:13
@flash1293 flash1293 requested a review from a team as a code owner December 5, 2022 16:13
Copy link
Contributor

@lmossman lmossman left a comment

Choose a reason for hiding this comment

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

Had a couple questions

"default" | "examples" | "description" | "pattern" | "order" | "const" | "title" | "airbyte_hidden" | "enum"
>;

interface FormItem extends FormRelevantJSONSchema {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason to keep order, title, description, and airbyte_hidden in this interface, now that they are coming from FormRelevantJSONSchema?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is not, good catch.

const partialSchema: Partial<AirbyteJSONSchema> = {
...Object.fromEntries(Object.entries(schema).filter(([k]) => defaultFields.includes(k as keyof AirbyteJSONSchema))),
};
type DefaultFields = Pick<AirbyteJSONSchema, typeof defaultFields[number] | "enum">;
Copy link
Contributor

Choose a reason for hiding this comment

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

A couple questions here:

  1. What is typeof defaultFields[number] doing? It seems like this is just trying to pick all of the fields in the defaultFields array from AirbyteJSONSchema, but I don't know what that number in the brackets is being defined. Is this some functionality specific to Pick?
  2. Why not just add "enum" to the defaultFields array? This way you wouldn't need to do | "enum" unless I've missed something

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I simplified this part of the code, by changing a bit the branching it gets much more readable IMHO.

What is typeof defaultFields[number] doing

It's a way to get a union type of all items in the const array:
Screenshot 2022-12-06 at 11 53 15

The logic is that basically the type of a const array is this:

{
  0: "a",
  1: "b",
  2: "c"
}

so, if you get the types of all number properties of that type, you end up with "a" | "b" | "c". This is called an index access type: https://www.typescriptlang.org/docs/handbook/2/indexed-access-types.html

Just as a side note here, the new code doesn't need that anymore because it adds "enum" in all cases, then removes it again for the "single value enum with default" case which is converted to const. The old code would omit it at first and not add it in this special situation, but the code becomes more readable that way and it also makes pick do the heavy lifting for determining the type which is nice.

@flash1293
Copy link
Contributor Author

Based on your comments I simplified a bit further, let me know if there are other questions.

Copy link
Contributor

@lmossman lmossman left a comment

Choose a reason for hiding this comment

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

Changes LGTM. Tested with connectors that have conditional fields and didn't notice any regression

@flash1293 flash1293 merged commit e12d366 into master Dec 7, 2022
@flash1293 flash1293 deleted the chore-improve-connector-form-types branch December 7, 2022 14:45
@flash1293 flash1293 mentioned this pull request Dec 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/frontend Related to the Airbyte webapp area/platform issues related to the platform
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants