Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 50 additions & 50 deletions lib/generator.js

Large diffs are not rendered by default.

48 changes: 45 additions & 3 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,34 @@ function _camelCase(str, split = '_') {
return str;
}

function _snakeCase(str) {
function _snakeCase(str, keys = []) {
if (!str) {
return '';
}

// Convert keys to array if it's a single string
if (typeof keys === 'string') {
keys = [keys];
}

let result = str;

// First, handle the keywords by replacing them with placeholders
const placeholders = {};
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (key && result.includes(key)) {
const placeholder = `@@_${i}@@`;
placeholders[placeholder] = `_${key.toLowerCase()}_`;
result = result.split(key).join(placeholder);
}
}

// Process the string normally
let res = '';
let tmp = '';
for (const c of str) {

for (const c of result) {
if (/[A-Z|0-9]/.test(c)) {
tmp += c;
} else {
Expand All @@ -230,13 +251,34 @@ function _snakeCase(str) {
res += c;
}
}

if (tmp.length > 0) {
res += '_' + tmp.toLowerCase();
res += (res === '' ? '' : '_') + tmp.toLowerCase();
}

res = res.replace(/-/g, '_');

// Clean up any double underscores
res = res.replace(/_+/g, '_');

// Restore the special keywords from placeholders
for (const [placeholder, value] of Object.entries(placeholders)) {
const regex = new RegExp(placeholder, 'g');
res = res.replace(regex, value);
}

res = res.replace(/_+/g, '_');

// Remove leading underscore if original string didn't start with one
if (res[0] === '_' && str[0] !== '_') {
res = res.substring(1);
}

// Remove trailing underscore if original string didn't end with one
if (res[res.length - 1] === '_' && str[str.length - 1] !== '_') {
res = res.substring(0, res.length - 1);
}

return res;
}

Expand Down
12 changes: 7 additions & 5 deletions tests/expected/complex/tea_python_tests/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
class Client(SourceClient):
_protocol: str = None
_pathname: str = None
__strs: List[str] = None
_strs: List[str] = None
_comple_list: List[List[str]] = None
_endpoint_map: Dict[str, str] = None
_configs: List[source_models.Config] = None
Expand Down Expand Up @@ -206,11 +206,11 @@ def complex_1(
any_map = {}
any_map[item] = 'test'
break
self.__strs = request.strs
self._strs = request.strs
self._protocol = 'test'
self._endpoint_map.get(self._protocol)
self._endpoint_map['test'] = 'ok'
request.strs = self.__strs
request.strs = self._strs
_request.protocol = self._protocol
_request.port = request.num
_request.method = 'GET'
Expand Down Expand Up @@ -421,11 +421,11 @@ async def complex_1_async(
any_map = {}
any_map[item] = 'test'
break
self.__strs = request.strs
self._strs = request.strs
self._protocol = 'test'
self._endpoint_map.get(self._protocol)
self._endpoint_map['test'] = 'ok'
request.strs = self.__strs
request.strs = self._strs
_request.protocol = self._protocol
_request.port = request.num
_request.method = 'GET'
Expand Down Expand Up @@ -611,6 +611,7 @@ def complex_3(
x = y
resp = _response
req = source_models.Request(
cors_rules = 'test',
accesskey = request.access_key,
region = resp.status_message
)
Expand Down Expand Up @@ -653,6 +654,7 @@ async def complex_3_async(
x = y
resp = _response
req = source_models.Request(
cors_rules = 'test',
accesskey = request.access_key,
region = resp.status_message
)
Expand Down
12 changes: 12 additions & 0 deletions tests/expected/function/tea_python_tests/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,15 @@ def a_params(self) -> None:

async def a_params_async(self) -> None:
await self.hello_params_async('a', 'b')

def test_jwt_with_option(self) -> None:
self.hello_params('a', 'b')

async def test_jwt_with_option_async(self) -> None:
await self.hello_params_async('a', 'b')

def test_oauth2_for_option(self) -> None:
self.hello_params('a', 'b')

async def test_oauth2_for_option_async(self) -> None:
await self.hello_params_async('a', 'b')
36 changes: 36 additions & 0 deletions tests/expected/model/tea_python_tests/models/_my_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class MyModel(DaraModel):
def __init__(
self,
model: main_models.MyModelModel = None,
cors_rules: str = None,
cors_rules_cors_r: str = None,
xml_parser_http_message: str = None,
test_oauth2_name: str = None,
stringfield: str = None,
bytesfield: bytes = None,
stringarrayfield: List[str] = None,
Expand Down Expand Up @@ -60,6 +64,10 @@ def __init__(
link: str = None,
):
self.model = model
self.cors_rules = cors_rules
self.cors_rules_cors_r = cors_rules_cors_r
self.xml_parser_http_message = xml_parser_http_message
self.test_oauth2_name = test_oauth2_name
self.stringfield = stringfield
self.bytesfield = bytesfield
self.stringarrayfield = stringarrayfield
Expand Down Expand Up @@ -109,6 +117,10 @@ def validate(self):
self.validate_required(self.model, 'model')
if self.model:
self.model.validate()
self.validate_required(self.cors_rules, 'cors_rules')
self.validate_required(self.cors_rules_cors_r, 'cors_rules_cors_r')
self.validate_required(self.xml_parser_http_message, 'xml_parser_http_message')
self.validate_required(self.test_oauth2_name, 'test_oauth2_name')
self.validate_required(self.stringfield, 'stringfield')
self.validate_required(self.bytesfield, 'bytesfield')
self.validate_required(self.stringarrayfield, 'stringarrayfield')
Expand Down Expand Up @@ -220,6 +232,18 @@ def to_map(self):
if self.model is not None:
result['model'] = self.model.to_map()

if self.cors_rules is not None:
result['CORSRules'] = self.cors_rules

if self.cors_rules_cors_r is not None:
result['CORSRulesCORSR'] = self.cors_rules_cors_r

if self.xml_parser_http_message is not None:
result['XMLParserHTTPMessage'] = self.xml_parser_http_message

if self.test_oauth2_name is not None:
result['TestOAuth2Name'] = self.test_oauth2_name

if self.stringfield is not None:
result['stringfield'] = self.stringfield

Expand Down Expand Up @@ -405,6 +429,18 @@ def from_map(self, m: dict = None):
temp_model = main_models.MyModelModel()
self.model = temp_model.from_map(m.get('model'))

if m.get('CORSRules') is not None:
self.cors_rules = m.get('CORSRules')

if m.get('CORSRulesCORSR') is not None:
self.cors_rules_cors_r = m.get('CORSRulesCORSR')

if m.get('XMLParserHTTPMessage') is not None:
self.xml_parser_http_message = m.get('XMLParserHTTPMessage')

if m.get('TestOAuth2Name') is not None:
self.test_oauth2_name = m.get('TestOAuth2Name')

if m.get('stringfield') is not None:
self.stringfield = m.get('stringfield')

Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/complex/Darafile
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
"main": "./main.dara",
"libraries": {
"Source": "alibabacloud:Import:*"
},
"python": {
"keywords": [
"CORS"
]
}
}
5 changes: 4 additions & 1 deletion tests/fixtures/complex/libraries/Darafile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
},
"python": {
"package": "Source",
"clientName": "source_client"
"clientName": "source_client",
"keywords": [
"CORS"
]
},
"python2": {
"package": "Source",
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/complex/libraries/import.dara
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ static function parse(c: class): void;
static async function asyncFunc(): void;

model Request = {
CORSRules: string,
accesskey?: string(name='accesskey', description='accesskey'),
region?: string(name='region', description='region'),
instance: {
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/complex/main.dara
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ api Complex3(request: ComplexRequest, name: string): ComplexRequest {
}
var resp = __response;
var req = new Source.Request{
CORSRules = "test",
accesskey = request.accessKey,
region = resp.statusMessage
};
Expand Down
8 changes: 7 additions & 1 deletion tests/fixtures/function/Darafile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@
"scope": "alibabacloud",
"name": "main",
"version": "0.0.1",
"main": "./main.dara"
"main": "./main.dara",
"python": {
"keywords": [
"JWT",
"OAuth2"
]
}
}
8 changes: 8 additions & 0 deletions tests/fixtures/function/main.dara
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,12 @@ static async function helloInterface(): void;

async function aParams(): void {
helloParams("a", "b");
}

async function testJWTWithOption(): void {
helloParams("a", "b");
}

async function testOAuth2forOption(): void {
helloParams("a", "b");
}
8 changes: 8 additions & 0 deletions tests/fixtures/model/Darafile
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,13 @@
"main": "./main.dara",
"libraries": {
"Source": "darabonba:Import:*"
},
"python": {
"keywords": [
"CORS",
"XML",
"HTTP",
"OAuth2"
]
}
}
4 changes: 4 additions & 0 deletions tests/fixtures/model/main.dara
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ model MyModel = {
str: string
},
},
CORSRules: string,
CORSRulesCORSR: string,
XMLParserHTTPMessage: string,
TestOAuth2Name: string,
stringfield: string,
bytesfield: bytes,
stringarrayfield: [ string ],
Expand Down
Loading