util: common: Add get_property to simplify the property getting operation 26/276926/7
authorChanwoo Choi <cw00.choi@samsung.com>
Fri, 24 Jun 2022 06:13:09 +0000 (15:13 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Tue, 5 Jul 2022 01:26:26 +0000 (10:26 +0900)
There are already helper funciton to get value from json object.
In order to simpilfy the code getting value from json object,
add get_value_from_object function with data type arguemnt.

[Detailed description of get_property]
int get_property(json_object *obj, const char *key,
int type, bool mandatory, void *data)
- json_object *obj : json object
- const char *key : property key
- int type : data type of property
- bool mandatory : if property is mandatory, true.
- void *data : each data will be stored

Change-Id: Ifd217ae5773e11ccb1f2d74850ee1c68fb035ebe
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
include/util/common.h
src/util/common.c

index 8ff3d85..f361886 100644 (file)
@@ -131,4 +131,7 @@ const int get_int_from_object(json_object *obj, const char *key);
 const double get_double_from_object(json_object *obj, const char *key);
 const int get_boolean_from_object(json_object *obj, const char *key);
 json_object *get_object_from_object(json_object *obj, const char *key);
+
+int get_property(json_object *obj, const char *key, int type, bool mandatory,
+                void *data);
 #endif /* __CORE_COMMON_H__ */
index defcc0f..aae3582 100644 (file)
@@ -138,3 +138,57 @@ json_object *get_object_from_object(json_object *obj, const char *key)
 
        return tmp;
 }
+
+int get_property(json_object *obj, const char *key,
+                int type, bool mandatory, void *data)
+{
+       int ret = 0;
+       int val_int;
+       double val_double;
+       const char *val_str;
+
+       switch (type) {
+       case DATA_TYPE_INT:
+               val_int = get_int_from_object(obj, key);
+               if (val_int < 0)
+                       ret = val_int;
+               else
+                       *(int *)data = val_int;
+               break;
+       case DATA_TYPE_DOUBLE:
+               val_double = get_double_from_object(obj, key);
+               if (val_double < 0)
+                       ret = -EINVAL;
+               else
+                       *(double *)data = val_double;
+               break;
+       case DATA_TYPE_STRING:
+               val_str = get_string_from_object(obj, key);
+               if (!val_str)
+                       ret = -EINVAL;
+               else
+                       snprintf((char *)data, BUFF_MAX, "%s", val_str);
+               break;
+       case DATA_TYPE_BOOLEAN:
+               val_int = get_int_from_object(obj, key);
+               if (val_int < 0)
+                       ret = val_int;
+               else
+                       *(int *)data = val_int;
+               break;
+       default:
+               ret = -EINVAL;
+               break;
+       }
+
+       if (ret < 0) {
+               if (mandatory) {
+                       _E("Failed to get property of '%s'\n", key);
+               } else {
+                       _D("There is no property of '%s'\n", key);
+                       ret = 0;
+               }
+       }
+
+       return ret;
+}