Skip to content

Commit

Permalink
fix: autogenerate the file headers
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Zhang <[email protected]>
  • Loading branch information
duobei committed Apr 3, 2024
1 parent 3110ed1 commit 944f300
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 962 deletions.
12 changes: 1 addition & 11 deletions ocaml/sdk-gen/go/dune
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
mustache
xapi-datamodel
gen_go_helper
go_file_headers
)
)

Expand All @@ -22,15 +21,6 @@
)
)

(library
(name go_file_headers)
(wrapped false)
(modules go_file_headers)
(libraries
mustache
)
)

(rule
(alias generate)
(deps
Expand All @@ -43,7 +33,7 @@
(test
(name test_gen_go)
(modules test_gen_go)
(libraries alcotest xapi-test-utils gen_go_helper go_file_headers)
(libraries alcotest xapi-test-utils gen_go_helper)
(deps
(source_tree test_data)
(source_tree templates)
Expand Down
21 changes: 7 additions & 14 deletions ocaml/sdk-gen/go/gen_go_binding.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,21 @@

open Gen_go_helper

let render_header name =
let open Go_file_headers in
let header =
List.assoc name headers |> render_template "FileHeader.mustache"
in
header ^ "\n"

let render_api_messages_and_errors () =
let obj =
`O
[
("api_errors", `A Json.api_errors)
; ("api_messages", `A Json.api_messages)
; ("modules", `Null)
]
in
let messages_rendered =
render_header "api_errors" ^ render_template "APIErrors.mustache" obj ^ "\n"
in
let header = render_template "FileHeader.mustache" obj ^ "\n" in
let error_rendered =
render_header "api_messages"
^ render_template "APIMessages.mustache" obj
^ "\n"
header ^ render_template "APIErrors.mustache" obj ^ "\n"
in
let messages_rendered =
header ^ render_template "APIMessages.mustache" obj ^ "\n"
in
generate_file error_rendered "api_errors.go" ;
generate_file messages_rendered "api_messages.go"
Expand All @@ -44,7 +37,7 @@ let main () =
let objects = Json.xenapi objects in
List.iter
(fun (name, obj) ->
let header_rendered = render_header name in
let header_rendered = render_template "FileHeader.mustache" obj ^ "\n" in
let enums_rendered = render_template "Enum.mustache" obj in
let record_rendered = render_template "Record.mustache" obj in
let rendered = header_rendered ^ enums_rendered ^ record_rendered in
Expand Down
29 changes: 26 additions & 3 deletions ocaml/sdk-gen/go/gen_go_helper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ module Json = struct
concated
in
List.fold_left
(fun (fields, enums) field ->
(fun (fields, enums, has_time_type) field ->
let is_time_type =
match field.ty with DateTime -> true | _ -> false
in
let ty, e = string_of_ty_with_enums field.ty in
( `O
[
Expand All @@ -128,9 +131,10 @@ module Json = struct
]
:: fields
, enums @ e
, if is_time_type then true else has_time_type
)
)
([], []) fields
([], [], false) fields

let enums_from_result obj msg =
match msg.msg_result with
Expand Down Expand Up @@ -176,6 +180,23 @@ module Json = struct
)
[] obj.messages

let get_modules messages has_time_type =
match messages with
| [] ->
`Null
| _ ->
let items =
match has_time_type with
| true ->
[
`O [("name", `String "fmt"); ("sname", `Null)]
; `O [("name", `String "time"); ("sname", `Null)]
]
| false ->
[`O [("name", `String "fmt"); ("sname", `Null)]]
in
`O [("import", `Bool true); ("items", `A items)]

let xenapi objs =
let enum_set = ref EnumSet.empty in
let enum_dedup enums =
Expand All @@ -192,7 +213,7 @@ module Json = struct
in
List.map
(fun obj ->
let fields, enums1 = fields_of_obj_with_enums obj in
let fields, enums1, has_time_type = fields_of_obj_with_enums obj in
let enums2 = enums_in_messages_of_obj obj in
let enums = enum_dedup (enums1 @ enums2) in
let event_snapshot =
Expand All @@ -213,6 +234,7 @@ module Json = struct
[]
in
let obj_name = snake_to_camel obj.name in
let modules = get_modules obj.messages has_time_type in
let event_session_value = function
| "event" ->
[("event", `Bool true); ("session", `Null)]
Expand All @@ -227,6 +249,7 @@ module Json = struct
; ("description", `String (String.trim obj.description))
; ("fields", `A (event_snapshot @ fields))
; ("enums", `A (List.map of_enum enums))
; ("modules", modules)
]
in
let assoc_list = event_session_value obj.name @ base_assoc_list in
Expand Down
Loading

0 comments on commit 944f300

Please sign in to comment.