pw-cli: allow setting keys by their value.

Makes things like:

  pw-cli s 102 Props '{ 16777216: 1.2 }'

work. Useful when dealing with custom properties that don't have a name
in the type array.
This commit is contained in:
Wim Taymans 2021-05-03 11:10:55 +02:00
parent 9206fb019f
commit 3c11b75e32

View file

@ -1542,6 +1542,7 @@ static int json_to_pod(struct spa_pod_builder *b, uint32_t id,
struct spa_json it[1]; struct spa_json it[1];
int l, res; int l, res;
const char *v; const char *v;
uint32_t type;
if (spa_json_is_object(value, len) && info != NULL) { if (spa_json_is_object(value, len) && info != NULL) {
if ((ti = spa_debug_type_find(NULL, info->parent)) == NULL) if ((ti = spa_debug_type_find(NULL, info->parent)) == NULL)
@ -1554,16 +1555,18 @@ static int json_to_pod(struct spa_pod_builder *b, uint32_t id,
const struct spa_type_info *pi; const struct spa_type_info *pi;
if ((l = spa_json_next(&it[0], &v)) <= 0) if ((l = spa_json_next(&it[0], &v)) <= 0)
break; break;
if ((pi = find_type_info(ti->values, key)) == NULL) if ((pi = find_type_info(ti->values, key)) != NULL)
type = pi->type;
else if ((type = atoi(key)) == 0)
continue; continue;
spa_pod_builder_prop(b, pi->type, 0); spa_pod_builder_prop(b, type, 0);
if ((res = json_to_pod(b, id, pi, &it[0], v, l)) < 0) if ((res = json_to_pod(b, id, pi, &it[0], v, l)) < 0)
return res; return res;
} }
spa_pod_builder_pop(b, &f[0]); spa_pod_builder_pop(b, &f[0]);
} }
else if (spa_json_is_array(value, len) && info != NULL) { else if (spa_json_is_array(value, len)) {
if (info->parent == SPA_TYPE_Struct) { if (info == NULL || info->parent == SPA_TYPE_Struct) {
spa_pod_builder_push_struct(b, &f[0]); spa_pod_builder_push_struct(b, &f[0]);
} else { } else {
spa_pod_builder_push_array(b, &f[0]); spa_pod_builder_push_array(b, &f[0]);
@ -1575,10 +1578,10 @@ static int json_to_pod(struct spa_pod_builder *b, uint32_t id,
return res; return res;
spa_pod_builder_pop(b, &f[0]); spa_pod_builder_pop(b, &f[0]);
} }
else if (spa_json_is_float(value, len) && info != NULL) { else if (spa_json_is_float(value, len)) {
float val = 0.0f; float val = 0.0f;
spa_json_parse_float(value, len, &val); spa_json_parse_float(value, len, &val);
switch (info->parent) { switch (info ? info->parent : SPA_TYPE_Struct) {
case SPA_TYPE_Bool: case SPA_TYPE_Bool:
spa_pod_builder_bool(b, val >= 0.5f); spa_pod_builder_bool(b, val >= 0.5f);
break; break;
@ -1616,14 +1619,16 @@ static int json_to_pod(struct spa_pod_builder *b, uint32_t id,
else if (spa_json_is_null(value, len)) { else if (spa_json_is_null(value, len)) {
spa_pod_builder_none(b); spa_pod_builder_none(b);
} }
else if (info) { else {
char *val = alloca(len+1); char *val = alloca(len+1);
spa_json_parse_string(value, len, val); spa_json_parse_string(value, len, val);
switch (info->parent) { switch (info ? info->parent : SPA_TYPE_Struct) {
case SPA_TYPE_Id: case SPA_TYPE_Id:
if ((ti = find_type_info(info->values, val)) == NULL) if ((ti = find_type_info(info->values, val)) != NULL)
type = ti->type;
else if ((type = atoi(val)) == 0)
return -EINVAL; return -EINVAL;
spa_pod_builder_id(b, ti->type); spa_pod_builder_id(b, type);
break; break;
case SPA_TYPE_Struct: case SPA_TYPE_Struct:
case SPA_TYPE_String: case SPA_TYPE_String: