Skip to content

Commit

Permalink
Fixes Model.to_json output
Browse files Browse the repository at this point in the history
  • Loading branch information
filipeom committed Jul 2, 2024
1 parent 2019ec6 commit d1668db
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
20 changes: 12 additions & 8 deletions lib/model.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,16 @@ let to_string (model : t) : string =
Format.asprintf "%a" (pp ~no_values:false) model

let to_json (model : t) : Yojson.t =
let model :> Yojson.t list =
Hashtbl.fold
(fun s v acc ->
let s = Symbol.to_json s in
let v = `Assoc [ ("value", Value.to_json v) ] in
Yojson.Basic.Util.combine s v :: acc )
model []
let combine = Yojson.Basic.Util.combine in
let add_assignment sym value assignments =
let assignment =
match Symbol.to_json sym with
| `Assoc [ (name, props) ] ->
let value = `Assoc [ ("value", Value.to_json value) ] in
`Assoc [ (name, combine props value) ]
| _ -> failwith "Model: Symbol.to_json returned something impossible"
in
combine assignments assignment
in
`Assoc [ ("model", `List model) ]
let model :> Yojson.t = Hashtbl.fold add_assignment model (`Assoc []) in
`Assoc [ ("model", model) ]
3 changes: 1 addition & 2 deletions lib/symbol.ml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ let rename (symbol : t) (name : string) : t = { symbol with name }
let to_string ({ name; _ } : t) : string = name

let to_json ({ name; ty } : t) : Yojson.Basic.t =
`Assoc
[ ("name", `String name); ("ty", `String (Format.asprintf "%a" Ty.pp ty)) ]
`Assoc [ (name, `Assoc [ ("ty", `String (Format.asprintf "%a" Ty.pp ty)) ]) ]

let type_of ({ ty; _ } : t) : Ty.t = ty
12 changes: 6 additions & 6 deletions test/unit/test_model.expected
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"model": [
{ "name": "u", "ty": "str", "value": "abc" },
{ "name": "z", "ty": "bool", "value": true },
{ "name": "x", "ty": "int", "value": 1 },
{ "name": "y", "ty": "real", "value": 2.0 }
]
"model": {
"y": { "ty": "real", "value": 2.0 },
"x": { "ty": "int", "value": 1 },
"z": { "ty": "bool", "value": true },
"u": { "ty": "str", "value": "abc" }
}
}

0 comments on commit d1668db

Please sign in to comment.