From e4cac118645ffb290ec78e4cde6c9757219d3a10 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Fri, 16 Feb 2018 21:11:21 +0700 Subject: Implement config reading functions and a minimal command line client for using them. Yes, I hate oversized commits too, but this is hard to avoid sometimes. Adjustments to the Session functions logic required to make it work: Do not try to validate the path. The validation function is geared towards validating _set_ paths, so when path lacks a value, it doesn't work right. We assume that the path has been through set at some point, so if a path currently exists in the config tree, it is also a valid path that can be used for Reference_tree.is_leaf etc. --- src/session.ml | 48 +++++++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 17 deletions(-) (limited to 'src/session.ml') diff --git a/src/session.ml b/src/session.ml index 90ab5c8..a502629 100644 --- a/src/session.ml +++ b/src/session.ml @@ -77,27 +77,41 @@ let delete w s path = {s with proposed_config=config; changeset=(op :: s.changeset)} let get_value w s path = - let path, _ = RT.validate_path D.(w.dirs.validators) w.reference_tree path in - if RT.is_leaf w.reference_tree path then - if not ((RT.is_multi w.reference_tree path) || (RT.is_valueless w.reference_tree path)) - then CT.get_value s.proposed_config path - else raise (Session_error "This node can have more than one value") - else raise (Session_error "Cannot get a value of a non-leaf node") + if not (Vytree.exists s.proposed_config path) then + raise (Session_error ("Path does not exist")) + else if not (RT.is_leaf w.reference_tree path) then + raise (Session_error "Cannot get a value of a non-leaf node") + else if (RT.is_multi w.reference_tree path) then + raise (Session_error "This node can have more than one value") + else if (RT.is_valueless w.reference_tree path) then + raise (Session_error "This node can have more than one value") + else CT.get_value s.proposed_config path let get_values w s path = - let path, _ = RT.validate_path D.(w.dirs.validators) w.reference_tree path in - if RT.is_leaf w.reference_tree path then - if RT.is_multi w.reference_tree path - then CT.get_values s.proposed_config path - else raise (Session_error "This node can have only one value") - else raise (Session_error "Cannot get a value of a non-leaf node") + if not (Vytree.exists s.proposed_config path) then + raise (Session_error ("Path does not exist")) + else if not (RT.is_leaf w.reference_tree path) then + raise (Session_error "Cannot get a value of a non-leaf node") + else if not (RT.is_multi w.reference_tree path) then + raise (Session_error "This node can have only one value") + else CT.get_values s.proposed_config path let list_children w s path = - let path, _ = RT.validate_path D.(w.dirs.validators) w.reference_tree path in - if not (RT.is_leaf w.reference_tree path) - then Vytree.children_of_path s.proposed_config path - else raise (Session_error "Cannot list children of a leaf node") + if not (Vytree.exists s.proposed_config path) then + raise (Session_error ("Path does not exist")) + else if (RT.is_leaf w.reference_tree path) then + raise (Session_error "Cannot list children of a leaf node") + else Vytree.children_of_path s.proposed_config path let exists w s path = - let path, _ = RT.validate_path D.(w.dirs.validators) w.reference_tree path in Vytree.exists s.proposed_config path + +let show_config w s path fmt = + let open Vyconf_types in + if not (Vytree.exists s.proposed_config path) then + raise (Session_error ("Path does not exist")) + else + let node = Vytree.get s.proposed_config path in + match fmt with + | Curly -> CT.render node + | Json -> CT.to_yojson node |> Yojson.Safe.pretty_to_string -- cgit v1.2.3