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 make. prefix for attr #9

Merged
merged 3 commits into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 21 additions & 19 deletions doc/index.mld
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ executable configuration in dune file.

{1 Syntax}

When tagging supported types with [[@@deriving make]], make functions will be
When tagging [[@@deriving make]] to supported types, make functions will be
derived accordingly.

{2 Supported Types}

{3 Records}

For a record type [t], function [make_t] will be derived. By default, each field will be
corresponded to a labeled argument, where the label is the same as the field
name. If the field has type of [option], [list], [array] or [string], the argument
will be optional and have a default value of [None], [[]], [[||]] or [""]. The
function will finally take an [unit] and return the constructed [t].
For a record type [t], function [make_t] will be derived. By default, each field
will be corresponded to a labeled argument, where the label is the same as the
field name. If the field has type of [option], [list], [array] or [string], the
argument will be optional and have a default value of [None], [[]], [[||]] or
[""]. The function will finally take an [unit] and return the constructed [t].

{[
type t = {
Expand All @@ -58,11 +58,11 @@ function will finally take an [unit] and return the constructed [t].

{3 Tuples}

For a tuple type [t], function [make_t] will be derived. Each value in the tuple will be
corresponded to a labeled argument. The first value will be corresponded to
labeled argument [v0], the second value to [v1] and vice versa. The optionality
of the arguments follows the same rule as that of the record. The function will
finally take an [unit] and return the constructed [t].
For a tuple type [t], function [make_t] will be derived. Each value in the tuple
will be corresponded to a labeled argument. The first value will be corresponded
to labeled argument [v0], the second value to [v1] and vice versa. The
optionality of the arguments follows the same rule as that of the record. The
function will finally take an [unit] and return the constructed [t].

{[
type t = int * int option * int list [@@deriving make]
Expand All @@ -72,8 +72,9 @@ finally take an [unit] and return the constructed [t].

{3 Options}

For an option type [t], function [make_t] will be derived. The function will take an
optional argument [?value] and an [unit], and then return the constructed [t].
For an option type [t], function [make_t] will be derived. The function will
take an optional argument [?value] and an [unit], and then return the
constructed [t].

{[
type t = int option [@@deriving make]
Expand All @@ -84,11 +85,11 @@ optional argument [?value] and an [unit], and then return the constructed [t].
{3 Variants}

For a variant type [t], a make function will be derived for each case of the
variant. For a case [C] in variant [t], function [make_c_of_t] will be derived. If the
case is constant, the function will only take an [unit] and return the constant
immediately. In other cases, the signature of the function will follow the rules
described above. For case with only 1 argument, it will be considered as a tuple of
size 1. Inline record is also supported.
variant. For a case [C] in variant [t], function [make_c_of_t] will be derived.
If the case is constant, the function will only take an [unit] and return the
constant immediately. In other cases, the signature of the function will follow
the rules described above. For case with only 1 argument, it will be considered
as a tuple of size 1. Inline record is also supported.

{[
type t =
Expand All @@ -107,7 +108,8 @@ size 1. Inline record is also supported.

{2 Attributes}

Attributes may be used to alter the behavior of the derived make function.
Attributes can be used to modify the behavior of the derived make function.
Attributes may be prefixed with [make.] to avoid conflicts with other extensions.

{3 [[@default]]}

Expand Down
2 changes: 1 addition & 1 deletion dune-project
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(lang dune 2.7)
(name ppx_make)
(version "0.3.2")
(version "0.3.3")

(generate_opam_files true)

Expand Down
2 changes: 1 addition & 1 deletion ppx_make.opam
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
version: "0.4.0"
version: "0.3.3"
synopsis: "[@@deriving make]"
description: "Ppxlib based [@@deriving] plugin to generate make functions"
maintainer: ["Boning <me@boni.ng>"]
Expand Down
12 changes: 5 additions & 7 deletions src/arg_type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ let get_attr (attrs : attribute list) =
List.fold_left
(fun acc (attr : attribute) ->
(match attr.attr_name.txt with
| "main" -> Main
| "required" -> Required
| "default" -> (
| "main" | "make.main" -> Main
| "required" | "make.required" -> Required
| "default" | "make.default" -> (
match attr.attr_payload with
| PStr [ { pstr_desc = Pstr_eval (expr, _); _ } ] -> Default expr
| _ ->
Expand Down Expand Up @@ -63,15 +63,13 @@ let label_to_asttypes (name, ct, arg_types) =
let pat = Ast_helper.Pat.var ~loc:name.loc name in
match arg_types with
| Some Labelled -> (Asttypes.Labelled name.txt, None, pat, ct)
| Some Optional def_expr -> (Optional name.txt, def_expr, pat, ct)
| Some (Optional def_expr) -> (Optional name.txt, def_expr, pat, ct)
| _ -> Location.raise_errorf ~loc:name.loc "unexpected error"

let split ts =
let labels, mains = List.partition (fun (_, _, l) -> l <> None) ts in
let labels =
List.map
(fun (name, ct, l) -> label_to_asttypes (name, ct, l))
labels
List.map (fun (name, ct, l) -> label_to_asttypes (name, ct, l)) labels
and mains =
List.map
(fun (name, ct, _) ->
Expand Down
6 changes: 3 additions & 3 deletions test/record_types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ type complex = {
c2 : int option;
c3 : int list;
c4 : string;
c5 : int; [@default 1024]
c6 : string; [@required]
c5 : int; [@make.default 1024]
c6 : string; [@make.required]
}
[@@deriving make]

type complex_with_main = {
cm1 : int; [@main]
cm1 : int; [@make.main]
cm2 : int option;
cm3 : int Option.t; [@main]
}
Expand Down
6 changes: 3 additions & 3 deletions test/record_types.mli
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ type complex = {
c2 : int option;
c3 : int list;
c4 : string;
c5 : int; [@default 1024]
c6 : string; [@required]
c5 : int; [@make.default 1024]
c6 : string; [@make.required]
}
[@@deriving_inline make]

Expand All @@ -121,7 +121,7 @@ end
[@@@end]

type complex_with_main = {
cm1 : int; [@main]
cm1 : int; [@make.main]
cm2 : int option;
cm3 : int Option.t; [@main]
}
Expand Down