-
-
Notifications
You must be signed in to change notification settings - Fork 150
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
feat(binding): save property type metadata to bindable definition #1689
Open
ekzobrain
wants to merge
2
commits into
aurelia:master
Choose a base branch
from
ekzo-dev:feature/bindable-coercion-meta
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ import { isString, objectFreeze, objectKeys } from './utilities'; | |
import type { Constructable, Writable } from '@aurelia/kernel'; | ||
import type { InterceptorFunc } from '@aurelia/runtime'; | ||
|
||
type PropertyType = typeof Number | typeof String | typeof Boolean | typeof BigInt | { coercer: InterceptorFunc } | Class<unknown>; | ||
type PropertyType = typeof Number | typeof String | typeof Boolean | typeof BigInt | typeof Array | typeof Object | typeof Function | { coercer: InterceptorFunc } | Class<unknown>; | ||
|
||
export type PartialBindableDefinition = { | ||
mode?: BindingMode; | ||
|
@@ -242,16 +242,22 @@ export class BindableDefinition { | |
public readonly primary: boolean, | ||
public readonly property: string, | ||
public readonly set: InterceptorFunc, | ||
public readonly type?: PropertyType, | ||
public readonly nullable?: boolean, | ||
) { } | ||
|
||
public static create(prop: string, target: Constructable<unknown>, def: PartialBindableDefinition = {}): BindableDefinition { | ||
const type = def.type ?? Metadata.get('design:type', target, prop); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use the |
||
|
||
return new BindableDefinition( | ||
firstDefined(def.attribute, kebabCase(prop)), | ||
firstDefined(def.callback, `${prop}Changed`), | ||
firstDefined(def.mode, BindingMode.toView), | ||
firstDefined(def.primary, false), | ||
firstDefined(def.property, prop), | ||
firstDefined(def.set, getInterceptor(prop, target, def)), | ||
firstDefined(def.set, getInterceptor(type, def.nullable)), | ||
type, | ||
def.nullable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use the |
||
); | ||
} | ||
} | ||
|
@@ -345,8 +351,7 @@ const Coercer = { | |
} | ||
}; | ||
|
||
function getInterceptor(prop: string, target: Constructable<unknown>, def: PartialBindableDefinition = {}) { | ||
const type: PropertyType | null = def.type ?? Metadata.get('design:type', target, prop) ?? null; | ||
function getInterceptor(type?: PropertyType, nullable?: boolean) { | ||
if (type == null) { return noop; } | ||
let coercer: InterceptorFunc; | ||
switch (type) { | ||
|
@@ -367,13 +372,13 @@ function getInterceptor(prop: string, target: Constructable<unknown>, def: Parti | |
} | ||
return coercer === noop | ||
? coercer | ||
: createCoercer(coercer, def.nullable); | ||
: createCoercer(coercer, nullable); | ||
} | ||
|
||
function createCoercer<TInput, TOutput>(coercer: InterceptorFunc<TInput, TOutput>, nullable: boolean | undefined): InterceptorFunc<TInput, TOutput> { | ||
return function (value: TInput, coercionConfiguration: ICoercionConfiguration | null): TOutput { | ||
if (!coercionConfiguration?.enableCoercion) return value as unknown as TOutput; | ||
return ((nullable ?? ((coercionConfiguration?.coerceNullish ?? false) ? false : true)) && value == null) | ||
return ((nullable ?? !(coercionConfiguration?.coerceNullish ?? false)) && value == null) | ||
? value as unknown as TOutput | ||
: coercer(value, coercionConfiguration); | ||
}; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two new properties should not be
undefined
. If there is no value available, please usenull
fortype
(and thereby making the type oftype
toPropertyType | null
) andtrue
fornullable
(considering sensible defaults; what do you think @bigopon?).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you prefer
null
overundefined
? You mean that undefined stands for unset value, and null - for empty value?In case of
nullable
we cannot settrue
as a default, it should be undefined/null, because logic behind this field is: use value if it is defined, otherwise use global setting, which defaults totrue
. So if we settrue
here, global settings will neder be invoked.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Sayan751 reply to the above, please. So I will finish up with this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Also,
undefined
can indicate absence of the value or the property in the object. As far as I know, there is seldom optional properties in the internal classes used in Au2 that deals with user (devs) provided values/configuration. Plus maintaining strict type definition also means that when you are making any comparison, you can safely and strictly compare it withnull
; that is=== null
instead of== null
or other sort of comparison that results in coercion, and thereby less work at runtime.The property can be set to
null
if no value is explicitly provided. However, I am not sure what global setting you are referring to. As far as I understand, thenullable
property introduced in theBindableDefinition
class isn't used anywhere.Sidenote: only an explicit
nullable: false
makes sense. Refer to the docs in this regard. Hence, my suggestion to set it totrue
by default.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean
coercionConfiguration.coerceNullish
, see line 381. It is used whennullable
isnull
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. I see what you have meant. I comment is regarding L260 when the value is persisted in
BindableDefinition#nullable
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think interceptor is kind of a public facing value, forcing it to be null will results in annoyance down the line. We can leave it at undefined. Probably applies for many other public facing stuff as well.