Skip to content

Commit 1fe13a9

Browse files
committed
Fix bug in renaming conflicting message types
Closes #8
1 parent 3c634d1 commit 1fe13a9

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

src/gentypes.jl

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ end
113113
function _usepkg(package::ASCIIString, ismsg::Bool, names::ASCIIString...)
114114
global _rospy_imports
115115
if ! haskey(_rospy_imports, package)
116-
@debug("Creating new package: ",package,".", ismsg ? "msg" : "srv")
116+
@debug("Creating new package: ",package,".", ismsg ? "msg" : "srv")
117117
_rospy_imports[package] = ROSPackage(package)
118118
end
119119
rospypkg = _rospy_imports[package]
@@ -339,9 +339,7 @@ end
339339
function _exportexpr(mod::ROSMsgModule)
340340
exportexpr = Expr(:export)
341341
for m in mod.members
342-
push!(exportexpr.args, _nameconflicts(m) ?
343-
symbol(string(m,"Msg")) :
344-
symbol(m))
342+
push!(exportexpr.args, symbol(_jl_safe_name(m,"Msg")))
345343
end
346344
exportexpr
347345
end
@@ -366,11 +364,6 @@ function buildtype(mod::ROSMsgModule, typename::ASCIIString)
366364
memtypes = pyobj[:_slot_types]
367365
members = collect(zip(memnames, memtypes))
368366

369-
#Some ROS message names conflict with Julia built-in types
370-
#Append 'Msg' to the type name to resolve
371-
if _nameconflicts(typename)
372-
fulltypestr = fulltypestr * "Msg"
373-
end
374367
typecode(fulltypestr, :MsgT, members)
375368
end
376369

@@ -408,49 +401,55 @@ function buildtype(mod::ROSSrvModule, typename::ASCIIString)
408401
end
409402

410403
#Create the core generated expressions for a native Julia message type that has
411-
#data fields and interchanges with a python counterpart:
404+
#data fields and interchanges with a python counterpart:
412405
# (1) the 'type ... end' block
413406
# (2) Default outer constructer with no arguments
414407
# (3) convert(PyObject, ...)
415408
# (4) convert(..., o::PyObject)
416409
function typecode(rosname::ASCIIString, super::Symbol, members::Vector)
417410
tname = _splittypestr(rosname)[2]
418411
@debug("Type: ", tname)
419-
tsym = symbol(tname)
412+
413+
#generated code should not conflict with julia built-ins
414+
#some messages need renaming
415+
suffix = if super == :MsgT; "Msg"
416+
elseif super == :SrvT; "Srv"
417+
else; "ROS" end
418+
jlsym = symbol(_jl_safe_name(tname,suffix))
420419

421420
exprs = Expr[]
422421
#First the empty expressions
423422
#(1) Type declaration
424423
push!(exprs, :(
425-
type $tsym <: $super
424+
type $jlsym <: $super
426425
#Generated code here
427426
end
428427
))
429428
#(2) Default constructor, but only if the type has members
430429
if length(members) > 0
431430
push!(exprs, :(
432-
function $tsym()
433-
$tsym() #Generated code inside parens here
431+
function $jlsym()
432+
$jlsym() #Generated code inside parens here
434433
end
435434
))
436435
else
437436
push!(exprs, :())
438437
end
439438
#(3) Convert to PyObject
440439
push!(exprs, :(
441-
function convert(::Type{PyObject}, o::$tsym)
440+
function convert(::Type{PyObject}, o::$jlsym)
442441
py = pycall(RobotOS._rospy_objects[$rosname], PyObject)
443442
#Generated code here
444443
py
445444
end
446445
))
447446
#(4) Convert from PyObject
448447
push!(exprs, :(
449-
function convert(jlt::Type{$tsym}, o::PyObject)
448+
function convert(jlt::Type{$jlsym}, o::PyObject)
450449
if convert(ASCIIString, o["_type"]) != _typerepr(jlt)
451450
throw(InexactError())
452451
end
453-
jl = $tsym()
452+
jl = $jlsym()
454453
#Generated code here
455454
jl
456455
end
@@ -462,7 +461,7 @@ function typecode(rosname::ASCIIString, super::Symbol, members::Vector)
462461
_addtypemember!(exprs, namestr, typ)
463462
@debug_subindent
464463
end
465-
push!(exprs, :(_typerepr(::Type{$tsym}) = $rosname))
464+
push!(exprs, :(_typerepr(::Type{$jlsym}) = $rosname))
466465
exprs
467466
end
468467

@@ -611,6 +610,10 @@ function _get_rospy_class(typ::DataType)
611610
rospycls
612611
end
613612

613+
_jl_safe_name(name::AbstractString, suffix) = _nameconflicts(name) ?
614+
string(name,suffix) :
615+
name
616+
614617
#Check if the type name conflicts with a Julia builtin. Currently this is only
615618
#some of the messages from the std_msgs.msg package
616619
_nameconflicts(typename::ASCIIString) = isdefined(Base, symbol(typename))

test/typegeneration.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ path2 = convert(nav_msgs.msg.Path, pypath)
6969
emptymsg = std_msgs.msg.Empty()
7070
@test length(fieldnames(emptymsg)) == 0
7171

72-
#Issue #7 - Renaming conflicting message types
72+
#Issue #7/8 - Renaming conflicting message types
7373
@test isdefined(std_msgs.msg, :Float64Msg)
7474
@test isdefined(std_msgs.msg, :StringMsg)
75+
@test Publisher{std_msgs.msg.Float64Msg}("x", queue_size=10) != nothing
76+
@test Subscriber{std_msgs.msg.Float64Msg}("x", x->x, queue_size=10) != nothing

0 commit comments

Comments
 (0)