Skip to content

Commit

Permalink
CP-47364: generate api messages and errors of Golang code
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Zhang <[email protected]>
  • Loading branch information
duobei committed Mar 29, 2024
1 parent a02cb0f commit 30434a1
Show file tree
Hide file tree
Showing 10 changed files with 747 additions and 585 deletions.
3 changes: 3 additions & 0 deletions ocaml/sdk-gen/go/autogen/src/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module go/xenapi

go 1.22.0
1 change: 1 addition & 0 deletions ocaml/sdk-gen/go/dune
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@
(libraries alcotest xapi-test-utils gen_go_helper go_file_headers)
(deps
(source_tree test_data)
(source_tree templates)
)
)
20 changes: 20 additions & 0 deletions ocaml/sdk-gen/go/gen_go_binding.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,27 @@ let render_header name =
in
header ^ "\n"

let render_api_messages_and_errors () =
let obj =
`O
[
("api_errors", `A Json.api_errors)
; ("api_messages", `A Json.api_messages)
]
in
let messages_rendered =
render_header "api_errors" ^ render_template "APIErrors.mustache" obj ^ "\n"
in
let error_rendered =
render_header "api_messages"
^ render_template "APIMessages.mustache" obj
^ "\n"
in
generate_file error_rendered "api_errors.go" ;
generate_file messages_rendered "api_messages.go"

let main () =
render_api_messages_and_errors () ;
let objects = Json.xenapi objects in
List.iter
(fun (name, obj) ->
Expand Down
6 changes: 6 additions & 0 deletions ocaml/sdk-gen/go/gen_go_helper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ module Json = struct
(String.lowercase_ascii obj.name, `O assoc_list)
)
objs

let api_messages =
List.map (fun (msg, _) -> `O [("name", `String msg)]) !Api_messages.msgList

let api_errors =
List.map (fun error -> `O [("name", `String error)]) !Api_errors.errors
end

let objects =
Expand Down
4 changes: 4 additions & 0 deletions ocaml/sdk-gen/go/gen_go_helper.mli
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ val generate_file : string -> string -> unit

module Json : sig
val xenapi : Datamodel_types.obj list -> (string * Mustache.Json.t) list

val api_messages : Mustache.Json.value list

val api_errors : Mustache.Json.value list
end
6 changes: 6 additions & 0 deletions ocaml/sdk-gen/go/test_data/api_errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const (
//
ERR_MESSAGE_DEPRECATED = "MESSAGE_DEPRECATED"
//
ERR_MESSAGE_REMOVED = "MESSAGE_REMOVED"
)
6 changes: 6 additions & 0 deletions ocaml/sdk-gen/go/test_data/api_messages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const (
//
MES_HA_STATEFILE_LOST = "HA_STATEFILE_LOST"
//
MES_METADATA_LUN_HEALTHY = "METADATA_LUN_HEALTHY"
)
42 changes: 41 additions & 1 deletion ocaml/sdk-gen/go/test_gen_go.ml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,30 @@ let enums : Mustache.Json.t =
)
]

let api_errors : Mustache.Json.t =
`O
[
( "api_errors"
, `A
[
`O [("name", `String "MESSAGE_DEPRECATED")]
; `O [("name", `String "MESSAGE_REMOVED")]
]
)
]

let api_messages : Mustache.Json.t =
`O
[
( "api_messages"
, `A
[
`O [("name", `String "HA_STATEFILE_LOST")]
; `O [("name", `String "METADATA_LUN_HEALTHY")]
]
)
]

module TemplatesTest = Generic.MakeStateless (struct
module Io = struct
type input_t = string * Mustache.Json.t
Expand All @@ -228,12 +252,18 @@ module TemplatesTest = Generic.MakeStateless (struct

let enums_rendered = string_of_file "enum.go"

let api_errors_rendered = string_of_file "api_errors.go"

let api_messages_rendered = string_of_file "api_messages.go"

let tests =
`QuickAndAutoDocumented
[
(("FileHeader.mustache", header), file_header_rendered)
; (("Record.mustache", record), record_rendered)
; (("Enum.mustache", enums), enums_rendered)
; (("APIErrors.mustache", api_errors), api_errors_rendered)
; (("APIMessages.mustache", api_messages), api_messages_rendered)
]
end)

Expand All @@ -256,7 +286,17 @@ let generated_json_tests =
check_true "Mustache.Json of headers has right structure"
@@ List.for_all (fun (_, h) -> is_same_struct h header) headers
in
[("headers", `Quick, headers); ("jsons", `Quick, jsons)]
let errors_and_messages () =
let errors = `O [("api_errors", `A Json.api_errors)] in
let messages = `O [("api_messages", `A Json.api_messages)] in
check_true "Mustache.Json of errors and messages has right structure"
@@ (is_same_struct errors api_errors && is_same_struct messages api_messages)
in
[
("headers", `Quick, headers)
; ("jsons", `Quick, jsons)
; ("errors_and_messages", `Quick, errors_and_messages)
]

let tests =
make_suite "gen_go_binding_"
Expand Down
Loading

0 comments on commit 30434a1

Please sign in to comment.