Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
K
Koishi Utils Schemagen
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
Koishi Utils Schemagen
Commits
ef40b463
Commit
ef40b463
authored
Oct 07, 2021
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add SchemaConf
parent
bcaff9bf
Pipeline
#6138
passed with stages
in 30 seconds
Changes
7
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
60 additions
and
20 deletions
+60
-20
README.md
README.md
+4
-0
package.json
package.json
+1
-1
src/constants.ts
src/constants.ts
+1
-0
src/decorators.ts
src/decorators.ts
+9
-2
src/def.ts
src/def.ts
+5
-2
src/methods.ts
src/methods.ts
+25
-12
test/test-transform.ts
test/test-transform.ts
+15
-3
No files found.
README.md
View file @
ef40b463
...
...
@@ -14,6 +14,10 @@ npm install koishi-utils-schemagen reflect-metadata
```
ts
// 使用装饰器定义 Schema。
// SchemaConf 填写 Schema 本身的顶级属性。
@
SchemaConf
({
desc
:
'
my desc
'
,
})
export
class
Config
{
// 基本的数据类型
@
DefineSchema
({
type
:
'
number
'
,
required
:
true
})
...
...
package.json
View file @
ef40b463
{
"name"
:
"koishi-utils-schemagengen"
,
"version"
:
"1.0.
6
"
,
"version"
:
"1.0.
7
"
,
"description"
:
"在 Koishi.js 中,使用类装饰器定义 Schema"
,
"main"
:
"dist/src/index.js"
,
"typings"
:
"dist/src/index.d.ts"
,
...
...
src/constants.ts
View file @
ef40b463
export
const
SchemaMetaKey
=
'
_koishi_schema_meta
'
;
export
const
SchemaKeysMetaKey
=
'
_koishi_schemakeys_meta
'
;
export
const
SchemaClassKey
=
'
_koishi_schema_class_meta
'
;
src/decorators.ts
View file @
ef40b463
import
{
SchemaOptions
}
from
'
./def
'
;
import
{
Schema
ClassOptions
,
Schema
Options
}
from
'
./def
'
;
import
'
reflect-metadata
'
;
import
{
SchemaKeysMetaKey
,
SchemaMetaKey
}
from
'
./constants
'
;
import
{
Schema
ClassKey
,
Schema
KeysMetaKey
,
SchemaMetaKey
}
from
'
./constants
'
;
import
{
Schema
}
from
'
koishi
'
;
import
{
ClassConstructor
,
...
...
@@ -43,6 +43,13 @@ export function DefineSchema(options: SchemaOptions): PropertyDecorator {
};
}
export
function
SchemaConf
(
options
:
SchemaClassOptions
):
ClassDecorator
{
return
(
obj
)
=>
{
const
objClass
=
obj
;
Reflect
.
defineMetadata
(
SchemaClassKey
,
options
,
objClass
);
};
}
export
function
UseSchema
(
schema
:
Schema
)
{
return
DefineSchema
({
schema
});
}
...
...
src/def.ts
View file @
ef40b463
...
...
@@ -9,13 +9,16 @@ export type SchemaType =
|
'
never
'
|
ClassConstructor
<
any
>
;
export
interface
SchemaOptions
{
schema
?:
Schema
<
any
>
;
export
interface
SchemaClassOptions
{
desc
?:
string
;
required
?:
boolean
;
hidden
?:
boolean
;
comment
?:
string
;
default
?:
any
;
}
export
interface
SchemaOptions
extends
SchemaClassOptions
{
schema
?:
Schema
<
any
>
;
dict
?:
boolean
;
array
?:
boolean
;
type
?:
SchemaType
;
...
...
src/methods.ts
View file @
ef40b463
...
...
@@ -4,9 +4,9 @@ import {
plainToClass
,
TransformOptions
,
}
from
'
class-transformer
'
;
import
{
SchemaOptions
,
SchemaOptionsDict
}
from
'
./def
'
;
import
{
Schema
ClassOptions
,
Schema
Options
,
SchemaOptionsDict
}
from
'
./def
'
;
import
'
reflect-metadata
'
;
import
{
SchemaKeysMetaKey
,
SchemaMetaKey
}
from
'
./constants
'
;
import
{
Schema
ClassKey
,
Schema
KeysMetaKey
,
SchemaMetaKey
}
from
'
./constants
'
;
import
_
from
'
lodash
'
;
function
getBasePropertySchemaFromOptions
(
options
:
SchemaOptions
)
{
...
...
@@ -32,14 +32,7 @@ function getBasePropertySchemaFromOptions(options: SchemaOptions) {
}
}
function
getPropertySchemaFromOptions
<
PT
>
(
options
:
SchemaOptions
):
Schema
<
PT
>
{
let
schema
=
getBasePropertySchemaFromOptions
(
options
);
if
(
options
.
dict
)
{
schema
=
Schema
.
dict
(
schema
);
}
if
(
options
.
array
)
{
schema
=
Schema
.
array
(
schema
);
}
function
applyOptionsToSchema
(
schema
:
Schema
,
options
:
SchemaClassOptions
)
{
if
(
options
.
required
!=
undefined
)
{
schema
.
_required
=
options
.
required
;
}
...
...
@@ -52,6 +45,20 @@ function getPropertySchemaFromOptions<PT>(options: SchemaOptions): Schema<PT> {
if
(
options
.
comment
!=
undefined
)
{
schema
.
_comment
=
options
.
comment
;
}
if
(
options
.
desc
!=
undefined
)
{
schema
.
desc
=
options
.
desc
;
}
}
function
getPropertySchemaFromOptions
<
PT
>
(
options
:
SchemaOptions
):
Schema
<
PT
>
{
let
schema
=
getBasePropertySchemaFromOptions
(
options
);
if
(
options
.
dict
)
{
schema
=
Schema
.
dict
(
schema
);
}
if
(
options
.
array
)
{
schema
=
Schema
.
array
(
schema
);
}
applyOptionsToSchema
(
schema
,
options
);
return
schema
;
}
...
...
@@ -83,11 +90,17 @@ function schemaOptionsFromClass<T>(
}
export
function
schemaFromClass
<
T
>
(
cl
:
ClassConstructor
<
T
>
):
Schema
<
T
>
{
let
schema
:
Schema
;
const
optionsDict
=
schemaOptionsFromClass
(
cl
);
if
(
!
optionsDict
)
{
return
Schema
.
any
();
schema
=
Schema
.
any
();
}
else
{
schema
=
schemasFromDict
<
T
>
(
optionsDict
);
}
return
schemasFromDict
<
T
>
(
optionsDict
);
const
extraOptions
:
SchemaClassOptions
=
Reflect
.
getMetadata
(
SchemaClassKey
,
cl
)
||
{};
applyOptionsToSchema
(
schema
,
extraOptions
);
return
schema
;
}
export
function
schemaTransform
<
T
>
(
cl
:
ClassConstructor
<
T
>
,
data
:
any
):
T
{
...
...
test/test-transform.ts
View file @
ef40b463
import
{
DefineSchema
,
schemaFromClass
,
schemaTransform
}
from
'
../src
'
;
import
{
DefineSchema
,
SchemaConf
,
schemaFromClass
,
schemaTransform
,
}
from
'
../src
'
;
import
{
Schema
}
from
'
koishi
'
;
@
SchemaConf
({
desc
:
'
my desc
'
,
})
class
B
{
@
DefineSchema
({
type
:
'
number
'
,
default
:
2
})
@
DefineSchema
({
type
:
'
number
'
,
default
:
2
,
desc
:
'
aaaaa
'
})
aa
:
number
;
@
DefineSchema
({
type
:
'
boolean
'
,
default
:
true
})
bb
:
boolean
;
}
@
SchemaConf
({
desc
:
'
my base desc
'
,
})
class
A
{
@
DefineSchema
({
type
:
'
number
'
,
required
:
true
})
a
:
number
;
...
...
@@ -30,6 +41,7 @@ class A {
}
const
schema
=
schemaFromClass
(
A
);
console
.
log
(
JSON
.
stringify
(
schema
,
null
,
2
));
const
testObject
=
{
a
:
4
,
...
...
@@ -42,7 +54,7 @@ const testObject = {
// console.log(JSON.stringify(testValidate, null, 2));
const
testTransform
=
schemaTransform
(
A
,
testObject
);
console
.
log
(
JSON
.
stringify
(
testTransform
,
null
,
2
));
//
console.log(JSON.stringify(testTransform, null, 2));
console
.
log
(
testTransform
.
bi
instanceof
B
);
console
.
log
(
testTransform
.
biArr
[
0
]
instanceof
B
);
console
.
log
(
testTransform
.
biDict
[
0
].
cccc
instanceof
B
);
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