-
-
Notifications
You must be signed in to change notification settings - Fork 46
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 member name transformers #17
Comments
Steps: step 2 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 |
@zachdaniel I added our discussion here, so maybe other people can benefit. I will try to do this |
Good call putting the chat here 👍 The final step of this will be to use the member name transformer throughout the application. Specifically, in |
I should be able to tackle this by in the next 3 weeks. If somebody can jump in soon just ping me! |
I'd like to take this one, but I have a few questions.
Are transformations computed entirely at compile time? I ask because in my own implementation I created maps at compile time so I wasn't transforming on the fly all the time. It would definitely complicate #3 a bit. |
There are two different things here. The concept of a "member name transformer" would be used for making wholesale transformations of the entire API. Ignoring performance, it would look like everything in the serializer, deserializer, and json schema builder doing something like Ultimately there is some non-trivial things to consider here. For example, inside of an attribute whose type is just
For this reason, I think we should do this all dynamically (with perhaps the exception of member names that appear literally in |
Right. Transformations need to happen at the last possible second and be relevant only the serialization/deserialization layer.
In my own API, where we use camelcase but everything else under the hood are just Ecto schemas using snake case, fields that are maps come in two flavors: plain old maps (like meta data) and nested structs which are mapped. What I do in that case is use an embedded struct and apply my mappings to the embedded resource. I did look at the JSON:API code around Christmas when I was toying with this and was just like, "Well, I would have done this if I'd been given two weeks off rather than a major deadline." Haha. I think the transformer is going to need to take some kind of either options or context as in addition to the resource and name. |
I don't think we should provide the resource or any context at all to the transformer actually. It should be all or nothing. If you use an embedded resource, then we transform its member names. Then nested maps are just "your own problem" to handle mixed casing if you want. |
So it really should just be two functions that take strings and should be inverses of eachother. |
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.The text was updated successfully, but these errors were encountered: