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

Support transformation of values without needing format-specific custom methods #178

Open
HassanAkbar opened this issue Nov 19, 2024 · 4 comments
Assignees

Comments

@HassanAkbar
Copy link
Member

No description provided.

@HassanAkbar HassanAkbar self-assigned this Nov 19, 2024
@ronaldtse ronaldtse changed the title lutaml-model with value procs Support "value procs" Jan 2, 2025
@ronaldtse
Copy link
Contributor

@HassanAkbar can you please elaborate this task? Is the "value proc" for value validation or for processing the input/output values?

@HassanAkbar
Copy link
Member Author

@ronaldtse From what I understand this was for processing the input/output of the values i.e

class Person < Lutaml::Model::Serializable
  attribute :id, :string, value: -> { |id| id.capitalize }
end

p = Person.new({ id: "abc-123" })
puts p.id
#=> "ABC-123"

Now thinking about it I have a question, do we need to support different procs for input and output?

@ronaldtse
Copy link
Contributor

ronaldtse commented Jan 2, 2025

Ah, now I remember. The "value proc" is meant to handle the simple usage of custom methods, where we are simply going to transfer the import/export of values, without needing complex processing of custom methods, which requires the user to handle the different formats.

Maybe it should just be called "value transform"?

We should differentiate the input and output of values at the attribute level. Or should this go into the format-specific level?

Transforming values at the attribute-level

class Person < Lutaml::Model::Serializable
  attribute :id, :string, transform: {
    import: -> { |id| id.capitalize },
    export: -> { |id| id }
  }
end

Transforming values at the format level

class Person < Lutaml::Model::Serializable
  attribute :id, :string
  xml do
    map_element 'Id', to: :id, transform: {
      import: -> { |id| id.capitalize },
      export: -> { |id| id }
    }
  end
end

Question

Which one?

@ronaldtse ronaldtse changed the title Support "value procs" Support transformation of values without needing (format-specific custom methods) Jan 2, 2025
@ronaldtse ronaldtse changed the title Support transformation of values without needing (format-specific custom methods) Support transformation of values without needing format-specific custom methods Jan 2, 2025
@HassanAkbar
Copy link
Member Author

@ronaldtse I think if we want the user not to handle multiple format, then adding it to the attribute level seems more reasonable.

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

No branches or pull requests

2 participants