ash_json_api icon indicating copy to clipboard operation
ash_json_api copied to clipboard

Support member name transformers

Open zachdaniel opened this issue 5 years ago • 4 comments

Currently, everything is snake_case but we want to let users configure a name transformer at the API level, to turn everything into kebab case or camelcase.

zachdaniel avatar Jun 07 '20 16:06 zachdaniel

Steps: step 1 will be adding an optional configuration to this schema https://github.com/ash-project/ash_json_api/blob/master/lib/ash_json_api/api/api.ex called member_name_transformer

step 2 then you need to make a behaviour module called AshJsonApi.MemberNameTransformer that behaviour should have two callbacks. I think the first one should be called transform_in(string) and the other should be transform_out(string) This would be an example of a transformer we'd probably provide by default

defmodule AshJsonApi.MemberNameTransformer.CamelCase do
  @behaviour AshJsonAPi.MemberNameTransformer

  def transform_out(snake_case) do
    convert_snake_case_to_camelcase(snake_case)
  end

  def transform_in(camel_case) do
    convert_camel_case_to_snake_case(camel_case)
  end
end

I mean, also maybe member_name_transformer is a bad name, it could just be like...key_transformer or something, feel free to propose less confusing names

mario-mazo avatar Sep 26 '20 13:09 mario-mazo

@zachdaniel I added our discussion here, so maybe other people can benefit.

I will try to do this

mario-mazo avatar Sep 26 '20 13:09 mario-mazo

Good call putting the chat here 👍 The final step of this will be to use the member name transformer throughout the application. Specifically, in AshJsonApi.Request, where we parse the input, AshJsonApi.Serializer where we form our responses, and AshJsonApi.JsonSchema which is used as validation and documentation.

zachdaniel avatar Sep 26 '20 14:09 zachdaniel

I should be able to tackle this by in the next 3 weeks. If somebody can jump in soon just ping me!

mario-mazo avatar Sep 28 '20 14:09 mario-mazo