summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2023-05-01 11:05:00 -0500
committerJohn Estabrook <jestabro@vyos.io>2023-05-10 22:52:25 -0500
commitbb6b4f97e08178c1cbf67927cd46d8301fb0a05f (patch)
treea9c179154c3a73130906a3856c755f2302429aa2
parent782d945950e52a8b48b812b0fad6161e43f7f5b0 (diff)
downloadvyos1x-config-bb6b4f97e08178c1cbf67927cd46d8301fb0a05f.tar.gz
vyos1x-config-bb6b4f97e08178c1cbf67927cd46d8301fb0a05f.zip
T5194: add render_json
-rw-r--r--src/reference_tree.ml38
-rw-r--r--src/reference_tree.mli14
2 files changed, 45 insertions, 7 deletions
diff --git a/src/reference_tree.ml b/src/reference_tree.ml
index 24e02a5..7b4c85c 100644
--- a/src/reference_tree.ml
+++ b/src/reference_tree.ml
@@ -1,4 +1,8 @@
-type node_type = Leaf | Tag | Other
+type node_type =
+ | Leaf [@name "leaf"]
+ | Tag [@name "tag"]
+ | Other [@name "other"]
+ [@@deriving yojson]
type value_constraint =
| Regex of string [@name "regex"]
@@ -18,9 +22,9 @@ type ref_node_data = {
keep_order: bool;
hidden: bool;
secret: bool;
-}
+} [@@deriving yojson]
-type t = ref_node_data Vytree.t
+type t = ref_node_data Vytree.t [@@deriving yojson]
exception Bad_interface_definition of string
@@ -191,3 +195,31 @@ let get_completion_data reftree path =
let data = Vytree.data_of_node node in
(data.node_type, data.multi, data.help)
in List.map aux (Vytree.children_of_node @@ Vytree.get reftree path)
+
+module JSONRenderer =
+struct
+ let render_data data =
+ ref_node_data_to_yojson data |> Yojson.Safe.to_string
+
+ let rec render_node node =
+ let name = Vytree.name_of_node node in
+ let children = Vytree.children_of_node node in
+ let data = Vytree.data_of_node node in
+ let data_str = render_data data in
+ let children_strs = List.map render_node children in
+ let children_str = String.concat "," children_strs in
+ if children_str <> "" then
+ Printf.sprintf "\"%s\": {\"node_data\": %s, %s}" name data_str children_str
+ else
+ Printf.sprintf "\"%s\": {\"node_data\": %s}" name data_str
+
+ let render_json node =
+ let data = Vytree.data_of_node node in
+ let data_str = render_data data in
+ let children = Vytree.children_of_node node in
+ let child_configs = List.map render_node children in
+ let child_config = String.concat "," child_configs in
+ Printf.sprintf "{\"node_data\": %s, %s}" data_str child_config
+end (* JSONRenderer *)
+
+let render_json = JSONRenderer.render_json
diff --git a/src/reference_tree.mli b/src/reference_tree.mli
index 92f4582..baa2694 100644
--- a/src/reference_tree.mli
+++ b/src/reference_tree.mli
@@ -1,4 +1,8 @@
-type node_type = Leaf | Tag | Other
+type node_type =
+ | Leaf [@name "leaf"]
+ | Tag [@name "tag"]
+ | Other [@name "other"]
+ [@@deriving yojson]
type value_constraint =
| Regex of string [@name "regex"]
@@ -18,14 +22,14 @@ type ref_node_data = {
keep_order: bool;
hidden: bool;
secret: bool;
-}
+} [@@deriving yojson]
+
+type t = ref_node_data Vytree.t [@@deriving yojson]
exception Bad_interface_definition of string
exception Validation_error of string
-type t = ref_node_data Vytree.t
-
val default_data : ref_node_data
val default : t
@@ -53,3 +57,5 @@ val get_help_string : t -> string list -> string
val get_value_help : t -> string list -> (string * string) list
val get_completion_data : t -> string list -> (node_type * bool * string) list
+
+val render_json : t -> string