Skip to content

CP-47358, CP-47361: Generate convert functions Go code #5603

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

Merged
merged 13 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions ocaml/sdk-gen/common/CommonFunctions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,56 @@ let objects =
api
in
objects_of_api api

module TypesOfMessages = struct
open Xapi_stdext_std

let records =
List.map
(fun obj ->
let obj_name = String.lowercase_ascii obj.name in
(obj_name, Datamodel_utils.fields_of_obj obj)
)
objects

let rec decompose = function
| Set x as y ->
y :: decompose x
| Map (a, b) as y ->
(y :: decompose a) @ decompose b
| Option x as y ->
y :: decompose x
| Record r as y ->
let name = String.lowercase_ascii r in
let types_in_field =
List.assoc_opt name records
|> Option.value ~default:[]
|> List.concat_map (fun field -> decompose field.ty)
in
y :: types_in_field
| (SecretString | String | Int | Float | DateTime | Enum _ | Bool | Ref _)
as x ->
[x]

let mesages objects = objects |> List.concat_map (fun x -> x.messages)

(** All types of params in a list of objects (automatically decomposes) *)
let of_params objects =
let param_types =
mesages objects
|> List.concat_map (fun x -> x.msg_params)
|> List.map (fun p -> p.param_type)
|> Listext.List.setify
in
List.concat_map decompose param_types |> Listext.List.setify

(** All types of results in a list of objects (automatically decomposes) *)
let of_results objects =
let return_types =
let aux accu msg =
match msg.msg_result with None -> accu | Some (ty, _) -> ty :: accu
in
mesages objects |> List.fold_left aux [] |> Listext.List.setify
in
List.concat_map decompose return_types |> Listext.List.setify
end
8 changes: 8 additions & 0 deletions ocaml/sdk-gen/common/CommonFunctions.mli
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,11 @@ val session_id : param

val objects : obj list
(** Objects of api that generate SDKs. *)

module TypesOfMessages : sig
val of_params : Datamodel_types.obj list -> Datamodel_types.ty list
(** All the types in the params of messages*)

val of_results : Datamodel_types.obj list -> Datamodel_types.ty list
(** All the types in the results of messages*)
end
49 changes: 49 additions & 0 deletions ocaml/sdk-gen/go/gen_go_binding.ml
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,60 @@ let render_api_messages_and_errors destdir =
generate_file ~rendered:messages_rendered ~destdir
~output_file:"api_messages.go"

let render_convert_header () =
let name s = `O [("name", `String s); ("sname", `Null)] in
let obj =
`O
[
("name", `String "convert")
; ( "modules"
, `O
[
("import", `Bool true)
; ( "items"
, `A (List.map name ["fmt"; "math"; "reflect"; "strconv"; "time"])
)
]
)
]
in
render_template "FileHeader.mustache" obj ~newline:true ()

let render_converts destdir =
let event = render_template "ConvertBatch.mustache" Convert.event_batch () in
let interface =
render_template "ConvertInterface.mustache" Convert.interface ()
in
let param_types = TypesOfMessages.of_params objects in
let result_types = TypesOfMessages.of_results objects in
let generate types of_json =
types
|> List.map (fun ty ->
let params = Convert.of_ty ty in
let template = Convert.template_of_convert params in
let json : Mustache.Json.t = of_json params in
render_template template json ()
)
|> String.concat ""
in
let rendered =
let serializes_rendered = generate param_types Convert.of_serialize in
let deserializes_rendered = generate result_types Convert.of_deserialize in
render_convert_header ()
^ serializes_rendered
^ deserializes_rendered
^ event
^ String.trim interface
^ "\n"
in
generate_file ~rendered ~destdir ~output_file:"convert.go"

let main destdir =
render_api_versions destdir ;
render_api_messages_and_errors destdir ;
let enums = Json.all_enums objects in
render_enums enums destdir ;
render_converts destdir ;
let objects = Json.xenapi objects in
List.iter
(fun (name, obj) ->
Expand Down
Loading
Loading