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

Using functions in the fields list #131

Open
justincy opened this issue Aug 7, 2023 · 2 comments
Open

Using functions in the fields list #131

justincy opened this issue Aug 7, 2023 · 2 comments

Comments

@justincy
Copy link
Contributor

justincy commented Aug 7, 2023

Something like this is possible:

Solicit.Repsponse.ok(conn, struct, [:one, :another, something: Module.function/1])

But it's weird that the full struct is passed in to that function:

solicit/lib/response.ex

Lines 542 to 543 in fdbaeeb

{field, generate} when is_function(generate, 1) ->
{field, generate.(struct)}

Should we instead be passing Map.get(struct, field)? Seems more appropriate.

@matthew-kerle
Copy link
Collaborator

Something like this is possible:


Solicit.Repsponse.ok(conn, struct, [:one, :another, something: Module.function/1])

But it's weird that the full struct is passed in to that function:

solicit/lib/response.ex

Lines 542 to 543 in fdbaeeb

{field, generate} when is_function(generate, 1) ->
{field, generate.(struct)}

Should we instead be passing Map.get(struct, field)? Seems more appropriate.

It seems like there's a good probability for one field like "status", you might need other fields from the strict to derive or calculate beyond just a single field.

@bjyoungblood
Copy link
Member

I could see the case for adding an additional clause like this:

{field, generate} when is_function(generate, 2) ->
  {field, generate.(struct, field)}

In usage, that'd let you reuse the same function for multiple fields in cases like:

def get(conn, _params) do
  Solicit.Response.ok(conn, struct, [:foo, inserted_at: &truncate_datetime/2, updated_at: &truncate_datetime/2])
end

defp truncate_datetime(map, field) do
  case Map.get(map, field) do
    %DateTime{} = v -> DateTime.truncate(v, :millisecond)
    nil -> nil
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants