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

Support index on array types #14

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 65 additions & 12 deletions packages/demo/src/demo.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 11 additions & 7 deletions packages/demo/src/demo.re
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
module StateLenses = [%lenses
type state = {
email: string,
age: int,
}
type state = {
email: string,
age: int,
hobbies: array(string),
}
];


open StateLenses;

let state = {email: "[email protected]", age: 0};
let state = {
email: "[email protected]",
age: 0,
hobbies: [|"foo", "bar"|],
};

Js.log(state->get(Email));
Js.log(state->get(Age));
Js.log(state->get(HobbiesAt(1)));
2 changes: 1 addition & 1 deletion packages/ppx/esy.lock/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -605,4 +605,4 @@
"devDependencies": []
}
}
}
}
1,187 changes: 1,071 additions & 116 deletions packages/ppx/src/LensesPpx.re

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions packages/ppx/test/Test.ml
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
module FormConfig = struct
module State = [%lenses
type t = {
email: string;
age: int;
};;
]
end;;
type t = {
email: string;
age: int;
hobbies: string array;
}
]
end

module User = [%lenses
type t = {
email: string;
age: int;
};;
hobbies: string array;
}
]
3 changes: 2 additions & 1 deletion packages/ppx/test/Test.re
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module State = [%lenses
type state = {
email: string,
age: int,
hobbies: array(string),
}
];

let state: State.state = {email: "", age: 0};
let state: State.state = {email: "", age: 0, hobbies: [|"foo", "bar"|]};
66 changes: 45 additions & 21 deletions packages/ppx/test/Wanted.ml
Original file line number Diff line number Diff line change
@@ -1,24 +1,48 @@
module FormConfig =
struct
type _ field =
| Email: string field
| Age: int field
type state = {
email: string;
age: int;}
let get : 'value . state -> 'value field -> 'value= fun (type value) ->
(fun state ->
fun field ->
match field with | Email -> state.email | Age -> state.age :
state -> value field -> value)
let set : 'value . state -> 'value field -> 'value -> state= fun (type
value) ->
(fun state ->
fun field ->
fun value ->
match field with
| Email -> { state with email = value }
| Age -> { state with age = value } : state ->
value field ->
value -> state)
module State =
struct
type t = {
email: string;
age: int;
hobbies: string array;}
type _ field =
| Email: string field
| Age: int field
| Hobbies: string array field
| HobbiesAt: int -> string option field
| HobbiesAtExn: int -> string field
let get : 'value . t -> 'value field -> 'value= fun (type value) ->
(fun t ->
fun field ->
match field with
| Email -> t.email
| Age -> t.age
| Hobbies -> t.hobbies
| HobbiesAt index ->
(try Some ((t.hobbies).(index)) with | _ -> None)
| HobbiesAtExn index -> (t.hobbies).(index) : t ->
value field ->
value)
let set : 'value . t -> 'value field -> 'value -> t= fun (type value)
->
(fun t ->
fun field ->
fun value ->
match field with
| Email -> { t with email = value }
| Age -> { t with age = value }
| Hobbies -> { t with hobbies = value }
| HobbiesAt index ->
(match value with
| Some value ->
((t.hobbies).(index) <- value;
{ t with hobbies = (t.hobbies) })
| None -> t)
| HobbiesAtExn index ->
((t.hobbies).(index) <- value;
{ t with hobbies = (t.hobbies) }) : t ->
value field ->
value -> t)
end
end