支持map_any类型转换

Signed-off-by: liuycag <liuycag@digitalchina.com>
This commit is contained in:
liuycag 2022-10-12 15:49:12 +08:00
parent e50339a6e5
commit 4c7c47eed3
5 changed files with 66 additions and 1 deletions

View File

@ -126,6 +126,11 @@ function mapTypeString(type, name) {
else if (mapType[1].substring(0, 12) == "NUMBER_TYPE_") {
mapTypeString = "std::string,%s".format(mapType[1])
}
else if (mapType[1].substring(0, 12) == "any") {
mapTypeString = `std::string,std::any`.format(mapType[1])
return `\n std::map<%s> %s;
std::string %s_type;`.format(mapTypeString, name, name)
}
else if (InterfaceList.getValue(mapType[1])) mapTypeString = "std::string,%s".format(mapType[1])
}
if (mapType[2] != undefined) {

View File

@ -368,6 +368,7 @@ function paramGenerateMap(funcValue, param, p) {
if (mapType[1] == "string") { mapTypeString = "std::string" }
else if (mapType[1].substring(0, 12) == "NUMBER_TYPE_") { mapTypeString = mapType[1] }
else if (mapType[1] == "boolean") { mapTypeString = "bool" }
else if (mapType[1] == "any") { mapTypeString = "std::any" }
else { mapTypeString = mapType[1] }
}
else if (mapType[2] != undefined) {
@ -380,10 +381,21 @@ function paramGenerateMap(funcValue, param, p) {
else if (mapType[3].substring(0, 12) == "NUMBER_TYPE_") { mapTypeString = "std::vector<"+mapType[3]+">" }
else if (mapType[3] == "boolean") { mapTypeString = "std::vector<bool>" }
}
paramGenerateMap2(funcValue, param, p, mapType, mapTypeString, name)
}
function paramGenerateMap2(funcValue, param, p, mapType, mapTypeString, name) {
let inParamName = funcValue.optional ? "(*vio->in" + p + ")" : "vio->in" + p
let modifiers = funcValue.optional ? "*" : "&"
param.valueIn += funcValue.optional ? "\n std::map<std::string,%s>* in%d = nullptr;".format(mapTypeString, p)
if (mapType[1] == "any") {
param.valueIn += funcValue.optional ? `\n std::map<std::string,%s>* in%d = nullptr;
std::string in%d_type;`.format(mapTypeString, p, p)
: `\n std::map<std::string,%s> in%d;
std::string in%d_type;`.format(mapTypeString, p, p)
} else {
param.valueIn += funcValue.optional ? "\n std::map<std::string,%s>* in%d = nullptr;".format(mapTypeString, p)
: "\n std::map<std::string,%s> in%d;".format(mapTypeString, p)
}
param.valueCheckout += getValueCheckout(funcValue, param, inParamName, p,
"std::map<std::string,%s>".format(mapTypeString))
param.valueFill += "%svio->in%d".format(param.valueFill.length > 0 ? ", " : "", p)
@ -471,6 +483,7 @@ function mapValue(mapType, napiVn, dest, lt) {
if (mapType[1] == "string") { mapTypeString = "std::string" }
else if (mapType[1].substring(0, 12) == "NUMBER_TYPE_") { mapTypeString = mapType[1] }
else if (mapType[1] == "boolean") { mapTypeString = "bool" }
else if (mapType[1] == "any") { mapTypeString = "std::any" }
else if (mapType[1] != null) { mapTypeString = mapType[1] }
let mapTemplete = mapValueTemplete.format(napiVn, mapTypeString, dest)
mapTemplete = mapTemplete.replaceAll("[replace_lt]", lt)
@ -493,6 +506,15 @@ function mapValue(mapType, napiVn, dest, lt) {
tt%d = pxt->SwapJs2CBool(pxt->GetMapElementValue(%s,tt%d.c_str()));`
.format(napiVn, lt, lt, lt + 1, napiVn, lt))
}
if (mapTypeString == "std::any") {
mapTemplete = mapTemplete.replaceAll("[replace_swap]",
`pxt->SwapJs2CUtf8(pxt->GetMapElementName(%s,i%d), tt%d);
if (i%d == 0){
%s_type = pxt->GetAnyType(pxt->GetMapElementValue(%s,tt%d.c_str()));
}
pxt->SetAnyValue(%s_type, pxt->GetMapElementValue(%s,tt%d.c_str()), tt%d);`
.format(napiVn, lt, lt, lt, dest, napiVn, lt, dest, napiVn, lt, lt + 1))
}
else if (InterfaceList.getValue(mapTypeString)) {
mapTemplete = mapInterface(mapTypeString, mapTemplete, napiVn, lt)
}

View File

@ -278,6 +278,9 @@ function mapTempleteValue(mapType, tnvdef, lt, value, tnv) {
} else if (mapType[1].substring(0, 12) == "NUMBER_TYPE_") {
ret = tnvdef.replaceAll("[calc_out]", `tnv%d = (i -> first).c_str();
tnv%d = NUMBER_C_2_JS(pxt,i->second);`.format(lt, lt + 1))
} else if (mapType[1] == "any") {
ret = tnvdef.replaceAll("[calc_out]", `tnv%d = (i -> first).c_str();
pxt->GetAnyValue(%s_type, tnv%d, i->second);`.format(lt, value, lt + 1))
}
else if (InterfaceList.getValue(mapType[1])) {
ret = mapInterface(value, lt, tnv, mapType)
@ -363,6 +366,7 @@ function returnGenerateMap(returnInfo, param) {
if (mapType[1] == "string") { mapTypeString = "std::string" }
else if (mapType[1].substring(0, 12) == "NUMBER_TYPE_") { mapTypeString = mapType[1] }
else if (mapType[1] == "boolean") { mapTypeString = "bool" }
else if (mapType[1] == "any") { mapTypeString = "std::any" }
else { mapTypeString = mapType[1] }
}
else if (mapType[2] != undefined) {

View File

@ -37,6 +37,7 @@ declare namespace napitest {
fun8(v: Map<string,Map<string,string>>): number;
fun9(v: {[key: string]: Human}): number;
fun10(v: Map<string,Human>): number;
fun11(v: Map<string,any>): number;
}
function fun13(v: {[key: string]: string[]}): number;

View File

@ -83,4 +83,37 @@ describe('Map', function () {
"jane":{"age":666,"name":"jane","isTrue":false}});
assert.strictEqual(ret, 0);
});
});
describe('Map_Any', function () {
it('test TestClass2 fun11', function () {
let tc2 = new TestClass2();
let ret = tc2.fun11({"age":"ageValue","name":"nameValue"});
assert.strictEqual(ret, 0);
});
it('test TestClass2 fun11', function () {
let tc2 = new TestClass2();
let ret = tc2.fun11({"age":1234,"name":5678});
assert.strictEqual(ret, 0);
});
it('test TestClass2 fun11', function () {
let tc2 = new TestClass2();
let ret = tc2.fun11({"age":true,"name":false});
assert.strictEqual(ret, 0);
});
it('test TestClass2 fun11', function () {
let tc2 = new TestClass2();
let ret = tc2.fun11({"age":[1,23,456],"name":[7,89,101112]});
assert.strictEqual(ret, 0);
});
it('test TestClass2 fun11', function () {
let tc2 = new TestClass2();
let ret = tc2.fun11({"peter":{"age":"ageValue","name":"nameValue"},
"jane":{"age":"ageValue","name":"nameValue"}});
assert.strictEqual(ret, 0);
});
});