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

Nullable object properties #231

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

roertbb
Copy link
Contributor

@roertbb roertbb commented Jul 3, 2022

Hi there! 👋 I stumbled upon that case 2 or 3 times and decide to give it a shot to solve my problem and most likely help some other great folks as well 😄

I've read the hints from #202, extended ModelValueType, adjusted the generation of nested object values within nullable nested objects and also adjusted types here and there to properly handle nullable nested objects.

Sharing some context behind technical decisions/changes I've made:

  • I decided to add isGetterFunctionReturningObject to NullableProperty - it indicates if we should generate properties for nested nullable objects in src/model/parseModelDefinition.ts. It's needed when we have some getter function returning object with nested properties in factory and we're not providing any value for these nested object properties during model creation (case is covered in nullable object property > when object getter is provided in factory definition > defaults to value provided in factory when not set during model creation test)
  • I also created getDefinition function to "unwrap" the getter value from NullableProperty when we want to get the default values of nested properties within nullable object properties
  • I've extended the type for Value in glossary.ts to properly infer types in case of some object is passed as generic type. It was also needed to make nulllable<Date>(() => null) to work properly (you can find some examples in test/query/date.test.ts)
  • changes in src/query/queryTypes.ts were required when querying dates with lte / gte (case is covered in ignores entities with missing values when querying using date test)

I tried to provide API without a getter function (as mentioned in #203), however I failed on providing proper typing for it (some raw PoC can be found in roertbb#1). Recently, decided to neglect that idea and try out extending existing NullableProperty (which is using getter function) to incorporate objects there.

Working on that one was a pretty good journey into depths of mswjs/data, I think I understood quite some of them, but I still may be missing some other parts - I'd be really grateful for guidance on what I could've missed or how I could make it more elegant and readable 😉

I also wrote couple of tests that served me as a test cases. These are not extensively testing all the possible use-cases, but should be a good foundation. If you have other test-cases on your mind, if you feel that it will be more readable to structure tests some other way - please share some feedback 😉

Resolves #203

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.

Support nested nullable objects
1 participant