Skip to content

Commit d344626

Browse files
committed
refactor: Rename 'filter' to 'querytree'
1 parent 0858db2 commit d344626

File tree

9 files changed

+92
-92
lines changed

9 files changed

+92
-92
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@ bpm install
5959
- clean up argument parsing?
6060
- do something with 'is_index_of_array?' (do not allow for using index notation to get keys of objects)
6161
- set element of the same type that already exists (--overwrite?)
62-
- filter => query
62+
- querytree => query

docs/query.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Query
22

3-
Queries are modeled from `jq`'s filter mechanism. The featureset compared to `jq` is significantly less
3+
Queries are modeled from `jq`'s querytree mechanism. The featureset compared to `jq` is significantly less
44

55
Both quering modes listed below are are mutually exclusive. By default, the 'simple' method is used by defualt; only if your query contains a `[` will the 'advanced' method be used
66

pkg/lib/parse.sh

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# object / array access
55
# @exitcode 1 Miscellaneous error
66
# @exitcode 2 Parsing error
7-
bash_object.parse_filter() {
7+
bash_object.parse_querytree() {
88
declare -ga REPLIES=()
99

1010
local flag_parser_type=
@@ -18,16 +18,16 @@ bash_object.parse_filter() {
1818
shift ;;
1919
esac done
2020

21-
local filter="$1"
21+
local querytree="$1"
2222

2323
if [ "$flag_parser_type" = 'simple' ]; then
24-
if [ "${filter::1}" != . ]; then
25-
bash_object.util.die 'ERROR_FILTER_INVALID' 'Filter must begin with a dot'
24+
if [ "${querytree::1}" != . ]; then
25+
bash_object.util.die 'ERROR_QUERYTREE_INVALID' 'Querytree must begin with a dot'
2626
return
2727
fi
2828

2929
local old_ifs="$IFS"; IFS=.
30-
for key in $filter; do
30+
for key in $querytree; do
3131
if [ -z "$key" ]; then
3232
continue
3333
fi
@@ -42,7 +42,7 @@ bash_object.parse_filter() {
4242

4343
# Append dot so parsing does not fail at end
4444
# This makes parsing a lot easier, since it always expects a dot after a ']'
45-
filter="${filter}."
45+
querytree="${querytree}."
4646

4747
# Reply represents an accessor (e.g. 'sub_key')
4848
local reply=
@@ -59,15 +59,15 @@ bash_object.parse_filter() {
5959
if [ "$char" = . ]; then
6060
mode='MODE_EXPECTING_BRACKET'
6161
else
62-
bash_object.util.die 'ERROR_FILTER_INVALID' 'Filter must begin with a dot'
62+
bash_object.util.die 'ERROR_QUERYTREE_INVALID' 'Querytree must begin with a dot'
6363
return
6464
fi
6565
;;
6666
MODE_BEFORE_DOT)
6767
if [ "$char" = . ]; then
6868
mode='MODE_EXPECTING_BRACKET'
6969
else
70-
bash_object.util.die 'ERROR_FILTER_INVALID' 'Each part in a filter must be deliminated by a dot'
70+
bash_object.util.die 'ERROR_QUERYTREE_INVALID' 'Each part in a querytree must be deliminated by a dot'
7171
return
7272
fi
7373
;;
@@ -77,7 +77,7 @@ bash_object.parse_filter() {
7777
elif [ "$char" = $'\n' ]; then
7878
return
7979
else
80-
bash_object.util.die 'ERROR_FILTER_INVALID' 'A dot MUST be followed by an opening bracket in this mode'
80+
bash_object.util.die 'ERROR_QUERYTREE_INVALID' 'A dot MUST be followed by an opening bracket in this mode'
8181
return
8282
fi
8383
;;
@@ -87,7 +87,7 @@ bash_object.parse_filter() {
8787
if [ "$char" = \" ]; then
8888
mode='MODE_EXPECTING_STRING'
8989
elif [ "$char" = ']' ]; then
90-
bash_object.util.die 'ERROR_FILTER_INVALID' 'Key cannot be empty'
90+
bash_object.util.die 'ERROR_QUERYTREE_INVALID' 'Key cannot be empty'
9191
return
9292
else
9393
case "$char" in
@@ -96,7 +96,7 @@ bash_object.parse_filter() {
9696
mode='MODE_EXPECTING_READ_NUMBER'
9797
;;
9898
*)
99-
bash_object.util.die 'ERROR_FILTER_INVALID' 'A number or opening quote must follow an open bracket'
99+
bash_object.util.die 'ERROR_QUERYTREE_INVALID' 'A number or opening quote must follow an open bracket'
100100
return
101101
;;
102102
esac
@@ -107,14 +107,14 @@ bash_object.parse_filter() {
107107
mode='MODE_STRING_ESCAPE_SEQUENCE'
108108
elif [ "$char" = \" ]; then
109109
if [ -z "$reply" ]; then
110-
bash_object.util.die 'ERROR_FILTER_INVALID' 'Key cannot be empty'
110+
bash_object.util.die 'ERROR_QUERYTREE_INVALID' 'Key cannot be empty'
111111
return
112112
fi
113113

114114
REPLIES+=("$reply")
115115
mode='MODE_EXPECTING_CLOSING_BRACKET'
116116
elif [ "$char" = $'\n' ]; then
117-
bash_object.util.die 'ERROR_FILTER_INVALID' 'Filter is not complete'
117+
bash_object.util.die 'ERROR_QUERYTREE_INVALID' 'Querytree is not complete'
118118
return
119119
else
120120
reply+="$char"
@@ -126,7 +126,7 @@ bash_object.parse_filter() {
126126
\") reply+=\" ;;
127127
']') reply+=']' ;;
128128
*)
129-
bash_object.util.die 'ERROR_FILTER_INVALID' "Escape sequence of '$char' not valid"
129+
bash_object.util.die 'ERROR_QUERYTREE_INVALID' "Escape sequence of '$char' not valid"
130130
return
131131
;;
132132
esac
@@ -142,7 +142,7 @@ bash_object.parse_filter() {
142142
reply+="$char"
143143
;;
144144
*)
145-
bash_object.util.die 'ERROR_FILTER_INVALID' "Expecting number, found '$char'"
145+
bash_object.util.die 'ERROR_QUERYTREE_INVALID' "Expecting number, found '$char'"
146146
return
147147
;;
148148
esac
@@ -152,12 +152,12 @@ bash_object.parse_filter() {
152152
if [ "$char" = ']' ]; then
153153
mode='MODE_BEFORE_DOT'
154154
else
155-
bash_object.util.die 'ERROR_FILTER_INVALID' 'Expected a closing bracket after the closing quotation mark'
155+
bash_object.util.die 'ERROR_QUERYTREE_INVALID' 'Expected a closing bracket after the closing quotation mark'
156156
return
157157
fi
158158
;;
159159
esac
160-
done <<< "$filter"
160+
done <<< "$querytree"
161161
else
162162
bash_object.util.die 'ERROR_ARGUMENTS_INVALID' "Must pass either '--simple' or '--advanced'"
163163
return

pkg/lib/traverse-get.sh

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,19 @@ bash_object.traverse-get() {
6565

6666
local final_value_type="${args[0]}"
6767
local root_object_name="${args[1]}"
68-
local filter="${args[2]}"
68+
local querytree="${args[2]}"
6969

7070
# Start traversing at the root object
7171
local current_object_name="$root_object_name"
7272
local -n current_object="$root_object_name"
7373

74-
# A stack of all the evaluated filter elements
75-
local -a filter_stack=()
74+
# A stack of all the evaluated querytree elements
75+
local -a querytree_stack=()
7676

77-
# Parse the filter, and recurse over their elements
78-
case "$filter" in
79-
*']'*) bash_object.parse_filter --advanced "$filter" ;;
80-
*) bash_object.parse_filter --simple "$filter" ;;
77+
# Parse the querytree, and recurse over their elements
78+
case "$querytree" in
79+
*']'*) bash_object.parse_querytree --advanced "$querytree" ;;
80+
*) bash_object.parse_querytree --simple "$querytree" ;;
8181
esac
8282
for ((i=0; i<${#REPLIES[@]}; i++)); do
8383
local key="${REPLIES[$i]}"
@@ -88,15 +88,15 @@ bash_object.traverse-get() {
8888
is_index_of_array='yes'
8989
fi
9090

91-
filter_stack+=("$key")
92-
# bash_object.util.generate_filter_stack_string
93-
# local filter_stack_string="$REPLY"
91+
querytree_stack+=("$key")
92+
# bash_object.util.generate_querytree_stack_string
93+
# local querytree_stack_string="$REPLY"
9494

9595
bash_object.trace_loop
9696

9797
# If 'key' is not a member of object or index of array, error
9898
if [ -z "${current_object["$key"]+x}" ]; then
99-
bash_object.util.die 'ERROR_NOT_FOUND' "Key or index '$key' (filter index '$i') does not exist"
99+
bash_object.util.die 'ERROR_NOT_FOUND' "Key or index '$key' (querytree index '$i') does not exist"
100100
return
101101
# If 'key' is a member of an object or index of array
102102
else
@@ -224,7 +224,7 @@ bash_object.traverse-get() {
224224
fi
225225

226226
if ((i+1 < ${#REPLIES[@]})); then
227-
bash_object.util.die 'ERROR_NOT_FOUND' "The passed filter implies that '$key' accesses an object or array, but a string with a value of '$key_value' was found instead"
227+
bash_object.util.die 'ERROR_NOT_FOUND' "The passed querytree implies that '$key' accesses an object or array, but a string with a value of '$key_value' was found instead"
228228
return
229229
elif ((i+1 == ${#REPLIES[@]})); then
230230
local value="${current_object["$key"]}"

pkg/lib/traverse-set.sh

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ bash_object.traverse-set() {
4141
bash_object.util.die 'ERROR_ARGUMENTS_INVALID' "--value not implemented"
4242
fi
4343

44-
local final_value_type root_object_name filter final_value
44+
local final_value_type root_object_name querytree final_value
4545
# for compat with 'set -u'
4646
if [ -n "${args[0]+x}" ]; then
4747
final_value_type="${args[0]}"
@@ -50,7 +50,7 @@ bash_object.traverse-set() {
5050
root_object_name="${args[1]}"
5151
fi
5252
if [ -n "${args[2]+x}" ]; then
53-
filter="${args[2]}"
53+
querytree="${args[2]}"
5454
fi
5555
if [ -n "${args[3]+x}" ]; then
5656
final_value="${args[3]}"
@@ -75,7 +75,7 @@ bash_object.traverse-set() {
7575
bash_object.util.die 'ERROR_ARGUMENTS_INVALID' "Positional parameter '2' is empty. Please check passed parameters"
7676
return
7777
fi
78-
if [ -z "$filter" ]; then
78+
if [ -z "$querytree" ]; then
7979
bash_object.util.die 'ERROR_ARGUMENTS_INVALID' "Positional parameter '3' is empty. Please check passed parameters"
8080
return
8181
fi
@@ -141,13 +141,13 @@ bash_object.traverse-set() {
141141
local current_object_name="$root_object_name"
142142
local -n current_object="$root_object_name"
143143

144-
# A stack of all the evaluated filter elements
145-
local -a filter_stack=()
144+
# A stack of all the evaluated querytree elements
145+
local -a querytree_stack=()
146146

147-
# Parse the filter, and recurse over their elements
148-
case "$filter" in
149-
*']'*) bash_object.parse_filter --advanced "$filter" ;;
150-
*) bash_object.parse_filter --simple "$filter" ;;
147+
# Parse the querytree, and recurse over their elements
148+
case "$querytree" in
149+
*']'*) bash_object.parse_querytree --advanced "$querytree" ;;
150+
*) bash_object.parse_querytree --simple "$querytree" ;;
151151
esac
152152
for ((i=0; i<${#REPLIES[@]}; i++)); do
153153
local key="${REPLIES[$i]}"
@@ -158,22 +158,22 @@ bash_object.traverse-set() {
158158
is_index_of_array='yes'
159159
fi
160160

161-
filter_stack+=("$key")
162-
bash_object.util.generate_filter_stack_string
163-
local filter_stack_string="$REPLY"
161+
querytree_stack+=("$key")
162+
bash_object.util.generate_querytree_stack_string
163+
local querytree_stack_string="$REPLY"
164164

165165
bash_object.trace_loop
166166

167167
# If 'key' is not a member of object or index of array, error
168168
if [ -z "${current_object["$key"]+x}" ]; then
169169
# If we are before the last element in the query, then error
170170
if ((i+1 < ${#REPLIES[@]})); then
171-
bash_object.util.die 'ERROR_NOT_FOUND' "Key or index '$key' (filter index '$i') does not exist"
171+
bash_object.util.die 'ERROR_NOT_FOUND' "Key or index '$key' (querytree index '$i') does not exist"
172172
return
173173
# If we are at the last element in the query, and it doesn't exist, create it
174174
elif ((i+1 == ${#REPLIES[@]})); then
175175
if [ "$final_value_type" = object ]; then
176-
bash_object.util.generate_vobject_name "$root_object_name" "$filter_stack_string"
176+
bash_object.util.generate_vobject_name "$root_object_name" "$querytree_stack_string"
177177
local global_object_name="$REPLY"
178178

179179
if bash_object.ensure.variable_does_not_exist "$global_object_name"; then :; else
@@ -195,7 +195,7 @@ bash_object.traverse-set() {
195195
global_object["$key"]="${object_to_copy_from["$key"]}"
196196
done
197197
elif [ "$final_value_type" = array ]; then
198-
bash_object.util.generate_vobject_name "$root_object_name" "$filter_stack_string"
198+
bash_object.util.generate_vobject_name "$root_object_name" "$querytree_stack_string"
199199
local global_array_name="$REPLY"
200200

201201
if bash_object.ensure.variable_does_not_exist "$global_array_name"; then :; else
@@ -337,7 +337,7 @@ bash_object.traverse-set() {
337337
fi
338338

339339
if ((i+1 < ${#REPLIES[@]})); then
340-
bash_object.util.die 'ERROR_NOT_FOUND' "The passed filter implies that '$key' accesses an object or array, but a string with a value of '$key_value' was found instead"
340+
bash_object.util.die 'ERROR_NOT_FOUND' "The passed querytree implies that '$key' accesses an object or array, but a string with a value of '$key_value' was found instead"
341341
return
342342
elif ((i+1 == ${#REPLIES[@]})); then
343343
if [ "$final_value_type" = object ]; then

pkg/lib/util/util.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ declare -gA ERRORS_BASH_OBJECT=(
1010
[ERROR_ARGUMENTS_INVALID_TYPE]="The type of the final value specified by the user is neither 'object', 'array', nor 'string'"
1111
[ERROR_ARGUMENTS_INCORRECT_TYPE]="The type of the final value does not match that of the actual final value (at end of query string)"
1212

13-
[ERROR_FILTER_INVALID]="The filter could not be parsed"
14-
[ERROR_FILTER_INCORRECT_TYPE]=
13+
[ERROR_QUERYTREE_INVALID]="The querytree could not be parsed"
14+
[ERROR_QUERYTREE_INCORRECT_TYPE]=
1515

1616
[ERROR_VOBJ_INVALID_TYPE]="The type of the virtual object is neither 'object' nor 'array'"
1717
[ERROR_VOBJ_INCORRECT_TYPE]="The type of the virtual object does not match with the type of the variable it references"
@@ -23,8 +23,8 @@ bash_object.util.die() {
2323

2424
local error_output=
2525
case "$error_key" in
26-
ERROR_FILTER_INVALID)
27-
printf -v error_output 'Failed to parse filter:
26+
ERROR_QUERYTREE_INVALID)
27+
printf -v error_output 'Failed to parse querytree:
2828
-> code: %s
2929
-> context: %s
3030
-> PARSER_COLUMN_NUMBER: %s
@@ -58,15 +58,15 @@ bash_object.util.generate_vobject_name() {
5858
printf -v REPLY '%q' "__bash_object_${root_object_name}_${root_object_query}_${random_string}"
5959
}
6060

61-
# @description A stringified version of the filter
61+
# @description A stringified version of the querytree
6262
# stack. This is used when generating objects to prevent
6363
# conflicts
64-
bash_object.util.generate_filter_stack_string() {
64+
bash_object.util.generate_querytree_stack_string() {
6565
unset REPLY; REPLY=
6666

6767
local oldIFS="$IFS"
6868
IFS='_'
69-
REPLY="${filter_stack[*]}"
69+
REPLY="${querytree_stack[*]}"
7070
IFS="$oldIFS"
7171
}
7272

0 commit comments

Comments
 (0)