Skip to content

Commit cb67978

Browse files
committed
Otimização na serialização e inferência de valores JSON.
1 parent 89ccb90 commit cb67978

1 file changed

Lines changed: 38 additions & 21 deletions

File tree

ShellBot.sh

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,15 @@ declare _VAR_INIT_
128128

129129
Json() { local obj=$(jq -Mc "$1" <<< "${*:2}"); obj=${obj#\"}; echo "${obj%\"}"; }
130130

131-
GetAllValues(){
131+
SetDelmValues(){
132132
local obj=$(jq "[..|select(type == \"string\" or type == \"number\" or type == \"boolean\")|tostring]|join(\"${_BOT_DELM_/\"/\\\"}\")" <<< $*)
133133
obj=${obj#\"}; echo "${obj%\"}"
134134
}
135135

136+
GetAllValues(){
137+
jq '[..|select(type == "string" or type == "number" or type == "boolean")|tostring]|.[]' <<< $*
138+
}
139+
136140
GetAllKeys(){
137141
jq -r 'path(..|select(type == "string" or type == "number" or type == "boolean"))|map(if type == "number" then .|tostring|"["+.+"]" else . end)|join(".")|gsub("\\.\\[";"[")' <<< $*
138142
}
@@ -345,7 +349,7 @@ CreateLog()
345349
fmt=${fmt//\{MESSAGE_TEXT\}/${mtext:--}}
346350
fmt=${fmt//\{ENTITIES_TYPE\}/${etype:--}}
347351
fmt=${fmt//\{METHOD\}/${FUNCNAME[2]/main/ShellBot.getUpdates}}
348-
fmt=${fmt//\{RETURN\}/$(GetAllValues ${*:2})}
352+
fmt=${fmt//\{RETURN\}/$(SetDelmValues ${*:2})}
349353

350354
exec 2<&5
351355

@@ -365,17 +369,23 @@ MethodReturn()
365369
# Retorno
366370
case $_BOT_TYPE_RETURN_ in
367371
json) echo "$*";;
368-
value) GetAllValues $*;;
372+
value) SetDelmValues $*;;
369373
map)
370-
local key val obj
374+
local key val vars vals i obj
371375
return=()
372376

373-
for obj in $(GetAllKeys $*); do
374-
key=${obj//[0-9\[\]]/}
377+
mapfile -t vars <<< $(GetAllKeys $*)
378+
mapfile -t vals <<< $(GetAllValues $*)
379+
380+
for i in ${!vars[@]}; do
381+
382+
key=${vars[$i]//[0-9\[\]]/}
375383
key=${key#result.}
376384
key=${key//./_}
377385

378-
val=$(Json ".$obj" $*)
386+
val=${vals[$i]}
387+
val=${val#\"}
388+
val=${val%\"}
379389

380390
[[ ${return[$key]} ]] && return[$key]+=${_BOT_DELM_}${val} || return[$key]=$val
381391
[[ $_BOT_MONITOR_ ]] && printf "[%s]: return[%s] = '%s'\n" "${FUNCNAME[1]}" "$key" "$val"
@@ -5708,18 +5718,18 @@ _EOF
57085718
ShellBot.getUpdates()
57095719
{
57105720
local total_keys offset limit timeout allowed_updates jq_obj
5711-
local vet val var obj oldv bar
5721+
local vet val var obj oldv bar vars vals i
57125722

5713-
# Define os parâmetros da função
5714-
local param=$(getopt --name "$FUNCNAME" \
5715-
--options 'o:l:t:a:' \
5716-
--longoptions 'offset:,
5717-
limit:,
5718-
timeout:,
5719-
allowed_updates:' \
5720-
-- "$@")
5723+
# Define os parâmetros da função
5724+
local param=$(getopt --name "$FUNCNAME" \
5725+
--options 'o:l:t:a:' \
5726+
--longoptions 'offset:,
5727+
limit:,
5728+
timeout:,
5729+
allowed_updates:' \
5730+
-- "$@")
57215731

5722-
eval set -- "$param"
5732+
eval set -- "$param"
57235733

57245734
while :
57255735
do
@@ -5779,20 +5789,26 @@ _EOF
57795789
"${_BOT_INFO_[3]}" \
57805790
"${_BOT_INFO_[1]}"
57815791
fi
5792+
5793+
mapfile -t vars <<< $(GetAllKeys $jq_obj)
5794+
mapfile -t vals <<< $(GetAllValues $jq_obj)
57825795

5783-
for obj in $(GetAllKeys $jq_obj); do
5796+
for i in ${!vars[@]}; do
57845797

5785-
[[ $obj =~ [0-9]+ ]]
5798+
[[ ${vars[$i]} =~ [0-9]+ ]]
57865799
vet=${BASH_REMATCH:-0}
57875800

5788-
var=${obj//[0-9\[\]]/}
5801+
var=${vars[$i]//[0-9\[\]]/}
57895802
var=${var#result.}
57905803
var=${var//./_}
57915804

57925805
declare -g $var
57935806
local -n byref=$var # ponteiro
57945807

5795-
val=$(Json ".$obj" $jq_obj)
5808+
val=${vals[$i]}
5809+
val=${val#\"}
5810+
val=${val%\"}
5811+
57965812
byref[$vet]+=${byref[$vet]:+$_BOT_DELM_}${val}
57975813

57985814
if [[ $_BOT_MONITOR_ ]]; then
@@ -5922,6 +5938,7 @@ readonly -f MessageError \
59225938
CreateUnitService \
59235939
GetAllKeys \
59245940
GetAllValues \
5941+
SetDelmValues \
59255942
MethodReturn \
59265943
CheckArgType \
59275944
CreateLog \

0 commit comments

Comments
 (0)