ash-project / ash_json_api

The JSON:API extension for the Ash Framework
https://hexdocs.pm/ash_json_api
MIT License
63 stars 42 forks source link

Support member name transformers #17

Open zachdaniel opened 4 years ago

zachdaniel commented 4 years ago

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.

mario-mazo commented 4 years ago

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 commented 4 years ago

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

I will try to do this

zachdaniel commented 4 years ago

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.

mario-mazo commented 4 years ago

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