コード例 #1
0
ファイル: role.c プロジェクト: gitter-badger/cparse
static void __cparse_role_add_dependency(cParseRole *role, const char *key, cParseObject *ref)
{
	cParseJson *users = NULL;
	cParseJson *objects = NULL;
	cParseJson *pointer = NULL;

	if (!role || cparse_str_empty(key) || !ref) {
		cparse_log_errno(EINVAL);
		return;
	}

	/* only allow certain values for key/pointer type */
	if (strcmp(key, CPARSE_USERS_PATH) && strcmp(key, CPARSE_ROLES_PATH))
	{
		cparse_log_error("can only by of type users or roles");
		return;
	}

	users = cparse_object_get(role, key);

	if (users == NULL) {
		users = cparse_json_new();
		cparse_json_set_string(users, CPARSE_KEY_OP, CPARSE_KEY_ADD_RELATION);
		cparse_object_set(role, key, users);
	}

	objects = cparse_json_get(users, "objects");

	if (objects == NULL) {
		objects = cparse_json_new_array();
		cparse_json_set(users, "objects", objects);
	}

	if (!cparse_json_is_array(objects)) {
		cparse_log_error("objects is not an array");
		return;
	}

	pointer = cparse_pointer_from_object(ref);

	cparse_json_array_add(objects, pointer);
}
コード例 #2
0
ファイル: query.c プロジェクト: ryjen/cparse
bool cparse_query_find_objects(cParseQuery *query, cParseError **error)
{
    cParseRequest *request = NULL;
    cParseJson *data = NULL;
    char buf[CPARSE_BUF_SIZE + 1] = {0};

    if (query == NULL) {
        cparse_log_set_errno(error, EINVAL);
        return false;
    }

    /* build the request */

    request = cparse_request_with_method_and_path(cParseHttpRequestMethodGet, query->urlPath);

    if (request == NULL) {
        cparse_log_set_error(error, "Unable to create request for query");
        return false;
    }

    if (query->where) {
        cparse_request_add_data(request, CPARSE_QUERY_WHERE, cparse_json_to_json_string(query->where));
    }

    if (query->limit > 0) {
        snprintf(buf, CPARSE_BUF_SIZE, "%d", query->limit);
        cparse_request_add_data(request, CPARSE_QUERY_LIMIT, buf);
    }

    if (query->skip > 0) {
        snprintf(buf, CPARSE_BUF_SIZE, "%d", query->skip);
        cparse_request_add_data(request, CPARSE_QUERY_SKIP, buf);
    }

    if (query->keys) {
        cparse_request_add_data(request, CPARSE_QUERY_KEYS, query->keys);
    }

    if (query->count) {
        snprintf(buf, CPARSE_BUF_SIZE, "%d", query->count);
        cparse_request_add_data(request, CPARSE_QUERY_COUNT, buf);
    }

    /* do the deed */
    data = cparse_request_get_json(request, error);

    cparse_request_free(request);

    if (data == NULL) {
        return false;
    }

    if (query->count) {
        query->size = cparse_json_get_number(data, CPARSE_QUERY_COUNT, 0);
    } else {
        cParseJson *results = cparse_json_get(data, CPARSE_QUERY_RESULTS);

        query->size = cparse_json_array_size(results);

        if (query->size > 0) {
            int i;

            if (query->results) {
                cparse_query_free_results(query);

                free(query->results);
            }

            query->results = malloc(cparse_object_sizeof() * query->size);

            if (query->results == NULL) {
                cparse_log_set_errno(error, ENOMEM);
                cparse_json_free(data);
                return false;
            }

            for (i = 0; i < query->size; i++) {
                query->results[i] = cparse_object_from_query(query, cparse_json_array_get(results, i));
            }
        }
    }

    cparse_json_free(data);

    return true;
}