Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filters to generate uuids #3119

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Makefile.am
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

### C source files to be built and distributed.

LIBJQ_INCS = src/builtin.h src/bytecode.h src/compile.h \
Expand All @@ -7,20 +6,23 @@ LIBJQ_INCS = src/builtin.h src/bytecode.h src/compile.h \
src/linker.h src/locfile.h src/opcode_list.h src/parser.y \
src/util.h src/decNumber/decContext.h src/decNumber/decNumber.h \
src/decNumber/decNumberLocal.h src/jv_dtoa_tsd.h src/jv_thread.h \
src/jv_private.h
src/jv_private.h \
src/uuid.h

LIBJQ_SRC = src/builtin.c src/bytecode.c src/compile.c src/execute.c \
src/jq_test.c src/jv.c src/jv_alloc.c src/jv_aux.c \
src/jv_dtoa.c src/jv_file.c src/jv_parse.c src/jv_print.c \
src/jv_unicode.c src/linker.c src/locfile.c src/util.c \
src/decNumber/decContext.c src/decNumber/decNumber.c \
src/jv_dtoa_tsd.c \
src/jv_dtoa_tsd.c src/uuid.c \
${LIBJQ_INCS}

### C build options

AM_CFLAGS = -Wextra -Wall -Wno-unused-parameter -Wno-unused-function \
-Woverlength-strings
-Woverlength-strings \
-Wno-cast-function-type -Wno-error=cast-function-type \
-Wimplicit-fallthrough=0

if WIN32
AM_CFLAGS += -municode
Expand Down
29 changes: 29 additions & 0 deletions src/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ void *alloca (size_t);
#endif
#include <string.h>
#include <time.h>
#include <uuid.h>
#ifdef WIN32
#include <windows.h>
#endif
Expand Down Expand Up @@ -513,6 +514,22 @@ static jv f_tostring(jq_state *jq, jv input) {
}
}

static jv f_toarray(jq_state *jq, jv input) {
if (jv_get_kind(input) == JV_KIND_ARRAY) {
return input;
} else {
return jv_array_set(jv_array(), 0, input);
}
}

static jv f_toobject(jq_state *jq, jv input) {
if (jv_get_kind(input) == JV_KIND_OBJECT) {
return input;
} else {
return jv_object_set(jv_object(), jv_string("value"), input);
}
}

static jv f_utf8bytelength(jq_state *jq, jv input) {
if (jv_get_kind(input) != JV_KIND_STRING)
return type_error(input, "only strings have UTF-8 byte length");
Expand Down Expand Up @@ -1727,6 +1744,15 @@ static jv f_now(jq_state *jq, jv a) {
}
#endif

static jv f_getuuid(jq_state *jq, jv a) {
jv_free(a);

char* uuid_value = get_uuid();
jv ret = jv_string(uuid_value);
free(uuid_value);
return ret;
}

static jv f_current_filename(jq_state *jq, jv a) {
jv_free(a);

Expand Down Expand Up @@ -1774,6 +1800,8 @@ BINOPS
{f_json_parse, "fromjson", 1},
{f_tonumber, "tonumber", 1},
{f_tostring, "tostring", 1},
{f_toarray, "toarray", 1},
{f_toobject, "toobject", 1},
{f_keys, "keys", 1},
{f_keys_unsorted, "keys_unsorted", 1},
{f_startswith, "startswith", 2},
Expand Down Expand Up @@ -1829,6 +1857,7 @@ BINOPS
{f_current_line, "input_line_number", 1},
{f_have_decnum, "have_decnum", 1},
{f_have_decnum, "have_literal_numbers", 1},
{f_getuuid, "getuuid", 1},
};
#undef LIBM_DDDD_NO
#undef LIBM_DDD_NO
Expand Down
29 changes: 29 additions & 0 deletions src/builtin.jq
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,32 @@ def JOIN($idx; stream; idx_expr; join_expr):
stream | [., $idx[idx_expr]] | join_expr;
def IN(s): any(s == .; .);
def IN(src; s): any(src == s; .);

# add a uuid to be the id for each object in the array
def add_uuids(idName):
if (idName | type != "string") then
error("add_uuids/1 expected idName to be a string")
else
toarray |
map(
toobject |
.[idName]|=(getuuid)
)
end
;
def add_uuids: add_uuids("uuid");

# add an integer to be the id for each object in the array
def add_ids(idName):
if (idName | type != "string") then
error("add_ids/1 expected idName to be a string")
else
toarray |
map(toobject) |
to_entries |
map(
.value + { (idName): .key }
)
end
;
def add_ids: add_ids("id");
Loading
Loading