Skip to content

Erb template - #40

Open
Rezenders wants to merge 7 commits into
rock-control:masterfrom
tidewise:erb_template
Open

Erb template#40
Rezenders wants to merge 7 commits into
rock-control:masterfrom
tidewise:erb_template

Conversation

@Rezenders

@Rezenders Rezenders commented Aug 6, 2026

Copy link
Copy Markdown

Add SDF::Loader and SDF::ERBLoader and use loader in load

Comment thread lib/sdf/erb.rb
Comment on lines +8 to +9
module_function

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid this. It's very easy to miss it when the module gets bigger.

There are two better patterns:

  1. define each method with self., e.g. def self.parse_erb_as_str
  2. define the module normally and do an extend self at the very end

The latter is also hard to miss, but the module in itself behaves normally (one can include it elsewhere and it works). I prefer (1) when the purpose of the module is really to be a namespace.

Comment thread lib/sdf/erb.rb
@@ -0,0 +1,32 @@
# frozen_string_literal: true

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please split code in modules only when you see that things get too big, or that you need the functionality in multiple places. Not when you think it/you will.

The functionality of erb.rb should really be straight into ERBLoader#parse_sdf_document

@Rezenders Rezenders Aug 7, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will include it in the ERBLoader class.

Comment thread lib/sdf/erb_loader.rb
@erb_args = erb_args
end

def parse_sdf_document(sdf_file)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should make a difference between erb and non-erb files, that is parse ERB only when the extension is .erb

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I told him to do it like this. If you mean for having an explicit error when a non-erb file is given to an ERBLoader, I feel this is overkill and it would painful in the Robot level to constantly juggle between loaders when the model file changes (I dont think you mean this, just getting it out there).

In the case you want to split the functionality between parse_sdf_document, or do a plain load directly as its done nowadays when the file does not have a .erb, my concern would be the flakiness of someone defining a model.sdf that IS an file with ERB variables on it without realizing, and then the syntax error when interpreting the SDF would be probably very noisy.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @jhonasiv on this one, but in this case I would simply remove the base SDF::Loader class and keep only the ERBLoader.

In case we enforce the files to end with .erb then I suggest we keep both loaders and make them only handle their specific file extension

But anyway, I don't have a strong opinion on this, so I would happily go with any

Comment thread lib/sdf/erb_loader.rb

module SDF
# class to load SDF and ERB templated SDF files
class ERBLoader < Loader

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm missing the purpose or advantage of having separate loader and ERBLoader classes. This stuff is so simple, why not a single class ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember correctly, @jhonasiv requested me to do it so we could enforce that when the SDF::Loader is configured only .sdf files are loaded

Comment thread lib/sdf/sdf_loader.rb
Comment on lines +41 to +44
raise Errno::ENOENT,
"Cannot find '#{file_name}' in '#{dir_path}'." \
"You probably want to update the GAZEBO_MODEL_PATH " \
"environment variable, or set SDF.model_path explicitly."

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class knows nothing about GAZEBO_MODEL_PATH or SDF.model_path. It is given an already resolved path as argument.

You should assume that the argument exists and let ENOENT propagate. The levels that do resolution based on GAZEBO_MODEL_PATH should error out if a model does not exist.

Comment thread lib/sdf/sdf_loader.rb
"#{sdf_file} can be parsed as an XML file, but it " \
"does not have a root"
end
return if %w[sdf gazebo].include?(sdf.root.name)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gazebo as a root ? Is that valid SDF ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so: https://sdformat.org/spec/1.12/sdf/
I included it because it was in the original code and I didn't know whether there was a reason for it.
I will remove it then

Comment thread lib/sdf/sdf_loader.rb
"Cannot load #{sdf_file}: #{e.message}"
end

REXML::Document.new

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that it was in the original code, but ...

I think the value of having an error message that says "it loads as XML but it has no root" rather than "this file has no root" does not warrant the complexity of having a codepath dedicated for it. Please simplify it.

Comment thread lib/sdf/xml.rb
Comment on lines +2 to +4
require_relative "exceptions"
require_relative "sdf_loader"
require_relative "erb_loader"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not use require_relative.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honest question, why is that? I thought require_relative was less error prune

Comment thread lib/sdf/sdf_loader.rb
@@ -0,0 +1,64 @@
# frozen_string_literal: true

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SDF::Loader lives in sdf/loader.rb not sdf/sdf_loader.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants