tangled
alpha
login
or
join now
8bit.lol
/
pegasus
forked from
futur.blue/pegasus
0
fork
atom
objective categorical abstract machine language personal data server
0
fork
atom
overview
issues
pulls
pipelines
hermes: Preserve union #type when converting to yojson
futur.blue
2 months ago
befa211a
1b842580
verified
This commit was signed with the committer's
known signature
.
futur.blue
SSH Key Fingerprint:
SHA256:QHGqHWNpqYyw9bt8KmPuJIyeZX9SZewBZ0PR1COtKQ0=
+11
-6
1 changed file
expand all
collapse all
unified
split
hermes-cli
lib
codegen.ml
+11
-6
hermes-cli/lib/codegen.ml
···
159
159
List.iter
160
160
(fun ref_str ->
161
161
let variant_name = Naming.variant_name_of_ref ref_str in
162
162
+
let full_type_uri = gen_type_uri nsid ref_str in
162
163
let payload_type = gen_ref_type nsid out ref_str in
163
163
-
emitln out
164
164
-
(Printf.sprintf " | %s v -> %s_to_yojson v" variant_name
165
165
-
payload_type ) )
164
164
+
emitln out (Printf.sprintf " | %s v ->" variant_name) ;
165
165
+
emitln out (Printf.sprintf " (match %s_to_yojson v with" payload_type) ;
166
166
+
emitln out (Printf.sprintf " | `Assoc fields -> `Assoc ((\"$type\", `String \"%s\") :: fields)" full_type_uri) ;
167
167
+
emitln out " | other -> other)" )
166
168
refs ;
167
169
if not is_closed then emitln out " | Unknown j -> j" ;
168
170
emit_newline out
···
243
245
else emitln out " | _ -> Ok (Unknown json)" ;
244
246
emitln out " with _ -> Error \"failed to parse union\"" ;
245
247
emit_newline out ;
246
246
-
(* generate to_yojson function *)
248
248
+
(* generate to_yojson function - inject $type field *)
247
249
emitln out (Printf.sprintf "let %s_to_yojson = function" type_name) ;
248
250
List.iter
249
251
(fun ref_str ->
250
252
let variant_name = Naming.variant_name_of_ref ref_str in
253
253
+
let full_type_uri = gen_type_uri nsid ref_str in
251
254
let payload_type = gen_ref_type nsid out ref_str in
252
252
-
emitln out
253
253
-
(Printf.sprintf " | %s v -> %s_to_yojson v" variant_name payload_type) )
255
255
+
emitln out (Printf.sprintf " | %s v ->" variant_name) ;
256
256
+
emitln out (Printf.sprintf " (match %s_to_yojson v with" payload_type) ;
257
257
+
emitln out (Printf.sprintf " | `Assoc fields -> `Assoc ((\"$type\", `String \"%s\") :: fields)" full_type_uri) ;
258
258
+
emitln out " | other -> other)" )
254
259
spec.refs ;
255
260
if not is_closed then emitln out " | Unknown j -> j" ;
256
261
emit_newline out