flipp-oss / deimos

Framework to work with Kafka, Avro and ActiveRecord
Other
59 stars 22 forks source link

CHANGE: Nest or flatten sub-schemas in generated classes #154

Closed dorner closed 2 years ago

dorner commented 2 years ago

Pull Request Template

Description

This change is one approach to #153 . This doesn't allow for changing namespaces but will fix issues around nested schemas.

There were a number of problems with schema class generation:

  1. The default was to generate classes in a schema_classes folder - but the module containing the classes is Schemas. This won't work with zeitwerk-powered Rails.
  2. Nested schemas would live in the same file as the parent schema, but would still be under the main Schemas namespace. This not only makes the classes impossible to find by Zeitwerk, but also would clobber them if you happened to have two identically-named sub-schemas in different top-level schemas.
  3. The filenames generated end in s when zeitwerk expects them to be singularized.

The solution is to add a new nest_schema_classes configuration option. When set to true (the default), it will nest the sub-schema classes inside their parent schema classes. This ensures that no clobbering takes place.

When set to false, the classes will be flattened into a single directory. This essentially leaves the code use as it was before, although the generated sub-classes will now be put in their own file so they can be found, rather than added to the same file as the parent class.

I also added RSpec snapshot testing to make this test easier to deal with. Also updated fig_tree to have a with_config method to make testing more succinct.

Type of change

How Has This Been Tested?

Checklist:

cc: @iMacTia @DeeChau

dorner commented 2 years ago

@DeeChau thanks for the feedback, fixed!

@iMacTia I actually wasn't planning on bumping up the major version for this. Yes it's technically a breaking change, but it's a change in code that generates other code. If you don't regenerate your code, this won't affect what you have (and if you do, there's a very simple config change that will give you your old way of handling it back). Unfortunately we can't get away from Ruby 2.3 for the foreseeable future - at least a year if not more.

Zeitwerk isn't compatible with older Rubies, but the changes actually aren't incompatible with the old autoloader either - this just makes it cleaner and has it work with both loaders.