Skip to content

Commit 719e11f

Browse files
committed
Add docs in livebook for extrenal derives, field, conditional etc
1 parent f377033 commit 719e11f

File tree

1 file changed

+84
-1
lines changed

1 file changed

+84
-1
lines changed

guidance/guarded-struct.livemd

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,14 @@ defmodule ExternalAuthCallModuleStruct do
842842
end
843843
```
844844

845+
```elixir
846+
ExternalAuthCallModuleStruct.builder(%{
847+
name: "Mishka",
848+
auth_path: %{action: "add"},
849+
auth_path1: [%{action: "add"}]
850+
})
851+
```
852+
845853
## List of structs
846854

847855
As was discussed in the earlier available choices. In the `field` macro that is used to call **another module**, as well as in the `sub_field` macro, you have the ability to retrieve a list of structs rather than a single struct.
@@ -958,9 +966,19 @@ end
958966

959967
> **Note**: When changing a record in the database, for example, you might need to make sure that a particular piece of data does not get overwritten by an automatic piece of data if one already exists. To find a solution to this issue, you will need to invoke the `builder` function in the following manner.
960968
969+
```elixir
970+
AutoCoreKeyStruct.builder(%{username: "mishka", user_id: "test_not_to_be_replaced"})
971+
```
972+
961973
```elixir
962974
AutoCoreKeyStruct.builder(
963-
{:root, %{username: "mishka", user_id: "test_not_to_be_replaced"}, :edit}
975+
{:root, %{username: "mishka", user_id: "test_not_to_be_replaced"}, :add}
976+
)
977+
```
978+
979+
```elixir
980+
AutoCoreKeyStruct.builder(
981+
{:root, %{username: "mishka", user_id: "test_should_be_replaced"}, :edit}
964982
)
965983
```
966984

@@ -1011,6 +1029,30 @@ defmodule OnCoreKeyStruct do
10111029
end
10121030
```
10131031

1032+
```elixir
1033+
OnCoreKeyStruct.builder(%{
1034+
name: "mishka",
1035+
profile: %{
1036+
nickname: "Mishka",
1037+
github: "test",
1038+
identity: %{
1039+
provider: "git",
1040+
sub_identity: %{id: "test", auth_path: %{action: "admin/edit"}}
1041+
}
1042+
}
1043+
})
1044+
```
1045+
1046+
```elixir
1047+
OnCoreKeyStruct.builder(%{
1048+
name: "mishka",
1049+
profile: %{
1050+
nickname: "Mishka",
1051+
identity: %{provider: "git"}
1052+
}
1053+
})
1054+
```
1055+
10141056
## From core key
10151057

10161058
You can select this alternative if you require any data that was delivered in another key to be incorporated into the key that you are looking for. If the key is present, the data associated with it will be copied; however, if the key is not there, the data in and of itself will be retained.
@@ -1057,6 +1099,14 @@ defmodule FromCoreKeyStruct do
10571099
end
10581100
```
10591101

1102+
```elixir
1103+
FromCoreKeyStruct.builder(%{
1104+
username: "mishka",
1105+
user_id: "test_to_be_replaced",
1106+
profile: %{nickname: "Mishka", social: %{skype: "mishka_skype", username: "none_to_test"}}
1107+
})
1108+
```
1109+
10601110
## Domain core key
10611111

10621112
When dealing with a structure that is heavily nested, it is occasionally necessary to establish the permitted range of values for a set of parameters based on the input provided by a parent.
@@ -1086,6 +1136,17 @@ defmodule AllowedParentDomain do
10861136
end
10871137
```
10881138

1139+
```elixir
1140+
AllowedParentDomain.builder(%{username: "mishka", auth: %{action: "admin1"}})
1141+
```
1142+
1143+
```elixir
1144+
AllowedParentDomain.builder(%{
1145+
username: "mishka",
1146+
auth: %{action: "admin", social: :banned}
1147+
})
1148+
```
1149+
10891150
**Please see the `domain` core key, for example:**
10901151

10911152
<!-- livebook:{"force_markdown":true} -->
@@ -1162,6 +1223,20 @@ defmodule AllowedParentCustomDomain do
11621223
end
11631224
```
11641225

1226+
```elixir
1227+
AllowedParentCustomDomain.builder(%{
1228+
username: "mishka",
1229+
auth: %{action: "ok"}
1230+
})
1231+
```
1232+
1233+
```elixir
1234+
AllowedParentCustomDomain.builder(%{
1235+
username: "mishka",
1236+
auth: %{action: "error"}
1237+
})
1238+
```
1239+
11651240
**Note**: if you want to use `custom` inside `derive` validation, you should do like this:
11661241

11671242
```elixir
@@ -1179,6 +1254,14 @@ end
11791254

11801255
**Note**: You can see when you use it inside a derive, the GuardedStruct calculates the you module `alias`.
11811256

1257+
```elixir
1258+
CustomValidationDerive.builder(%{status: "ok"})
1259+
```
1260+
1261+
```elixir
1262+
CustomValidationDerive.builder(%{status: "error"})
1263+
```
1264+
11821265
## Conditional fields
11831266

11841267
One of the unique capabilities of this macro is the ability to define conditions and differentiate between the various kinds of `fields`. Assume that you want the `social` field to be able to take both a value `string` and a `map` where `address` and `provider` are included in the `map`.

0 commit comments

Comments
 (0)