@voxgig/sdkgen 4.35.1 → 4.35.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/voxgig-sdkgen
CHANGED
package/package.json
CHANGED
|
@@ -17,7 +17,7 @@ import {
|
|
|
17
17
|
const LANG = 'py'
|
|
18
18
|
|
|
19
19
|
|
|
20
|
-
// Python keywords
|
|
20
|
+
// Python keywords, which cannot be class-syntax TypedDict field names.
|
|
21
21
|
const PY_KEYWORDS = new Set([
|
|
22
22
|
'False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break',
|
|
23
23
|
'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally',
|
|
@@ -32,32 +32,49 @@ function pyIdent(name: string): boolean {
|
|
|
32
32
|
|
|
33
33
|
|
|
34
34
|
function emitTypedDict(typeName: string, items: any[], log?: any): void {
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
items.forEach((it: any) => {
|
|
38
|
-
if (it && null != it.name && !pyIdent(it.name) && log && log.warn) {
|
|
39
|
-
log.warn({
|
|
40
|
-
point: 'entity-types-skip-field', typeName, field: it.name,
|
|
41
|
-
note: `py: field "${it.name}" of ${typeName} has no legal class-syntax ` +
|
|
42
|
-
`TypedDict key form; omitted from the typed model (still reachable ` +
|
|
43
|
-
`via the runtime dict)`,
|
|
44
|
-
})
|
|
45
|
-
}
|
|
46
|
-
})
|
|
35
|
+
const named = items.filter((it: any) => it && null != it.name)
|
|
47
36
|
|
|
48
|
-
const required =
|
|
49
|
-
const optional =
|
|
37
|
+
const required = named.filter((it: any) => !it.optional)
|
|
38
|
+
const optional = named.filter((it: any) => it.optional)
|
|
50
39
|
|
|
51
40
|
const field = (it: any) => ` ${it.name}: ${canonToType(it.type, LANG)}
|
|
52
41
|
`
|
|
53
42
|
|
|
54
|
-
if (0 ===
|
|
43
|
+
if (0 === named.length) {
|
|
55
44
|
Content(`class ${typeName}(TypedDict):
|
|
56
45
|
pass
|
|
57
46
|
`)
|
|
58
47
|
return
|
|
59
48
|
}
|
|
60
49
|
|
|
50
|
+
// A keyword, or a name that is not an identifier, has no class-syntax
|
|
51
|
+
// form. The functional form takes any string as a key, so the whole type
|
|
52
|
+
// takes that form; a mixed one inherits a required and an optional half.
|
|
53
|
+
if (named.some((it: any) => !pyIdent(it.name))) {
|
|
54
|
+
const entry = (it: any) => ` ${JSON.stringify(it.name)}: ${canonToType(it.type, LANG)},
|
|
55
|
+
`
|
|
56
|
+
const dict = (name: string, its: any[], total: boolean) =>
|
|
57
|
+
`${name} = TypedDict('${name}', {
|
|
58
|
+
` + its.map(entry).join('') + `}${total ? '' : ', total=False'})
|
|
59
|
+
`
|
|
60
|
+
|
|
61
|
+
if (0 === required.length || 0 === optional.length) {
|
|
62
|
+
Content(dict(typeName, named, 0 === optional.length))
|
|
63
|
+
return
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
Content(dict(typeName + 'Required', required, true))
|
|
67
|
+
Content(`
|
|
68
|
+
`)
|
|
69
|
+
Content(dict(typeName + 'Optional', optional, false))
|
|
70
|
+
Content(`
|
|
71
|
+
|
|
72
|
+
class ${typeName}(${typeName}Required, ${typeName}Optional):
|
|
73
|
+
pass
|
|
74
|
+
`)
|
|
75
|
+
return
|
|
76
|
+
}
|
|
77
|
+
|
|
61
78
|
if (0 === required.length) {
|
|
62
79
|
Content(`class ${typeName}(TypedDict, total=False):
|
|
63
80
|
`)
|