@darabonba/python-generator 2.0.0 → 2.0.1
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/lib/generator.js +10 -6
- package/package.json +1 -1
- package/tests/expected/complex/tea_python_tests/client.py +5 -4
- package/tests/expected/multi/tea_python_tests/model/user.py +3 -0
- package/tests/fixtures/complex/main.dara +1 -0
- package/tests/fixtures/multi/libraries/darabonba_Util_0.2.11/Teafile +7 -1
- package/tests/fixtures/multi/libraries/darabonba_Util_0.2.11/util.tea +2 -0
- package/tests/fixtures/multi/model/user.dara +2 -0
- package/tests/output/complex/tea_python_tests/client.py +5 -4
- package/tests/output/multi/tea_python_tests/model/user.py +3 -0
- package/src/generator.js +0 -231
- package/src/langs/common/combinator.js +0 -193
- package/src/langs/common/config.js +0 -60
- package/src/langs/common/enum.js +0 -179
- package/src/langs/common/items.js +0 -551
- package/src/langs/common/package_info.js +0 -53
- package/src/langs/python/combinator.js +0 -1898
- package/src/langs/python/config.js +0 -169
- package/src/langs/python/files/.gitignore.tmpl +0 -5
- package/src/langs/python/files/__init__.py.tmpl +0 -1
- package/src/langs/python/files/setup.py.tmpl +0 -74
- package/src/langs/python/package_info.js +0 -78
- package/src/langs/python2/combinator.js +0 -1770
- package/src/langs/python2/config.js +0 -162
- package/src/langs/python2/files/.gitignore.tmpl +0 -5
- package/src/langs/python2/files/__init__.py.tmpl +0 -1
- package/src/langs/python2/files/setup.py.tmpl +0 -82
- package/src/langs/python2/package_info.js +0 -78
- package/src/lib/debug.js +0 -55
- package/src/lib/emitter.js +0 -95
- package/src/lib/helper.js +0 -207
- package/src/resolver/base.js +0 -282
- package/src/resolver/client.js +0 -1013
- package/src/resolver/model.js +0 -159
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const defaultConfig = require('../common/config');
|
|
4
|
-
|
|
5
|
-
module.exports = {
|
|
6
|
-
...defaultConfig,
|
|
7
|
-
indent: ' ',
|
|
8
|
-
ext: '.py',
|
|
9
|
-
keywords: {
|
|
10
|
-
general: [
|
|
11
|
-
'and',
|
|
12
|
-
'as',
|
|
13
|
-
'assert',
|
|
14
|
-
'break',
|
|
15
|
-
'class',
|
|
16
|
-
'continue',
|
|
17
|
-
'def',
|
|
18
|
-
'del',
|
|
19
|
-
'elif',
|
|
20
|
-
'else',
|
|
21
|
-
'except',
|
|
22
|
-
'finally',
|
|
23
|
-
'for',
|
|
24
|
-
'from',
|
|
25
|
-
'False',
|
|
26
|
-
'global',
|
|
27
|
-
'if',
|
|
28
|
-
'import',
|
|
29
|
-
'in',
|
|
30
|
-
'is',
|
|
31
|
-
'lambda',
|
|
32
|
-
'not',
|
|
33
|
-
'None',
|
|
34
|
-
'or',
|
|
35
|
-
'pass',
|
|
36
|
-
'raise',
|
|
37
|
-
'return',
|
|
38
|
-
'try',
|
|
39
|
-
'True',
|
|
40
|
-
'while',
|
|
41
|
-
'with',
|
|
42
|
-
'yield'
|
|
43
|
-
],
|
|
44
|
-
function: [],
|
|
45
|
-
class: [],
|
|
46
|
-
param_variables: [
|
|
47
|
-
'exec',
|
|
48
|
-
'print',
|
|
49
|
-
'self'
|
|
50
|
-
]
|
|
51
|
-
},
|
|
52
|
-
symbolMap: {
|
|
53
|
-
'ASSIGN': '=',
|
|
54
|
-
'EQ': '==',
|
|
55
|
-
'NOT': 'NOT',
|
|
56
|
-
'AND': 'and',
|
|
57
|
-
'OR': 'or',
|
|
58
|
-
'PLUS': '+',
|
|
59
|
-
'SUB': '-',
|
|
60
|
-
'MULTI': '*',
|
|
61
|
-
'DIV': '/',
|
|
62
|
-
'POWER': '^',
|
|
63
|
-
'GREATER': '>',
|
|
64
|
-
'GREATER_EQ': '>=',
|
|
65
|
-
'LESS': '<',
|
|
66
|
-
'LESS_EQ': '<=',
|
|
67
|
-
'REVERSE': 'not ',
|
|
68
|
-
'CONCAT': '+'
|
|
69
|
-
},
|
|
70
|
-
typeMap: {
|
|
71
|
-
'string': 'str',
|
|
72
|
-
'boolean': 'bool',
|
|
73
|
-
'number': 'int',
|
|
74
|
-
'integer': 'int',
|
|
75
|
-
'object': 'dict',
|
|
76
|
-
'map': 'dict',
|
|
77
|
-
'bytes': 'bytes',
|
|
78
|
-
'long': 'long',
|
|
79
|
-
'array': 'list',
|
|
80
|
-
'readable': 'READABLE',
|
|
81
|
-
'writable': 'WRITABLE',
|
|
82
|
-
'float': 'float',
|
|
83
|
-
'double': 'float',
|
|
84
|
-
'int64': 'long',
|
|
85
|
-
'int32': 'int',
|
|
86
|
-
'int16': 'int',
|
|
87
|
-
'int8': 'int',
|
|
88
|
-
'ulong': 'int',
|
|
89
|
-
'uint8': 'int',
|
|
90
|
-
'uint16': 'int',
|
|
91
|
-
'uint32': 'int',
|
|
92
|
-
'uint64': 'long',
|
|
93
|
-
'any': 'any',
|
|
94
|
-
'void': 'None',
|
|
95
|
-
'null': 'None'
|
|
96
|
-
},
|
|
97
|
-
type: {
|
|
98
|
-
'long': 'base',
|
|
99
|
-
'unicode': 'base',
|
|
100
|
-
'basestring': 'base',
|
|
101
|
-
'str': 'base',
|
|
102
|
-
'int': 'base',
|
|
103
|
-
'float': 'base',
|
|
104
|
-
'bool': 'base',
|
|
105
|
-
'None': 'base',
|
|
106
|
-
'dict': 'complex',
|
|
107
|
-
'list': 'complex',
|
|
108
|
-
'READABLE': 'custom',
|
|
109
|
-
'WRITABLE': 'custom'
|
|
110
|
-
},
|
|
111
|
-
exceptionMap: {
|
|
112
|
-
'BASE': 'Exception',
|
|
113
|
-
},
|
|
114
|
-
model: {
|
|
115
|
-
dir: 'models',
|
|
116
|
-
mode: 'single_file',
|
|
117
|
-
include: [],
|
|
118
|
-
},
|
|
119
|
-
client: {
|
|
120
|
-
defaultName: 'client',
|
|
121
|
-
include: []
|
|
122
|
-
},
|
|
123
|
-
tea: {
|
|
124
|
-
core: {
|
|
125
|
-
name: 'Tea.core.TeaCore',
|
|
126
|
-
doAction: 'do_action',
|
|
127
|
-
allowRetry: 'allow_retry',
|
|
128
|
-
sleep: 'sleep',
|
|
129
|
-
getBackoffTime: 'get_backoff_time',
|
|
130
|
-
isRetryable: 'is_retryable',
|
|
131
|
-
toModel: 'toModel',
|
|
132
|
-
merge: 'merge',
|
|
133
|
-
toMap: 'to_map',
|
|
134
|
-
fromMap: 'from_map'
|
|
135
|
-
},
|
|
136
|
-
model: {
|
|
137
|
-
name: 'Tea.model.TeaModel'
|
|
138
|
-
},
|
|
139
|
-
converter: {
|
|
140
|
-
name: 'Tea.converter.TeaConverter',
|
|
141
|
-
toStr: 'to_str',
|
|
142
|
-
toUnicode: 'to_unicode',
|
|
143
|
-
toString: 'to_string',
|
|
144
|
-
toBytes: 'to_bytes'
|
|
145
|
-
},
|
|
146
|
-
response: {
|
|
147
|
-
name: 'Tea.response.TeaResponse'
|
|
148
|
-
},
|
|
149
|
-
request: {
|
|
150
|
-
name: 'Tea.request.TeaRequest'
|
|
151
|
-
},
|
|
152
|
-
exception: {
|
|
153
|
-
name: 'Tea.exceptions.TeaException'
|
|
154
|
-
},
|
|
155
|
-
error: {
|
|
156
|
-
name: 'Tea.exceptions.TeaException'
|
|
157
|
-
},
|
|
158
|
-
exceptionUnretryable: {
|
|
159
|
-
name: 'Tea.exceptions.UnretryableException'
|
|
160
|
-
},
|
|
161
|
-
}
|
|
162
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = '0.0.1'
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
"""
|
|
3
|
-
Licensed to the Apache Software Foundation (ASF) under one
|
|
4
|
-
or more contributor license agreements. See the NOTICE file
|
|
5
|
-
distributed with this work for additional information
|
|
6
|
-
regarding copyright ownership. The ASF licenses this file
|
|
7
|
-
to you under the Apache License, Version 2.0 (the
|
|
8
|
-
"License"); you may not use this file except in compliance
|
|
9
|
-
with the License. You may obtain a copy of the License at
|
|
10
|
-
|
|
11
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
-
|
|
13
|
-
Unless required by applicable law or agreed to in writing,
|
|
14
|
-
software distributed under the License is distributed on an
|
|
15
|
-
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
16
|
-
KIND, either express or implied. See the License for the
|
|
17
|
-
specific language governing permissions and limitations
|
|
18
|
-
under the License.
|
|
19
|
-
"""
|
|
20
|
-
|
|
21
|
-
import os
|
|
22
|
-
import sys
|
|
23
|
-
from setuptools import setup, find_packages
|
|
24
|
-
|
|
25
|
-
"""
|
|
26
|
-
setup module for ${name}.
|
|
27
|
-
|
|
28
|
-
Created on ${date}
|
|
29
|
-
|
|
30
|
-
@author: ${author}
|
|
31
|
-
"""
|
|
32
|
-
|
|
33
|
-
PACKAGE = "${package}"
|
|
34
|
-
NAME = "${name}" or "alibabacloud-package"
|
|
35
|
-
DESCRIPTION = "${desc}"
|
|
36
|
-
AUTHOR = "${author}"
|
|
37
|
-
AUTHOR_EMAIL = "${email}"
|
|
38
|
-
URL = "${github}"
|
|
39
|
-
VERSION = __import__(PACKAGE).__version__
|
|
40
|
-
REQUIRES = ${require}
|
|
41
|
-
|
|
42
|
-
LONG_DESCRIPTION = ''
|
|
43
|
-
|
|
44
|
-
if os.path.exists('./README.md'):
|
|
45
|
-
if sys.version_info.major == 2:
|
|
46
|
-
with open("README.md") as fp:
|
|
47
|
-
LONG_DESCRIPTION = fp.read()
|
|
48
|
-
else:
|
|
49
|
-
with open("README.md", encoding="utf-8") as fp:
|
|
50
|
-
LONG_DESCRIPTION = fp.read()
|
|
51
|
-
|
|
52
|
-
setup(
|
|
53
|
-
name=NAME,
|
|
54
|
-
version=VERSION,
|
|
55
|
-
description=DESCRIPTION,
|
|
56
|
-
long_description=LONG_DESCRIPTION,
|
|
57
|
-
long_description_content_type='text/markdown',
|
|
58
|
-
author=AUTHOR,
|
|
59
|
-
author_email=AUTHOR_EMAIL,
|
|
60
|
-
license="Apache License 2.0",
|
|
61
|
-
url=URL,
|
|
62
|
-
keywords=${keywords},
|
|
63
|
-
packages=find_packages(exclude=["tests*"]),
|
|
64
|
-
include_package_data=True,
|
|
65
|
-
platforms="any",
|
|
66
|
-
install_requires=REQUIRES,
|
|
67
|
-
classifiers=(
|
|
68
|
-
"Development Status :: 4 - Beta",
|
|
69
|
-
"Intended Audience :: Developers",
|
|
70
|
-
"License :: OSI Approved :: Apache Software License",
|
|
71
|
-
"Programming Language :: Python",
|
|
72
|
-
"Programming Language :: Python :: 2",
|
|
73
|
-
"Programming Language :: Python :: 2.7",
|
|
74
|
-
"Programming Language :: Python :: 3",
|
|
75
|
-
"Programming Language :: Python :: 3.4",
|
|
76
|
-
"Programming Language :: Python :: 3.5",
|
|
77
|
-
"Programming Language :: Python :: 3.6",
|
|
78
|
-
"Programming Language :: Python :: 3.7",
|
|
79
|
-
"Programming Language :: Python :: 3.8",
|
|
80
|
-
"Topic :: Software Development"
|
|
81
|
-
)
|
|
82
|
-
)
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const debug = require('../../lib/debug');
|
|
4
|
-
const path = require('path');
|
|
5
|
-
const fs = require('fs');
|
|
6
|
-
const BasePackageInfo = require('../common/package_info');
|
|
7
|
-
|
|
8
|
-
const OPTION_LOCAL = 1;
|
|
9
|
-
const OPTION_SOURCE = 2;
|
|
10
|
-
const OPTION_RENDER = 4;
|
|
11
|
-
|
|
12
|
-
// file_name : OPTIONS
|
|
13
|
-
const files = {
|
|
14
|
-
'.gitignore': OPTION_LOCAL,
|
|
15
|
-
'setup.py': OPTION_LOCAL | OPTION_RENDER | OPTION_SOURCE,
|
|
16
|
-
'LICENSE': OPTION_SOURCE,
|
|
17
|
-
'README-CN.md': OPTION_SOURCE | OPTION_RENDER,
|
|
18
|
-
'README.md': OPTION_SOURCE | OPTION_RENDER
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
class PackageInfo extends BasePackageInfo {
|
|
22
|
-
emit(packageInfo, requirePackage) {
|
|
23
|
-
let outputDir = path.join(this.config.dir, '../');
|
|
24
|
-
if (packageInfo.outputDir) {
|
|
25
|
-
outputDir = path.join(outputDir, packageInfo.outputDir);
|
|
26
|
-
}
|
|
27
|
-
if (!fs.existsSync(outputDir)) {
|
|
28
|
-
fs.mkdirSync(outputDir, {
|
|
29
|
-
recursive: true
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
const checkParams = ['name', 'desc', 'github'];
|
|
33
|
-
checkParams.forEach(key => {
|
|
34
|
-
if (typeof packageInfo[key] === 'undefined') {
|
|
35
|
-
debug.stack('need config packageInfo.' + key, packageInfo);
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
let requires = [];
|
|
40
|
-
requirePackage.forEach(packageItem => {
|
|
41
|
-
let packages = packageItem.split(':');
|
|
42
|
-
let [moduleName, version] = packages;
|
|
43
|
-
const requireItem = `${moduleName}>=${version}, <${parseInt(version.split('.')[0])+1}.0.0`;
|
|
44
|
-
requires.push(requireItem);
|
|
45
|
-
});
|
|
46
|
-
const date = new Date();
|
|
47
|
-
const keywords = JSON.stringify(packageInfo.name.split(/_|-/));
|
|
48
|
-
const params = {
|
|
49
|
-
email: '',
|
|
50
|
-
author: '',
|
|
51
|
-
package: this.config.package,
|
|
52
|
-
require: JSON.stringify(requires, null, 4),
|
|
53
|
-
namespace: this.config.package.split('.').join('\\\\'),
|
|
54
|
-
date: ('0' + date.getDate()).slice(-2) + '/' + ('0' + (date.getMonth() + 1)).slice(-2) + '/' + date.getFullYear(),
|
|
55
|
-
keywords: keywords,
|
|
56
|
-
...packageInfo
|
|
57
|
-
};
|
|
58
|
-
Object.keys(files).forEach(filename => {
|
|
59
|
-
let content = '';
|
|
60
|
-
let optional = files[filename];
|
|
61
|
-
if (optional & OPTION_SOURCE && packageInfo.files && packageInfo.files[filename]) {
|
|
62
|
-
let filepath = path.isAbsolute(packageInfo.files[filename]) ?
|
|
63
|
-
packageInfo.files[filename] : path.join(this.config.pkgDir, packageInfo.files[filename]);
|
|
64
|
-
content = fs.readFileSync(filepath).toString();
|
|
65
|
-
} else if (optional & OPTION_LOCAL) {
|
|
66
|
-
content = fs.readFileSync(path.join(__dirname, './files/' + filename + '.tmpl')).toString();
|
|
67
|
-
}
|
|
68
|
-
if (content !== '') {
|
|
69
|
-
if (optional & OPTION_RENDER) {
|
|
70
|
-
content = this.render(content, params);
|
|
71
|
-
}
|
|
72
|
-
fs.writeFileSync(path.join(outputDir, filename), content);
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
module.exports = PackageInfo;
|
package/src/lib/debug.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
const os = require('os');
|
|
3
|
-
|
|
4
|
-
var count = 0;
|
|
5
|
-
|
|
6
|
-
function dump(...data) {
|
|
7
|
-
data.forEach(d => {
|
|
8
|
-
console.log(d);
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function halt(...data) {
|
|
13
|
-
this.dump(...data);
|
|
14
|
-
process.exit(-1);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function jump(jumpNumber = 0, ...data) {
|
|
18
|
-
if (count === jumpNumber) {
|
|
19
|
-
this.halt(...data);
|
|
20
|
-
count = 0;
|
|
21
|
-
} else {
|
|
22
|
-
count++;
|
|
23
|
-
}
|
|
24
|
-
return count;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function stack(...data) {
|
|
28
|
-
let msg = '';
|
|
29
|
-
if (data[0] && typeof data[0] === 'string') {
|
|
30
|
-
msg = data[0];
|
|
31
|
-
data = data.slice(1);
|
|
32
|
-
}
|
|
33
|
-
this.dump(...data);
|
|
34
|
-
throw new Error(msg);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function warning(...data) {
|
|
38
|
-
let msg = '';
|
|
39
|
-
if (data[0] && typeof data[0] === 'string') {
|
|
40
|
-
msg = data[0];
|
|
41
|
-
data = data.slice(1);
|
|
42
|
-
}
|
|
43
|
-
this.dump(...data);
|
|
44
|
-
if (msg.length) {
|
|
45
|
-
process.stdout.write(`\x1b[33m[WARNING] ${msg}\x1b[0m${os.EOL}`);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
module.exports = {
|
|
50
|
-
dump,
|
|
51
|
-
halt,
|
|
52
|
-
stack,
|
|
53
|
-
jump,
|
|
54
|
-
warning
|
|
55
|
-
};
|
package/src/lib/emitter.js
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const fs = require('fs');
|
|
4
|
-
const path = require('path');
|
|
5
|
-
const os = require('os');
|
|
6
|
-
|
|
7
|
-
class Emitter {
|
|
8
|
-
constructor(option = {}) {
|
|
9
|
-
this.config = {
|
|
10
|
-
indent: ' ',
|
|
11
|
-
eol: 'auto', // \n | \r\n | auto
|
|
12
|
-
dir: '',
|
|
13
|
-
layer: '',
|
|
14
|
-
filename: '',
|
|
15
|
-
ext: '',
|
|
16
|
-
...option
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
this.output = '';
|
|
20
|
-
|
|
21
|
-
this.eol = this.config.eol === 'auto' ? os.EOL : this.config.eol;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
indent(level) {
|
|
25
|
-
if (typeof level === 'undefined') {
|
|
26
|
-
level = 0;
|
|
27
|
-
}
|
|
28
|
-
return this.config.indent.repeat(level);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
emit(str = '', level) {
|
|
32
|
-
this.output += this.indent(level) + str;
|
|
33
|
-
return this;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
emitln(str = '', level) {
|
|
37
|
-
this.emit(str + this.eol, level);
|
|
38
|
-
return this;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
emits(level, ...strs) {
|
|
42
|
-
strs.forEach(str => {
|
|
43
|
-
this.emitln(str, level);
|
|
44
|
-
});
|
|
45
|
-
return this;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
erase(lenght) {
|
|
49
|
-
this.output = this.output.substring(0, this.output.length - lenght);
|
|
50
|
-
return this;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
savePath() {
|
|
54
|
-
return path.join(
|
|
55
|
-
this.config.dir,
|
|
56
|
-
this.config.layer.split('.').join(path.sep),
|
|
57
|
-
this.config.filename + this.config.ext
|
|
58
|
-
);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
save(append = false) {
|
|
62
|
-
if (!this.config.dir) {
|
|
63
|
-
throw new Error('`option.dir` should not be empty');
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (!this.config.filename) {
|
|
67
|
-
throw new Error('filename cannot be empty');
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const targetPath = this.savePath();
|
|
71
|
-
|
|
72
|
-
if (!fs.existsSync(path.dirname(targetPath))) {
|
|
73
|
-
fs.mkdirSync(path.dirname(targetPath), {
|
|
74
|
-
recursive: true
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (append && fs.existsSync(targetPath)) {
|
|
79
|
-
fs.appendFileSync(targetPath, this.output);
|
|
80
|
-
} else {
|
|
81
|
-
fs.writeFileSync(targetPath, this.output);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
this.output = '';
|
|
85
|
-
|
|
86
|
-
const consoleInfo = append ? 'append file : ' : 'save path : ';
|
|
87
|
-
if (this.config.showInfo) {
|
|
88
|
-
console.log('');
|
|
89
|
-
console.log(`\x1b[32${consoleInfo}${path.resolve(targetPath)}\x1b[0m`);
|
|
90
|
-
console.log('');
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
module.exports = Emitter;
|
package/src/lib/helper.js
DELETED
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const DSL = require('@darabonba/parser');
|
|
4
|
-
|
|
5
|
-
let config = {};
|
|
6
|
-
|
|
7
|
-
function _config(langConfig = null) {
|
|
8
|
-
if (null !== langConfig) {
|
|
9
|
-
config = langConfig;
|
|
10
|
-
}
|
|
11
|
-
return config;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function _upperFirst(str) {
|
|
15
|
-
if (!str) {
|
|
16
|
-
return '';
|
|
17
|
-
}
|
|
18
|
-
return str[0].toUpperCase() + str.substring(1);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function _camelCase(str, split = '_') {
|
|
22
|
-
if (str.indexOf(split) > -1) {
|
|
23
|
-
let tmp = str.split(split);
|
|
24
|
-
tmp = tmp.map((s, i) => {
|
|
25
|
-
if (s.length > 0 && i !== 0) {
|
|
26
|
-
return _upperFirst(s);
|
|
27
|
-
}
|
|
28
|
-
return s;
|
|
29
|
-
});
|
|
30
|
-
str = tmp.join('');
|
|
31
|
-
}
|
|
32
|
-
return str;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function _lowerFirst(str) {
|
|
36
|
-
return str[0].toLowerCase() + str.substring(1);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function _subModelName(name) {
|
|
40
|
-
return name.split('.').map((name) => _upperFirst(name)).join('');
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function _string(str) {
|
|
44
|
-
if (str.string === '""') {
|
|
45
|
-
return '\\"\\"';
|
|
46
|
-
}
|
|
47
|
-
return str.string.replace(/([^\\])"+|^"/g, function (str) {
|
|
48
|
-
return str.replace(/"/g, '\\"');
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function _isBasicType(type) {
|
|
53
|
-
return DSL.util.isBasicType(type);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function _deepClone(obj) {
|
|
57
|
-
return JSON.parse(JSON.stringify(obj));
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
function _avoidKeywords(str) {
|
|
61
|
-
if (config.keywords.general.indexOf(str.toLowerCase()) > -1) {
|
|
62
|
-
return str + '_';
|
|
63
|
-
}
|
|
64
|
-
return str;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function _avoidFuncKeywords(str) {
|
|
68
|
-
if (config.keywords.general.indexOf(str.toLowerCase()) > -1
|
|
69
|
-
|| config.keywords.function.indexOf(str.toLowerCase()) > -1) {
|
|
70
|
-
return str + '_';
|
|
71
|
-
}
|
|
72
|
-
return str;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function _avoidClassKeywords(str) {
|
|
76
|
-
if (config.keywords.general.indexOf(str.toLowerCase()) > -1
|
|
77
|
-
|| config.keywords.class.indexOf(str.toLowerCase()) > -1) {
|
|
78
|
-
return str + '_';
|
|
79
|
-
}
|
|
80
|
-
return str;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function _avoidVarKeywords(str) {
|
|
84
|
-
if (config.keywords.general.indexOf(str.toLowerCase()) > -1
|
|
85
|
-
|| config.keywords.param_variables.indexOf(str.toLowerCase()) > -1) {
|
|
86
|
-
return str + '_';
|
|
87
|
-
}
|
|
88
|
-
return str;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function _convertStaticParam(param) {
|
|
92
|
-
if (param === '__response') {
|
|
93
|
-
param = config.response;
|
|
94
|
-
} else if (param === '__request') {
|
|
95
|
-
param = config.request;
|
|
96
|
-
}
|
|
97
|
-
return param;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function _isKeywords(str) {
|
|
101
|
-
return config.keywords.general.indexOf(str.toLowerCase()) > -1;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function _modify(modify) {
|
|
105
|
-
if (Array.isArray(modify)) {
|
|
106
|
-
return modify.filter((m) => config.modifyOrder.indexOf(m) > -1)
|
|
107
|
-
.map((m) => _modify(m)).sort(function (a, b) {
|
|
108
|
-
return config.modifyOrder.indexOf(a.toUpperCase()) - config.modifyOrder.indexOf(b.toUpperCase());
|
|
109
|
-
}).join(' ');
|
|
110
|
-
}
|
|
111
|
-
return modify.toLowerCase();
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
function _symbol(str) {
|
|
115
|
-
if (config.symbolMap[str]) {
|
|
116
|
-
return config.symbolMap[str];
|
|
117
|
-
}
|
|
118
|
-
throw new Error(`Unsupported symbol : ${str}`);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
function _exception(str) {
|
|
122
|
-
if (config.exceptionMap[str]) {
|
|
123
|
-
return config.exceptionMap[str];
|
|
124
|
-
}
|
|
125
|
-
return str;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function _isSnakeCase(str) {
|
|
129
|
-
if (/[^\da-z_]/.test(str)) {
|
|
130
|
-
return false;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
if (/\d/.test(str[0])) {
|
|
134
|
-
return false;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return true;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
function _toCamelCase(str) {
|
|
141
|
-
if (!str) {
|
|
142
|
-
return '';
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
const word = str.split('_');
|
|
146
|
-
let tmp = '';
|
|
147
|
-
word.forEach(w => {
|
|
148
|
-
tmp += _upperFirst(w);
|
|
149
|
-
});
|
|
150
|
-
return tmp;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
function _toSnakeCase(str) {
|
|
154
|
-
if (!str) {
|
|
155
|
-
return '';
|
|
156
|
-
}
|
|
157
|
-
let res = '';
|
|
158
|
-
let tmp = '';
|
|
159
|
-
for (const c of str) {
|
|
160
|
-
if (/[A-Z|0-9]/.test(c)) {
|
|
161
|
-
tmp += c;
|
|
162
|
-
} else {
|
|
163
|
-
if (tmp.length > 0) {
|
|
164
|
-
res += res === '' ? tmp.toLowerCase() : '_' + tmp.toLowerCase();
|
|
165
|
-
tmp = '';
|
|
166
|
-
}
|
|
167
|
-
res += c;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
if (tmp.length > 0) {
|
|
171
|
-
res += '_' + tmp.toLowerCase();
|
|
172
|
-
}
|
|
173
|
-
res = res.replace(/-/g, '_');
|
|
174
|
-
if (res[0] === '_' && str[0] !== '_') {
|
|
175
|
-
res = res.substring(1);
|
|
176
|
-
}
|
|
177
|
-
return res;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
function _escape(str) {
|
|
181
|
-
return str.includes('-') ? `${str}` : str;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
module.exports = {
|
|
186
|
-
_config,
|
|
187
|
-
_escape,
|
|
188
|
-
_upperFirst,
|
|
189
|
-
_camelCase,
|
|
190
|
-
_string,
|
|
191
|
-
_subModelName,
|
|
192
|
-
_lowerFirst,
|
|
193
|
-
_isBasicType,
|
|
194
|
-
_deepClone,
|
|
195
|
-
_avoidKeywords,
|
|
196
|
-
_avoidFuncKeywords,
|
|
197
|
-
_avoidClassKeywords,
|
|
198
|
-
_avoidVarKeywords,
|
|
199
|
-
_convertStaticParam,
|
|
200
|
-
_isKeywords,
|
|
201
|
-
_modify,
|
|
202
|
-
_symbol,
|
|
203
|
-
_exception,
|
|
204
|
-
_toSnakeCase,
|
|
205
|
-
_toCamelCase,
|
|
206
|
-
_isSnakeCase
|
|
207
|
-
};
|