Skip to content

Commit d348d06

Browse files
committed
fix: resolve declare and template_string
1 parent bf39fba commit d348d06

File tree

11 files changed

+225
-12
lines changed

11 files changed

+225
-12
lines changed

src/langs/swift/combinator.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ class Combinator extends CombinatorBase {
279279
}
280280

281281
emitConstruct(emitter, node, hasExtends) {
282+
// New Client need try
282283
emitter.emitln(`public${hasExtends ? ' override ' : ' '}init(${this.resolveFuncParams(node.params)}) throws {`, this.level);
283284
this.levelUp();
284285
node.body.forEach(element => {
@@ -783,6 +784,9 @@ class Combinator extends CombinatorBase {
783784
grammerNewObject(emitter, gram) {
784785
let objectName = '';
785786
objectName = gram.name;
787+
if (objectName.indexOf('^') === 0) { // New Client
788+
emitter.emit('try ');
789+
}
786790
emitter.emit(`${this.resolveName(objectName)}(`);
787791
if (!Array.isArray(gram.params)) {
788792
this.grammerValue(emitter, gram.params);
@@ -869,6 +873,9 @@ class Combinator extends CombinatorBase {
869873
let emit = new Emitter(this.config);
870874
if (item.dataType instanceof TypeString) {
871875
this.grammer(emit, item, false, false);
876+
if (item.type !== 'string') {
877+
emit.output = `(${emit.output})`;
878+
}
872879
} else {
873880
emit.emit('String(');
874881
this.grammer(emit, item, false, false);

src/resolver/client.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ class ClientResolver extends BaseResolver {
561561
if (i !== (object.propertyPath.length - 1)) {
562562
resolve = true;
563563
}
564-
call.addPath({ type: call_type, name: path_name, dataType: path_type, needCast: model && resolve });
564+
call.addPath({ type: call_type, name: path_name, dataType: path_type, needCast: resolve });
565565
last_path_type = path_type;
566566
});
567567

@@ -590,7 +590,7 @@ class ClientResolver extends BaseResolver {
590590
let behaviorTamplateString = new BehaviorTamplateString();
591591
object.elements.forEach(ele => {
592592
if (ele.type !== 'element') {
593-
behaviorTamplateString.addItem(this.renderGrammerValue(null, ele.expr));
593+
behaviorTamplateString.addItem(this.renderGrammerValue(null, ele.expr, null, true));
594594
} else {
595595
behaviorTamplateString.addItem(new GrammerValue('string', ele.value.string, new TypeString()));
596596
}
@@ -692,7 +692,7 @@ class ClientResolver extends BaseResolver {
692692
const objectName = `^${object.aliasId.lexeme}`;
693693
valGrammer.value = new GrammerNewObject(objectName);
694694
object.args.forEach(item => {
695-
valGrammer.value.addParam(this.renderGrammerValue(null, item));
695+
valGrammer.value.addParam(this.renderGrammerValue(null, item, null, canCast));
696696
});
697697
} else if (object.type === 'boolean') {
698698
valGrammer.type = 'bool';
@@ -717,7 +717,7 @@ class ClientResolver extends BaseResolver {
717717
valGrammer.expected = expectedType ? this.resolveTypeItem(expectedType) : null;
718718
} else if (object.type === 'property') {
719719
object.type = 'property_access';
720-
this.renderGrammerValue(valGrammer, object);
720+
this.renderGrammerValue(valGrammer, object, null, canCast);
721721
} else if (object.type === 'super') {
722722
valGrammer.type = 'call';
723723
let call = new GrammerCall('super', undefined, undefined, undefined, object.hasThrow, object.isAsync);
@@ -833,16 +833,16 @@ class ClientResolver extends BaseResolver {
833833
node = this.renderGrammerValue(null, stmt);
834834
} else if (stmt.type === 'declare') {
835835
let type = null;
836-
if (stmt.expectedType) {
837-
type = this.resolveTypeItem(stmt.expectedType);
838-
} else if (stmt.expr.inferred) {
836+
if (stmt.expr && stmt.expr.inferred) {
839837
let inferred = stmt.expr.inferred;
840838
if (stmt.expr.propertyPathTypes && stmt.expr.propertyPathTypes.length) {
841839
if (stmt.expr.propertyPathTypes[stmt.expr.propertyPathTypes.length - 1].type === 'map') {
842840
inferred = stmt.expr.propertyPathTypes[stmt.expr.propertyPathTypes.length - 1].keyType;
843841
}
844842
}
845843
type = this.resolveTypeItem(inferred);
844+
} else if (stmt.expectedType) {
845+
type = this.resolveTypeItem(stmt.expectedType);
846846
} else {
847847
debug.stack(stmt);
848848
}

tests/expected/complex/Sources/Darabonba_Main/Client.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ open class Client : DarabonbaImport.Client {
4343
var moduleMapVal: [String: DarabonbaImport.Client] = [:]
4444
var modelMapVal: [String: ComplexRequest] = [:]
4545
var subModelMapVal: [String: ComplexRequest.Header] = [:]
46-
var version: String = "/" + "2019-01-08" + self._pathname
46+
var version: String = "/" + "2019-01-08" + self._pathname ?? ""
4747
var mapAccess: String = _API[version as! String] ?? ""
4848
var reqMap: [String: ComplexRequest]? = nil
4949
var mapString: [String: String] = [
@@ -103,7 +103,7 @@ open class Client : DarabonbaImport.Client {
103103
_request.protocol_ = self._protocol ?? ""
104104
_request.port = 80
105105
_request.method = "GET"
106-
_request.pathname = "/" + self._pathname
106+
_request.pathname = "/" + self._pathname ?? ""
107107
_request.query = DarabonbaImport.Client.query(Tea.TeaConverter.merge([
108108
"date": "2019",
109109
"access": mapAccess as! String,
@@ -355,7 +355,7 @@ open class Client : DarabonbaImport.Client {
355355

356356
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
357357
public func TemplateString() async throws -> String {
358-
return "/\n" + self._protocol
358+
return "/\n" + self._protocol ?? ""
359359
}
360360

361361
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
@@ -401,7 +401,7 @@ open class Client : DarabonbaImport.Client {
401401
}
402402

403403
public func throwsFunc() throws -> String {
404-
return "/" + self._protocol
404+
return "/" + self._protocol ?? ""
405405
}
406406

407407
public func throwsFunc1() throws -> String {

tests/expected/exec/Client.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Tea
66

77
open class Client {
88
public static func test(_ str: String?) throws -> String {
9-
return str + "\n"
9+
return str as! String + "\n"
1010
}
1111

1212
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)

tests/expected/import/Sources/Package/Client.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ open class Client {
1919
])
2020
}
2121

22+
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
23+
public func testClient() async throws -> Void {
24+
var tmp: Import.Client? = nil
25+
tmp = try Import.Client()
26+
}
27+
2228
public static func Sample(_ client: Import.Client?) -> Void {
2329
var runtime: Import.RuntimeObject = Import.RuntimeObject([:])
2430
var request: Import.Request = Import.Request([
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Foundation
2+
import Tea
3+
4+
open class Client {
5+
public init() throws {
6+
}
7+
8+
public func isSet(_ a: Any) -> Bool {
9+
return true
10+
}
11+
12+
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
13+
public func test() async throws -> Void {
14+
var res: Response = Response([:])
15+
var test: String = "test1" + (res.body!.name ?? "") + String(res.body!.len!)
16+
var req: Request = Request([:])
17+
req.b = res.body!.name
18+
req.b = "test2" + (res.body!.name ?? "") + String(res.body!.len!)
19+
var a: String = res.body!.name ?? ""
20+
if (isSet(res.body!)) {
21+
var b: String = res.body!.name ?? ""
22+
}
23+
}
24+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import Foundation
2+
import Tea
3+
4+
public class Request : Tea.TeaModel {
5+
public var a: [String: String]?
6+
7+
public var b: String?
8+
9+
public var c: Int?
10+
11+
public override init() {
12+
super.init()
13+
}
14+
15+
public init(_ dict: [String: Any]) {
16+
super.init()
17+
self.fromMap(dict)
18+
}
19+
20+
public override func validate() throws -> Void {
21+
}
22+
23+
public override func toMap() -> [String : Any] {
24+
var map = super.toMap()
25+
if self.a != nil {
26+
map["a"] = self.a!
27+
}
28+
if self.b != nil {
29+
map["b"] = self.b!
30+
}
31+
if self.c != nil {
32+
map["c"] = self.c!
33+
}
34+
return map
35+
}
36+
37+
public override func fromMap(_ dict: [String: Any]) -> Void {
38+
if dict.keys.contains("a") && dict["a"] != nil {
39+
self.a = dict["a"] as! [String: String]
40+
}
41+
if dict.keys.contains("b") && dict["b"] != nil {
42+
self.b = dict["b"] as! String
43+
}
44+
if dict.keys.contains("c") && dict["c"] != nil {
45+
self.c = dict["c"] as! Int
46+
}
47+
}
48+
}
49+
50+
public class Response : Tea.TeaModel {
51+
public class Body : Tea.TeaModel {
52+
public var name: String?
53+
54+
public var len: Int64?
55+
56+
public override init() {
57+
super.init()
58+
}
59+
60+
public init(_ dict: [String: Any]) {
61+
super.init()
62+
self.fromMap(dict)
63+
}
64+
65+
public override func validate() throws -> Void {
66+
}
67+
68+
public override func toMap() -> [String : Any] {
69+
var map = super.toMap()
70+
if self.name != nil {
71+
map["name"] = self.name!
72+
}
73+
if self.len != nil {
74+
map["len"] = self.len!
75+
}
76+
return map
77+
}
78+
79+
public override func fromMap(_ dict: [String: Any]) -> Void {
80+
if dict.keys.contains("name") && dict["name"] != nil {
81+
self.name = dict["name"] as! String
82+
}
83+
if dict.keys.contains("len") && dict["len"] != nil {
84+
self.len = dict["len"] as! Int64
85+
}
86+
}
87+
}
88+
public var header: [String: String]?
89+
90+
public var body: Response.Body?
91+
92+
public override init() {
93+
super.init()
94+
}
95+
96+
public init(_ dict: [String: Any]) {
97+
super.init()
98+
self.fromMap(dict)
99+
}
100+
101+
public override func validate() throws -> Void {
102+
try self.body?.validate()
103+
}
104+
105+
public override func toMap() -> [String : Any] {
106+
var map = super.toMap()
107+
if self.header != nil {
108+
map["header"] = self.header!
109+
}
110+
if self.body != nil {
111+
map["body"] = self.body?.toMap()
112+
}
113+
return map
114+
}
115+
116+
public override func fromMap(_ dict: [String: Any]) -> Void {
117+
if dict.keys.contains("header") && dict["header"] != nil {
118+
self.header = dict["header"] as! [String: String]
119+
}
120+
if dict.keys.contains("body") && dict["body"] != nil {
121+
var model = Response.Body()
122+
model.fromMap(dict["body"] as! [String: Any])
123+
self.body = model
124+
}
125+
}
126+
}

tests/fixtures/import/main.dara

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ init(id: [ string ], str: string) {
1414
};
1515
}
1616

17+
async function testClient(): void {
18+
var tmp : Source = null;
19+
tmp = new Source();
20+
}
21+
1722
static function Sample(client: Source): void {
1823
var runtime = new Source.RuntimeObject{};
1924
var request = new Source.Request{

tests/fixtures/optional/Darafile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"scope": "darabonba",
3+
"name": "main",
4+
"version": "0.0.1",
5+
"main": "./main.dara"
6+
}

tests/fixtures/optional/main.dara

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
init(){
2+
}
3+
4+
model Request = {
5+
a?: map[string]string,
6+
b?: string,
7+
c?: integer,
8+
}
9+
10+
model Response = {
11+
header?: map[string]string,
12+
body?: {
13+
name?: string,
14+
len?: int64,
15+
},
16+
}
17+
18+
function isSet(a: any) : boolean {
19+
return true;
20+
}
21+
22+
async function test() : void {
23+
var res : Response = new Response{};
24+
var test : string = `test1${res.body.name}${res.body.len}`;
25+
var req : Request = new Request{};
26+
req.b = res.body.name;
27+
req.b = `test2${res.body.name}${res.body.len}`;
28+
var a = res.body.name;
29+
if (isSet(res.body)) {
30+
var b = res.body.name;
31+
}
32+
}

tests/swift.tests.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,11 @@ describe('Swift Generator', function () {
150150
'Client.swift',
151151
]);
152152
});
153+
154+
it('optional should ok', function () {
155+
check('optional', [
156+
'Sources/Darabonba_Main/Client.swift',
157+
'Sources/Darabonba_Main/Models.swift'
158+
]);
159+
});
153160
});

0 commit comments

Comments
 (0)