Skip to content

Commit

Permalink
Ignore fields that start with _
Browse files Browse the repository at this point in the history
Closes #230
  • Loading branch information
emillon committed Mar 7, 2019
1 parent 13bfb4b commit 7dfd205
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
57 changes: 31 additions & 26 deletions ppx/ppx_cstruct.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ type field = {
off: int;
}

let field_is_ignored f =
String.get f.field 0 = '_'

type t = {
name: string;
fields: field list;
Expand Down Expand Up @@ -220,14 +223,17 @@ let hexdump_to_buffer_expr s =
| UInt64 -> [%expr "0x%Lx\n"]
in
let hexdump_field f =
let get_f = op_evar s (Op_get f) in
let expr =
match f.ty with
|Prim p ->
[%expr Printf.bprintf buf [%e prim_format_string p] ([%e get_f] v)]
|Buffer (_,_) ->
[%expr Printf.bprintf buf "<buffer %s>" [%e Ast.str (field_to_string f)];
Cstruct.hexdump_to_buffer buf ([%e get_f] v)]
if field_is_ignored f then
[%expr ()]
else
let get_f = op_evar s (Op_get f) in
let expr =
match f.ty with
|Prim p ->
[%expr Printf.bprintf buf [%e prim_format_string p] ([%e get_f] v)]
|Buffer (_,_) ->
[%expr Printf.bprintf buf "<buffer %s>" [%e Ast.str (field_to_string f)];
Cstruct.hexdump_to_buffer buf ([%e get_f] v)]
in
[%expr
Printf.bprintf buf " %s = " [%e Ast.str f.field];
Expand All @@ -249,26 +255,25 @@ let op_expr loc s = function
[%expr fun src srcoff dst ->
Cstruct.blit src srcoff dst [%e Ast.int f.off] [%e Ast.int len]]

let field_ops_for f =
if field_is_ignored f then
[]
else
let if_buffer x =
match f.ty with
|Buffer (_,_) -> [x]
|Prim _ -> []
in
List.concat
[ [Op_get f]
; if_buffer (Op_copy f)
; [Op_set f]
; if_buffer (Op_blit f)
]

let ops_for s =
let field_ops =
List.concat (
List.map (fun f ->
let if_buffer x =
match f.ty with
|Buffer (_,_) -> [x]
|Prim _ -> []
in
List.concat
[ [Op_get f]
; if_buffer (Op_copy f)
; [Op_set f]
; if_buffer (Op_blit f)
]
) s.fields
)
in
( [Op_sizeof]
@ field_ops
@ List.concat (List.map field_ops_for s.fields)
@ [Op_hexdump_to_buffer;
Op_hexdump;
])
Expand Down
1 change: 0 additions & 1 deletion ppx_test/basic.expected
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ foo = {
"\007\000,\000\000\190\239abcdefgh"
with_ignored_field = {
a = 0x1
_b = 0x2
c = 0x3

}
2 changes: 1 addition & 1 deletion ppx_test/basic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ type with_ignored_field = {
} [@@little_endian]
]

let _ : Cstruct.t -> int -> unit = set_with_ignored_field__b
let _ : bool = set_with_ignored_field__b

let tests () =
(* Test basic set/get functions *)
Expand Down

0 comments on commit 7dfd205

Please sign in to comment.