Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc: Introduce BaseResult to get rid of OpenStruct #3122

Merged
merged 1 commit into from
Feb 3, 2025

Conversation

vincent-pochet
Copy link
Collaborator

@vincent-pochet vincent-pochet commented Jan 30, 2025

Context

As a requirement to enable YJIT compiler for Ruby, we need to get rid of OpenStruct as it leads to a huge memory consumption.

At the same time, we are lacking visibility on what properties are attached to result objects returned by services.

Description

This PR introduces a new BaseResult result object, that should be used a a base in every services to define the attributes of a Result:

module SomeModule
  class SomeService < BaseService
    Result = BaseResult[:attr_name1, :attr_name2]

To keep compatibility until all services are migrated to this approach, the BaseService is still relying on a LegacyResult using OpenStruct as a parent class.

@julienbourdeau
Copy link
Contributor

julienbourdeau commented Jan 31, 2025

Awesome! Being able to see what's expected in the result will be a tremendous help.

Just thinking out loud about the syntax. How do you feel about using ActiveModel::Attributes?

module AddOns
  class ApplyTaxesService < BaseService
    class Result < BaseResult
      attribute :add_on
      attribute :something, default: true
    end

    def call
      # 
    end
  end
end

def BaseResult
  include ActiveModel::Attributes

  # ...
end

@vincent-pochet
Copy link
Collaborator Author

Awesome! Being able to see what's expected in the result will be a tremendous help.

Just thinking out loud about the syntax. How do you feel about using ActiveModel::Attributes?

module AddOns
  class ApplyTaxesService < BaseService
    class Result < BaseResult
      attribute :add_on
      attribute :something, default: true
    end

    def call
      # 
    end
  end
end

def BaseResult
  include ActiveModel::Attributes

  # ...
end

I like the idea, but I see to problems with that:

  1. It leads to a "heavier" definitions, and I would prefer keeping a short syntax (more a matter or personal preference here 🙂 )
  2. ActiveModel::Attributes is built for basic, but most of our return objects are model instances or array of model instances, so I'm not sure it would bring much value (+ we have no need for validation as it must be handled at service level)

@vincent-pochet vincent-pochet force-pushed the misc-result-objects branch 2 times, most recently from e5600ca to 642abc8 Compare February 3, 2025 10:39
@vincent-pochet vincent-pochet marked this pull request as ready for review February 3, 2025 10:39
Copy link
Contributor

@julienbourdeau julienbourdeau left a comment

Choose a reason for hiding this comment

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

🙌 🙌 🙌

@vincent-pochet vincent-pochet merged commit e34dc5c into main Feb 3, 2025
6 checks passed
@vincent-pochet vincent-pochet deleted the misc-result-objects branch February 3, 2025 15:36
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.

5 participants