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
Add ParameterizedType and Embed #3339
Closed
TheFirstAvenger
wants to merge
1
commit into
elixir-ecto:master
from
TheFirstAvenger:mb-parameterized-types-and-enum
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -602,53 +602,53 @@ defmodule Ecto do | |
end | ||
end | ||
|
||
@doc """ | ||
Loads previously dumped `data` in the given `format` into a schema. | ||
|
||
The first argument can be a an embedded schema module, or a map (of types) and | ||
determines the return value: a struct or a map, respectively. | ||
|
||
The second argument `data` specifies fields and values that are to be loaded. | ||
It can be a map, a keyword list, or a `{fields, values}` tuple. Fields can be | ||
atoms or strings. | ||
|
||
The third argument `format` is the format the data has been dumped as. For | ||
example, databases may dump embedded to `:json`, this function allows such | ||
dumped data to be put back into the schemas. | ||
|
||
Fields that are not present in the schema (or `types` map) are ignored. | ||
If any of the values has invalid type, an error is raised. | ||
|
||
Note that if you want to load data into a non-embedded schema that was | ||
directly persisted into a given repository, then use `c:Ecto.Repo.load/2`. | ||
|
||
## Examples | ||
|
||
iex> result = Ecto.Adapters.SQL.query!(MyRepo, "SELECT users.settings FROM users", []) | ||
iex> Enum.map(result.rows, fn [settings] -> Ecto.embedded_load(Setting, Jason.decode!(settings), :json) end) | ||
[%Setting{...}, ...] | ||
""" | ||
@spec embedded_load( | ||
module_or_map :: module | map(), | ||
data :: map(), | ||
format :: atom() | ||
) :: Ecto.Schema.t() | map() | ||
def embedded_load(schema_or_types, data, format) do | ||
Ecto.Schema.Loader.unsafe_load(schema_or_types, data, &Ecto.Type.embedded_load(&1, &2, format)) | ||
end | ||
|
||
@doc """ | ||
Dumps the given struct defined by an embedded schema. | ||
|
||
This converts the given embedded schema to a map to be serialized | ||
with the given format. For example: | ||
|
||
iex> Ecto.embedded_dump(%Post{}, :json) | ||
%{title: "hello"} | ||
|
||
""" | ||
@spec embedded_dump(Ecto.Schema.t(), format :: atom()) :: map() | ||
def embedded_dump(%schema{} = data, format) do | ||
Ecto.Schema.Loader.safe_dump(data, schema.__schema__(:dump), &Ecto.Type.embedded_dump(&1, &2, format)) | ||
end | ||
# @doc """ | ||
# Loads previously dumped `data` in the given `format` into a schema. | ||
|
||
# The first argument can be a an embedded schema module, or a map (of types) and | ||
# determines the return value: a struct or a map, respectively. | ||
|
||
# The second argument `data` specifies fields and values that are to be loaded. | ||
# It can be a map, a keyword list, or a `{fields, values}` tuple. Fields can be | ||
# atoms or strings. | ||
|
||
# The third argument `format` is the format the data has been dumped as. For | ||
# example, databases may dump embedded to `:json`, this function allows such | ||
# dumped data to be put back into the schemas. | ||
|
||
# Fields that are not present in the schema (or `types` map) are ignored. | ||
# If any of the values has invalid type, an error is raised. | ||
|
||
# Note that if you want to load data into a non-embedded schema that was | ||
# directly persisted into a given repository, then use `c:Ecto.Repo.load/2`. | ||
|
||
# ## Examples | ||
|
||
# iex> result = Ecto.Adapters.SQL.query!(MyRepo, "SELECT users.settings FROM users", []) | ||
# iex> Enum.map(result.rows, fn [settings] -> Ecto.embedded_load(Setting, Jason.decode!(settings), :json) end) | ||
# [%Setting{...}, ...] | ||
# """ | ||
# @spec embedded_load( | ||
# module_or_map :: module | map(), | ||
# data :: map(), | ||
# format :: atom() | ||
# ) :: Ecto.Schema.t() | map() | ||
# def embedded_load(schema_or_types, data, format) do | ||
# Ecto.Schema.Loader.unsafe_load(schema_or_types, data, &Ecto.Type.embedded_load(&1, &2, format)) | ||
# end | ||
|
||
# @doc """ | ||
# Dumps the given struct defined by an embedded schema. | ||
|
||
# This converts the given embedded schema to a map to be serialized | ||
# with the given format. For example: | ||
|
||
# iex> Ecto.embedded_dump(%Post{}, :json) | ||
# %{title: "hello"} | ||
|
||
# """ | ||
# @spec embedded_dump(Ecto.Schema.t(), format :: atom()) :: map() | ||
# def embedded_dump(%schema{} = data, format) do | ||
# Ecto.Schema.Loader.safe_dump(data, schema.__schema__(:dump), &Ecto.Type.embedded_dump(&1, &2, format)) | ||
# end | ||
Comment on lines
+605
to
+653
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a breaking change … We can add |
||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not be a part of this
PR
…There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will be removed before merging