Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
T
typed-reflector
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
3rdeye
typed-reflector
Commits
a127d504
Commit
a127d504
authored
Jul 20, 2022
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add param decorator
parent
574166d7
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
51 additions
and
14 deletions
+51
-14
src/setter.ts
src/setter.ts
+23
-14
src/utility/utility.ts
src/utility/utility.ts
+1
-0
tests/reflect.spec.ts
tests/reflect.spec.ts
+27
-0
No files found.
src/setter.ts
View file @
a127d504
...
...
@@ -9,7 +9,7 @@ export type AllDecorators = MethodDecorator &
export
class
MetadataSetter
<
M
extends
StringDict
,
AM
extends
StringDict
>
{
private
getMetadataInDecorator
<
K
extends
MergeKey
<
M
,
AM
>
,
GM
extends
Record
<
MergeKey
<
M
,
AM
>
,
any
>
=
GenericMap
<
M
,
AM
>
GM
extends
Record
<
MergeKey
<
M
,
AM
>
,
any
>
=
GenericMap
<
M
,
AM
>
,
>
(
metaKey
:
K
,
target
:
any
,
key
?:
any
):
GM
[
K
]
{
if
(
key
)
{
return
Reflect
.
getMetadata
(
metaKey
,
target
,
key
);
...
...
@@ -20,7 +20,7 @@ export class MetadataSetter<M extends StringDict, AM extends StringDict> {
private
setMetadataInDecorator
<
K
extends
MergeKey
<
M
,
AM
>
,
GM
extends
Record
<
MergeKey
<
M
,
AM
>
,
any
>
=
GenericMap
<
M
,
AM
>
GM
extends
Record
<
MergeKey
<
M
,
AM
>
,
any
>
=
GenericMap
<
M
,
AM
>
,
>
(
metaKey
:
K
,
value
:
GM
[
K
],
target
:
any
,
key
?:
any
)
{
if
(
key
)
{
return
Reflect
.
defineMetadata
(
metaKey
,
value
,
target
,
key
);
...
...
@@ -32,23 +32,13 @@ export class MetadataSetter<M extends StringDict, AM extends StringDict> {
transform
<
K
extends
MergeKey
<
M
,
AM
>
,
IK
extends
Key
<
AM
>
,
GM
extends
Record
<
MergeKey
<
M
,
AM
>
,
any
>
=
GenericMap
<
M
,
AM
>
GM
extends
Record
<
MergeKey
<
M
,
AM
>
,
any
>
=
GenericMap
<
M
,
AM
>
,
>
(
metadataKey
:
K
,
metadataValueFun
:
(
oldValue
:
GM
[
K
])
=>
GM
[
K
],
keysIndexMeta
?:
IK
,
):
AllDecorators
{
return
(
target
:
any
,
key
?:
any
,
descriptorOrParamIndex
?:
number
|
any
)
=>
{
const
descriptor
=
typeof
descriptorOrParamIndex
===
'
number
'
?
undefined
:
descriptorOrParamIndex
;
/*
const paramIndex =
typeof descriptorOrParamIndex === 'number'
? descriptorOrParamIndex
: undefined;
*/
return
(
target
,
key
?)
=>
{
const
targetClass
=
getClass
(
target
);
const
oldValue
=
this
.
getMetadataInDecorator
<
K
,
GM
>
(
metadataKey
,
...
...
@@ -122,4 +112,23 @@ export class MetadataSetter<M extends StringDict, AM extends StringDict> {
keysIndexMeta
,
);
}
param
<
K
extends
Key
<
AM
>
,
IK
extends
Key
<
AM
>>
(
metadataKey
:
K
,
metadataValue
:
AM
[
K
],
keysIndexMeta
?:
IK
,
):
ParameterDecorator
{
return
(
obj
,
key
,
i
)
=>
{
const
dec
=
this
.
transform
<
K
,
IK
,
ArrayValueMap
<
AM
>>
(
metadataKey
,
(
arr
)
=>
{
const
newArr
=
arr
||
[];
newArr
[
i
]
=
metadataValue
;
return
newArr
;
},
keysIndexMeta
,
);
dec
(
obj
,
key
);
};
}
}
src/utility/utility.ts
View file @
a127d504
...
...
@@ -6,6 +6,7 @@ export function isClass(target: any) {
return
proto
===
Function
;
}
// eslint-disable-next-line @typescript-eslint/ban-types
export
function
getClass
(
target
:
any
):
Function
{
return
isClass
(
target
)
?
target
:
getClass
(
target
.
constructor
);
}
tests/reflect.spec.ts
View file @
a127d504
...
...
@@ -185,4 +185,31 @@ describe('Reflector', () => {
expect
(
reflector
.
get
(
'
keys
'
,
B
)).
toEqual
([
'
a
'
,
'
b
'
]);
expect
(
reflector
.
get
(
'
keys
'
,
b
)).
toEqual
([
'
a
'
,
'
b
'
]);
});
it
(
'
should work with parameter decorators
'
,
()
=>
{
class
A
{
constructor
(
@
Metadata
.
param
(
'
bar
'
,
1
)
a
:
string
,
@
Metadata
.
param
(
'
bar
'
,
2
)
b
:
string
,
)
{}
method
(
@
Metadata
.
param
(
'
bar
'
,
3
)
a
:
string
,
@
Metadata
.
param
(
'
bar
'
,
4
)
b
:
string
,
)
{}
static
staticMethod
(
@
Metadata
.
param
(
'
bar
'
,
5
)
a
:
string
,
@
Metadata
.
param
(
'
bar
'
,
6
)
b
:
string
,
)
{}
}
const
a
=
new
A
(
'
foo
'
,
'
bar
'
);
expect
(
reflector
.
get
(
'
bar
'
,
A
)).
toStrictEqual
([
1
,
2
]);
expect
(
reflector
.
get
(
'
bar
'
,
a
)).
toStrictEqual
([
1
,
2
]);
expect
(
reflector
.
get
(
'
bar
'
,
A
,
'
method
'
)).
toStrictEqual
([
3
,
4
]);
expect
(
reflector
.
get
(
'
bar
'
,
a
,
'
method
'
)).
toStrictEqual
([
3
,
4
]);
expect
(
reflector
.
get
(
'
bar
'
,
A
,
'
staticMethod
'
)).
toStrictEqual
([
5
,
6
]);
expect
(
reflector
.
get
(
'
bar
'
,
a
,
'
staticMethod
'
)).
toStrictEqual
([
5
,
6
]);
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment