util: Introduce virJSONValueObjectStealArray

Provide the Steal API for any code paths that will desire to grab the
object array and then free it afterwards rather than relying to freeing
the whole chain from the reply.
This commit is contained in:
John Ferlan 2016-10-03 14:45:13 -04:00
parent 8546f723db
commit ebf8b783bf
3 changed files with 49 additions and 0 deletions

View File

@ -1821,6 +1821,7 @@ virJSONValueObjectHasKey;
virJSONValueObjectIsNull; virJSONValueObjectIsNull;
virJSONValueObjectKeysNumber; virJSONValueObjectKeysNumber;
virJSONValueObjectRemoveKey; virJSONValueObjectRemoveKey;
virJSONValueObjectStealArray;
virJSONValueToString; virJSONValueToString;

View File

@ -770,6 +770,30 @@ virJSONValueObjectGet(virJSONValuePtr object,
} }
static virJSONValuePtr
virJSONValueObjectSteal(virJSONValuePtr object,
const char *key)
{
size_t i;
virJSONValuePtr obj = NULL;
if (object->type != VIR_JSON_TYPE_OBJECT)
return NULL;
for (i = 0; i < object->data.object.npairs; i++) {
if (STREQ(object->data.object.pairs[i].key, key)) {
VIR_STEAL_PTR(obj, object->data.object.pairs[i].value);
VIR_FREE(object->data.object.pairs[i].key);
VIR_DELETE_ELEMENT(object->data.object.pairs, i,
object->data.object.npairs);
break;
}
}
return obj;
}
/* Return the value associated with KEY within OBJECT, but return NULL /* Return the value associated with KEY within OBJECT, but return NULL
* if the key is missing or if value is not the correct TYPE. */ * if the key is missing or if value is not the correct TYPE. */
virJSONValuePtr virJSONValuePtr
@ -785,6 +809,21 @@ virJSONValueObjectGetByType(virJSONValuePtr object,
} }
/* Steal the value associated with KEY within OBJECT, but return NULL
* if the key is missing or if value is not the correct TYPE. */
static virJSONValuePtr
virJSONValueObjectStealByType(virJSONValuePtr object,
const char *key,
virJSONType type)
{
virJSONValuePtr value = virJSONValueObjectSteal(object, key);
if (value && value->type == type)
return value;
return NULL;
}
int int
virJSONValueObjectKeysNumber(virJSONValuePtr object) virJSONValueObjectKeysNumber(virJSONValuePtr object)
{ {
@ -1194,6 +1233,13 @@ virJSONValueObjectGetArray(virJSONValuePtr object, const char *key)
} }
virJSONValuePtr
virJSONValueObjectStealArray(virJSONValuePtr object, const char *key)
{
return virJSONValueObjectStealByType(object, key, VIR_JSON_TYPE_ARRAY);
}
int int
virJSONValueObjectIsNull(virJSONValuePtr object, virJSONValueObjectIsNull(virJSONValuePtr object,
const char *key) const char *key)

View File

@ -136,6 +136,8 @@ virJSONValuePtr virJSONValueObjectGetObject(virJSONValuePtr object,
const char *key); const char *key);
virJSONValuePtr virJSONValueObjectGetArray(virJSONValuePtr object, virJSONValuePtr virJSONValueObjectGetArray(virJSONValuePtr object,
const char *key); const char *key);
virJSONValuePtr virJSONValueObjectStealArray(virJSONValuePtr object,
const char *key);
const char *virJSONValueObjectGetString(virJSONValuePtr object, const char *key); const char *virJSONValueObjectGetString(virJSONValuePtr object, const char *key);
int virJSONValueObjectGetNumberInt(virJSONValuePtr object, const char *key, int *value); int virJSONValueObjectGetNumberInt(virJSONValuePtr object, const char *key, int *value);