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

Check for list emptiness before checking variants #178

Merged
merged 3 commits into from
Feb 29, 2024

Conversation

Vincent-lau
Copy link
Contributor

No description provided.

@mseri
Copy link
Collaborator

mseri commented Feb 28, 2024

Why? Filter of an empty list is already the empty list

@Vincent-lau
Copy link
Contributor Author

sorry meant to do this in the List.fold below. Fixed now

@MarkSymsCtx
Copy link

MarkSymsCtx commented Feb 29, 2024

Might be worth adding to the commit message the example failure that raised this

dune build @python --profile=release
File "ocaml/xapi-storage/python/xapi/storage/api/v5/dune", line 1, characters 0-204:
1 | (rule
2 |   (targets datapath.py plugin.py task.py volume.py)
3 |   (alias python)
4 |   (deps
5 |     (:x ../../../../../generator/src/main.exe)
6 |     (source_tree ../../../../)
7 |   )
8 |   (action (run %{x} gen_python -p .))
9 | )
main: internal error, uncaught exception:
      Failure("tl")
      
make: *** [build] Error 1
error: Bad exit status from /var/tmp/rpm-tmp.3pD9wu (%build)

Which resulted from trying to build this change - MarkSymsCtx/xen-api@692d17d

Comment on lines 218 to 222
| variants_to_check ->
List.fold_left
(fun acc x -> List.concat [ acc; check false x ])
(check true (List.hd variants_to_check))
(List.tl variants_to_check)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| variants_to_check ->
List.fold_left
(fun acc x -> List.concat [ acc; check false x ])
(check true (List.hd variants_to_check))
(List.tl variants_to_check)
| v :: vs ->
List.fold_left
(fun acc x -> List.concat [ acc; check false x ])
(check true v) vs

You can simplify this further with pattern matching

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be probably worth to add a test, so that there are no regressions when updating the code in the future

(check true (List.hd variants_to_check))
(List.tl variants_to_check)
match variants_to_check with
| [] -> []

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Vincent-lau I think @mseri meant that you didn't need the | [] -> [] if you used | v :: vs -> form

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need both, the second one pattern matches only on nonempty lists

@Vincent-lau
Copy link
Contributor Author

unit tests written, should be good to go

@mseri
Copy link
Collaborator

mseri commented Feb 29, 2024

If the test requires the use of ppx, please move it to the tests/ppx test suite, this is a bit annoying but avoids circular dependencies when we publish the packages on opam

@MarkSymsCtx
Copy link

If the test requires the use of ppx, please move it to the tests/ppx test suite, this is a bit annoying but avoids circular dependencies when we publish the packages on opam

In fact, could it not go into https://github.com/mirage/ocaml-rpc/blob/master/tests/ppx/variants.ml ?

@Vincent-lau
Copy link
Contributor Author

Vincent-lau commented Feb 29, 2024

I think there is a test_pythongen.ml in rpc/ already, so we should probably move it there. And it is already doing @@deriving rpcty

This tests whether it can handle the case where a varaiant consist only
of zero-arg constructors.

Signed-off-by: Vincent Liu <[email protected]>
@mseri mseri merged commit bdd3ab5 into mirage:master Feb 29, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants