diff --git a/ocaml/sdk-gen/go/autogen/src/go.mod b/ocaml/sdk-gen/go/autogen/src/go.mod new file mode 100644 index 00000000000..0d33115cf62 --- /dev/null +++ b/ocaml/sdk-gen/go/autogen/src/go.mod @@ -0,0 +1,3 @@ +module go/xenapi + +go 1.22.0 diff --git a/ocaml/sdk-gen/go/dune b/ocaml/sdk-gen/go/dune index f481b389530..00d835053bf 100644 --- a/ocaml/sdk-gen/go/dune +++ b/ocaml/sdk-gen/go/dune @@ -35,5 +35,6 @@ (libraries alcotest xapi-test-utils gen_go_helper) (deps (source_tree test_data) + (source_tree templates) ) ) diff --git a/ocaml/sdk-gen/go/gen_go_binding.ml b/ocaml/sdk-gen/go/gen_go_binding.ml index fcabad59d03..1912c87e667 100644 --- a/ocaml/sdk-gen/go/gen_go_binding.ml +++ b/ocaml/sdk-gen/go/gen_go_binding.ml @@ -13,7 +13,27 @@ open Gen_go_helper +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 header = render_template "FileHeader.mustache" obj ^ "\n" in + let error_rendered = + 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" + let main () = + render_api_messages_and_errors () ; let objects = Json.xenapi objects in List.iter (fun (name, obj) -> diff --git a/ocaml/sdk-gen/go/gen_go_helper.ml b/ocaml/sdk-gen/go/gen_go_helper.ml index 0daa9e9f3fe..21a1f75581a 100644 --- a/ocaml/sdk-gen/go/gen_go_helper.ml +++ b/ocaml/sdk-gen/go/gen_go_helper.ml @@ -252,6 +252,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 = diff --git a/ocaml/sdk-gen/go/gen_go_helper.mli b/ocaml/sdk-gen/go/gen_go_helper.mli index 57421335ad1..f58fb079786 100644 --- a/ocaml/sdk-gen/go/gen_go_helper.mli +++ b/ocaml/sdk-gen/go/gen_go_helper.mli @@ -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 diff --git a/ocaml/sdk-gen/go/test_data/api_errors.go b/ocaml/sdk-gen/go/test_data/api_errors.go new file mode 100644 index 00000000000..b14349c1885 --- /dev/null +++ b/ocaml/sdk-gen/go/test_data/api_errors.go @@ -0,0 +1,6 @@ +const ( + // + ERR_MESSAGE_DEPRECATED = "MESSAGE_DEPRECATED" + // + ERR_MESSAGE_REMOVED = "MESSAGE_REMOVED" +) diff --git a/ocaml/sdk-gen/go/test_data/api_messages.go b/ocaml/sdk-gen/go/test_data/api_messages.go new file mode 100644 index 00000000000..055d2077ebd --- /dev/null +++ b/ocaml/sdk-gen/go/test_data/api_messages.go @@ -0,0 +1,6 @@ +const ( + // + MES_HA_STATEFILE_LOST = "HA_STATEFILE_LOST" + // + MES_METADATA_LUN_HEALTHY = "METADATA_LUN_HEALTHY" +) \ No newline at end of file diff --git a/ocaml/sdk-gen/go/test_gen_go.ml b/ocaml/sdk-gen/go/test_gen_go.ml index ab9a08b371a..c2eb2d0fd67 100644 --- a/ocaml/sdk-gen/go/test_gen_go.ml +++ b/ocaml/sdk-gen/go/test_gen_go.ml @@ -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 @@ -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) @@ -251,7 +281,16 @@ let generated_json_tests = check_true "Mustache.Json of records has right structure" @@ List.for_all (fun (_, obj) -> is_same_struct obj json) objects in - [("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 + [ + ("jsons", `Quick, jsons) + ; ("errors_and_messages", `Quick, errors_and_messages) + ] let tests = make_suite "gen_go_binding_"