Skip to content

Commit 4b9aedf

Browse files
Merge pull request #100 from zuzy/erpcgen-py-union-type
Erpcgen py union type
2 parents 3f8baa7 + 2499b13 commit 4b9aedf

11 files changed

+105
-13
lines changed

erpcgen/src/PythonGenerator.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ void PythonGenerator::generate()
136136

137137
makeIncludesTemplateData();
138138

139-
makeAliasesTemplateData();
139+
//makeAliasesTemplateData();
140140

141141
makeConstTemplateData();
142142

@@ -407,6 +407,7 @@ data_map PythonGenerator::makeGroupSymbolsTemplateData(Group *group)
407407

408408
data_list structs;
409409
data_list unions;
410+
data_list aliases;
410411

411412
Log::info("Group symbols:\n");
412413

@@ -453,6 +454,10 @@ data_map PythonGenerator::makeGroupSymbolsTemplateData(Group *group)
453454
Log::info("%s\n", unionType->getDescription().c_str());
454455

455456
string name = filterName(getOutputName(unionType));
457+
if(name.find('$') != string::npos) {
458+
Log::debug("%s is inside struct!\n", name.c_str());
459+
break;
460+
}
456461

457462
// check if template for this structure has not already been generated
458463
if (names.find(name) == names.end())
@@ -468,13 +473,38 @@ data_map PythonGenerator::makeGroupSymbolsTemplateData(Group *group)
468473
}
469474
break;
470475
}
476+
case DataType::kAliasTypeSymbol:
477+
{
478+
AliasType *aliasType = dynamic_cast<AliasType *>(symbol);
479+
if (aliasType == nullptr)
480+
break;
481+
482+
DataType *elementDataType = aliasType->getElementType();
483+
DataType *trueDataType = elementDataType->getTrueDataType();
484+
// Only generate aliases for enums, unions and structs in Python.
485+
if (!(trueDataType->isEnum() || trueDataType->isUnion() || trueDataType->isStruct()))
486+
break;
487+
488+
string realType = getOutputName(aliasType);
489+
Log::debug("%s\n", realType.c_str());
490+
491+
info["name"] = filterName(realType);
492+
info["elementType"] = getTypeInfo(elementDataType);
493+
info["trueType"] = getTypeInfo(trueDataType);
494+
495+
setTemplateComments(aliasType, info);
496+
497+
aliases.push_back(info);
498+
break;
499+
}
471500
default:
472501
break;
473502
}
474503
}
475504

476505
symbolsTemplate["structs"] = structs;
477506
symbolsTemplate["unions"] = unions;
507+
symbolsTemplate["aliases"] = aliases;
478508

479509
return symbolsTemplate;
480510
}

erpcgen/src/templates/c_client_source.template

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,6 @@ static {$callbackType.prototype}
164164
{
165165
{% if fn.isCallback %}
166166
{% if fn.returnValue.type.isNotVoid %}return {% endif %}{$fn.callbackFName}(k{$iface.name}_service_id, k{$iface.name}_{$fn.name}_id{% for param in fn.parameters %}, {$param.name}{% endfor %});
167-
{% if fn.returnValue.type.isNotVoid %}return;
168-
{% endif %}
169167
{% else -- fn.isCallback >%}
170168
{$ clientShimCode(fn, "k"& iface.name & "_service_id", "k" & iface.name & "_" & fn.name & "_id") >}
171169
{% endif -- fn.isCallback >%}

erpcgen/src/templates/py_client.template

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ class {$iface.name}Client(interface.I{$iface.name}):
6161
{% if p.type.type == 'union' %}
6262
{$p.name}._write(codec, {$p.discriminator})
6363
{% else%}
64+
{% if p.direction == "inout" %}
65+
{$encodeValue(p.type, p.name & ".value", "codec", " ", 0)}
66+
{% else %}
6467
{$encodeValue(p.type, p.name, "codec", " ", 0)}
68+
{% endif -- p.direction %}
6569
{% endif -- isUnion %}
6670
{% endif -- isNullable %}
6771
{% endfor -- inParams %}

erpcgen/src/templates/py_coders.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
{% else %}
1212
{% set self = "self." %}
1313
{% endif %}
14-
codec.start_write_union({$self}discriminator)
14+
codec.start_write_union({$self}{$info.discriminatorName})
1515
{# unions are always within structs, so we have self available #}
1616
{% set isFirst = true %}
1717
{% set hasNonVoidCase = false %}

erpcgen/src/templates/py_common.template

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,13 @@ class {$s.name}(object):
5858
{% endfor -- union cases %}
5959

6060
{% endfor -- members %}
61-
def __init__(self{% for m in s.members if not m.lengthForMember %}, {$m.name}=None{% endfor %}):
61+
def __init__(self{% for m in s.members if ((not m.lengthForMember) && m.type.type != 'union') %}, {$m.name}=None{% endfor %}):
6262
{% for m in s.members if not m.lengthForMember %}
63+
{% if (m.type.type == 'union' && m.type.isNonEncapsulatedUnion == false) %}
64+
self.{$m.name} = self.{$m.name}_union # {$prettyTypeName(m.name, m.type)}
65+
{% else %}
6366
self.{$m.name} = {$m.name} # {$prettyTypeName(m.name, m.type)}
67+
{% endif -- union type %}
6468
{% endfor -- members %}
6569

6670
{# create read-only properties for @length counts #}
@@ -146,10 +150,10 @@ class {$u.name}(object):
146150

147151
{% endfor -- group.symbolsMap.unions %}
148152
{% endif -- not empty(group.symbolsMap.unions) %}
153+
{% if not empty(group.symbolsMap.aliases) %}
149154

150-
{% if aliases %}
151155
# Type aliases
152-
{% for a in aliases %}
156+
{% for a in group.symbolsMap.aliases %}
153157
{$a.name} = {$a.elementType.name}
154-
{% endfor -- aliases %}
155-
{% endif -- aliases %}
158+
{% endfor -- group.symbolsMap.aliases %}
159+
{% endif -- not empty(group.symbolsMap.aliases) %}

erpcgen/test/test_length_py.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ test/interface.py:
4848
- def bar(self, l)
4949
test/client.py:
5050
- def bar(self, l)
51-
- start_write_list(len(l))
51+
- start_write_list(len(l.value))
5252
- perform_request
5353
- start_read_list
5454
- not: codec.read_int32()
@@ -78,9 +78,14 @@ idl: |
7878
lang: py
7979
test/client.py:
8080
- def bar(self, v)
81+
- if: dir in ('in', )
82+
then:
83+
- write_{type}(v)
84+
- if: dir in ('inout', )
85+
then:
86+
- write_{type}(v.value)
8187
- if: dir in ('in', 'inout')
8288
then:
83-
- write_{type}(v)
8489
- not: write_int32(len(v))
8590
- perform_request
8691
- if: dir in ('inout',)

erpcgen/test/test_name_py.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ test/common.py:
4141
- if self.d is None
4242
- codec.write_int32(self.d)
4343
- self.d
44-
- type = StructName
4544

4645
test/client.py:
4746
- def function(self, m)

erpcgen/test/test_union_py.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ lang: py
168168
test/common.py:
169169
- not: class unionVariable_union(object)
170170
- "class structType(object):"
171-
- def __init__(self, discriminator=None, unionVariable=None)
171+
- def __init__(self, discriminator=None)
172172
- self.discriminator = discriminator # fruitType
173173
- self.unionVariable = unionVariable # unionType
174174
- self.unionVariable, self.discriminator = common.unionType()._read(codec)

test/test_callbacks/test_callbacks.erpc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import "../common/unit_test_common.erpc"
1212

1313
oneway callback1_t(int32 a, int32 b)
1414
oneway callback2_t(int32, int32)
15+
callback3_t(int32 arg1, int32 arg2) -> int32
1516

1617
@include("test_core1.h")
1718
@group("core0")
@@ -21,9 +22,17 @@ interface ClientCore0Services
2122

2223
myFun2(callback2_t pCallback2_in, out callback2_t pCallback2_out) -> void
2324

25+
myFun3(callback3_t callback, in int32 arg1, in int32 arg2) -> int32
26+
27+
callback3_t my_add;
28+
callback3_t my_sub;
29+
callback3_t my_mul;
30+
callback3_t my_div;
31+
2432
@include("callbacks1.h")
2533
callback1_t callback1a;
2634

35+
2736
@include("callbacks1.h")
2837
callback1_t callback1b(param1, param2);
2938
}

test/test_callbacks/test_callbacks_client_impl.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,18 @@ TEST(test_callbacks, In_Out_withoutTable)
3838

3939
EXPECT_TRUE(callback2 == *pCallback2_out);
4040
}
41+
42+
TEST(test_callbacks, Common_Callback)
43+
{
44+
callback3_t callback = my_add;
45+
EXPECT_TRUE(12 == myFun3(callback, 9, 3));
46+
47+
callback = my_sub;
48+
EXPECT_TRUE(6 == myFun3(callback, 9, 3));
49+
50+
callback = my_mul;
51+
EXPECT_TRUE(27 == myFun3(callback, 9, 3));
52+
53+
callback = my_div;
54+
EXPECT_TRUE(3 == myFun3(callback, 9, 3));
55+
}

0 commit comments

Comments
 (0)