diff mbox series

[V2,1/2] Add accessors for parser to check field type

Message ID 20250716142510.249071-1-stefano.babic@swupdate.org
State Accepted
Headers show
Series [V2,1/2] Add accessors for parser to check field type | expand

Commit Message

Stefano Babic July 16, 2025, 2:25 p.m. UTC
Signed-off-by: Stefano Babic <stefano.babic@swupdate.org>
---
 core/parsing_library.c              | 30 ++++++++++++++++++
 corelib/parsing_library_libconfig.c | 47 +++++++++++++++++++++++------
 corelib/parsing_library_libjson.c   | 46 +++++++++++++++++++++++-----
 include/parselib-private.h          |  4 +++
 include/parselib.h                  |  2 ++
 5 files changed, 112 insertions(+), 17 deletions(-)
diff mbox series

Patch

diff --git a/core/parsing_library.c b/core/parsing_library.c
index a1fcfdd8..329ab50c 100644
--- a/core/parsing_library.c
+++ b/core/parsing_library.c
@@ -164,6 +164,36 @@  bool is_field_numeric(parsertype p, void *e, const char *path)
 	return false;
 }
 
+bool is_field_bool(parsertype p, void *e, const char *path)
+{
+	switch (p) {
+	case LIBCFG_PARSER:
+		return is_field_bool_cfg((config_setting_t *)e, path);
+	case JSON_PARSER:
+		return is_field_bool_json((json_object *)e, path);
+	default:
+		(void)e;
+		(void)path;
+	}
+	return false;
+}
+
+bool is_field_string(parsertype p, void *e, const char *path)
+{
+	switch (p) {
+	case LIBCFG_PARSER:
+		return is_field_string_cfg((config_setting_t *)e, path);
+	case JSON_PARSER:
+		return is_field_string_json((json_object *)e, path);
+	default:
+		(void)e;
+		(void)path;
+	}
+	return false;
+}
+
+
+
 void get_field(parsertype p, void *e, const char *path, void *dest, field_type_t type)
 {
 	switch (p) {
diff --git a/corelib/parsing_library_libconfig.c b/corelib/parsing_library_libconfig.c
index bcb72a11..8878a42f 100644
--- a/corelib/parsing_library_libconfig.c
+++ b/corelib/parsing_library_libconfig.c
@@ -37,6 +37,22 @@  static unsigned int map_field_type(field_type_t type)
 	}
 }
 
+static bool get_field_type(config_setting_t *e, const char *path, int *type)
+{
+	config_setting_t *elem;
+
+	if (path)
+		elem = config_setting_lookup(e, path);
+	else
+		elem = e;
+
+	if (!elem)
+		return false;
+
+	*type = config_setting_type(elem);
+
+	return true;
+}
 
 static void get_value_libconfig(const config_setting_t *e, const char *path, void *dest, field_type_t expected_type)
 {
@@ -103,24 +119,37 @@  void iterate_field_libconfig(config_setting_t *e, iterate_callback cb, void *dat
 
 bool is_field_numeric_cfg(config_setting_t *e, const char *path)
 {
-	config_setting_t *elem;
 	int type;
 
-	if (path)
-		elem = config_setting_lookup(e, path);
-	else
-		elem = e;
-
-	if (!elem)
+	if (!get_field_type(e, path, &type))
 		return false;
 
-	type = config_setting_type(elem);
-
 	return type == CONFIG_TYPE_INT ||
 	       type == CONFIG_TYPE_INT64 ||
 	       type == CONFIG_TYPE_FLOAT;
 }
 
+bool is_field_bool_cfg(config_setting_t *e, const char *path)
+{
+	int type;
+
+	if (!get_field_type(e, path, &type))
+		return false;
+
+	return type == CONFIG_TYPE_BOOL;
+}
+
+bool is_field_string_cfg(config_setting_t *e, const char *path)
+{
+	int type;
+
+	if (!get_field_type(e, path, &type))
+		return false;
+
+	return type == CONFIG_TYPE_STRING;
+}
+
+
 void get_field_cfg(config_setting_t *e, const char *path, void *dest, field_type_t type)
 {
 	config_setting_t *elem;
diff --git a/corelib/parsing_library_libjson.c b/corelib/parsing_library_libjson.c
index 99b60f55..96db0d35 100644
--- a/corelib/parsing_library_libjson.c
+++ b/corelib/parsing_library_libjson.c
@@ -38,6 +38,22 @@  static json_type map_field_type(field_type_t type)
 	}
 }
 
+static bool get_field_type(json_object *e, const char *path, enum json_type *type)
+{
+	json_object *fld = NULL;
+
+	if (path) {
+		if (!json_object_object_get_ex(e, path, &fld))
+			return false;
+	} else {
+		fld = e;
+	}
+
+	*type = json_object_get_type(fld);
+
+	return true;
+}
+
 json_object *find_json_recursive_node(json_object *root, const char **names)
 {
 	json_object *node = root;
@@ -147,20 +163,34 @@  static void get_value_json(json_object *e, const char *path, void *dest, field_t
 bool is_field_numeric_json(json_object *e, const char *path)
 {
 	enum json_type type;
-	json_object *fld = NULL;
 
-	if (path) {
-		if (!json_object_object_get_ex(e, path, &fld))
-			return false;
-	} else {
-		fld = e;
-	}
+	if (!get_field_type(e, path, &type))
+		return false;
 
-	type = json_object_get_type(fld);
 	return type == json_type_int ||
 	       type == json_type_double;
 }
 
+bool is_field_bool_json(json_object *e, const char *path)
+{
+	enum json_type type;
+
+	if (!get_field_type(e, path, &type))
+		return false;
+
+	return type == json_type_boolean;
+}
+
+bool is_field_string_json(json_object *e, const char *path)
+{
+	enum json_type type;
+
+	if (!get_field_type(e, path, &type))
+		return false;
+
+	return type == json_type_string;
+}
+
 void get_field_json(json_object *e, const char *path, void *dest, field_type_t type)
 {
 	json_object *fld = NULL;
diff --git a/include/parselib-private.h b/include/parselib-private.h
index 9115b0b8..efb6e5ee 100644
--- a/include/parselib-private.h
+++ b/include/parselib-private.h
@@ -12,6 +12,8 @@ 
 #include <json-c/json.h>
 
 bool is_field_numeric_cfg(config_setting_t *e, const char *path);
+bool is_field_bool_cfg(config_setting_t *e, const char *path);
+bool is_field_string_cfg(config_setting_t *e, const char *path);
 void get_field_cfg(config_setting_t *e, const char *path, void *dest, field_type_t type);
 void *get_child_libconfig(void *e, const char *name);
 void iterate_field_libconfig(config_setting_t *e, iterate_callback cb,
@@ -24,6 +26,8 @@  void *get_node_libconfig(config_t *cfg, const char **nodes);
  * JSON implementation for parselib
  */
 bool is_field_numeric_json(json_object *e, const char *path);
+bool is_field_bool_json(json_object *e, const char *path);
+bool is_field_string_json(json_object *e, const char *path);
 const char *get_field_string_json(json_object *e, const char *path);
 void get_field_json(json_object *e, const char *path, void *dest, field_type_t type);
 void *get_child_json(json_object *e, const char *name);
diff --git a/include/parselib.h b/include/parselib.h
index 16361590..b1c821e7 100644
--- a/include/parselib.h
+++ b/include/parselib.h
@@ -48,6 +48,8 @@  char *json_get_data_url(json_object *json_root, const char *key);
  * Parselib interface
  */
 bool is_field_numeric(parsertype p, void *e, const char *path);
+bool is_field_bool(parsertype p, void *e, const char *path);
+bool is_field_string(parsertype p, void *e, const char *path);
 const char *get_field_string(parsertype p, void *e, const char *path);
 void get_field_string_with_size(parsertype p, void *e, const char *path,
 				char *d, size_t n);