[reportdiff] allow "current" and "previous" to be specified in any order and among other fields in file_renamings.json

Summary: This seems more in line with the expectations of the JSON format.

Reviewed By: mbouaziz

Differential Revision: D5500939

fbshipit-source-id: 76dcc47
master
Jules Villard 7 years ago committed by Facebook Github Bot
parent 0c7f08d29d
commit cfecff03c3

@ -25,13 +25,30 @@ module FileRenamings = struct
let from_json input : t =
let j = Yojson.Basic.from_string input in
let renaming_of_assoc assoc : renaming =
match assoc with
| `Assoc [("current", `String current); ("previous", `String previous)]
-> {current; previous}
| _
-> failwithf "Expected JSON object of the following form: '%s', but instead got: '%s'"
"{\"current\": \"aaa.java\", \"previous\": \"BBB.java\"}"
(Yojson.Basic.to_string assoc)
try
match assoc with
| `Assoc l
-> (
let current_opt = List.Assoc.find ~equal:String.equal l "current" in
let previous_opt = List.Assoc.find ~equal:String.equal l "previous" in
match (current_opt, previous_opt) with
| Some `String current, Some `String previous
-> {current; previous}
| None, _
-> raise (Yojson.Json_error "\"current\" field missing")
| Some _, None
-> raise (Yojson.Json_error "\"previous\" field missing")
| Some _, Some `String _
-> raise (Yojson.Json_error "\"current\" field is not a string")
| Some _, Some _
-> raise (Yojson.Json_error "\"previous\" field is not a string") )
| _
-> raise (Yojson.Json_error "not a record")
with Yojson.Json_error err ->
failwithf
"Error parsing file renamings: %s@\nExpected JSON object of the following form: '%s', but instead got: '%s'"
err "{\"current\": \"aaa.java\", \"previous\": \"BBB.java\"}"
(Yojson.Basic.to_string assoc)
in
match j with
| `List json_renamings

@ -49,7 +49,8 @@ let test_file_renamings_from_json =
, "[{\"current\": 1, \"previous\": \"BBB.java\"}]"
, Raise
(Failure
( "Expected JSON object of the following form: "
( "Error parsing file renamings: \"current\" field is not a string"
^ "\nExpected JSON object of the following form: "
^ "'{\"current\": \"aaa.java\", \"previous\": \"BBB.java\"}', "
^ "but instead got: '{\"current\":1,\"previous\":\"BBB.java\"}'" )) )
; ( "test_file_renamings_from_json_with_malformed_input"

Loading…
Cancel
Save