So quick practical question: Why not use TSort (http://www.ruby-doc.org/stdlib-2.0/libdoc/tsort/rdoc/TSort.html) to sort the messages by the message types of their child fields?  You'd emit less code to the user that way.