@@ -31,70 +31,110 @@ bash_object.traverse-set() {
31
31
32
32
bash_object.trace_loop
33
33
34
- # If 'key' is not a member of object, create the object, and set it
34
+ # If 'key' is not a member of object or index of array, error
35
35
if [ -z " ${current_object["$key"]+x} " ]; then
36
- # If we are before the last element in the query, then set
36
+ # If we are before the last element in the query, then error
37
37
if (( i+ 1 < ${# REPLIES[@]} )) ; then
38
- # The variable is 'new_current_object_name', but it also could
39
- # be the name of a new _array_
40
- local new_current_object_name=" __bash_object_${root_object_name} _tree_${key} _${RANDOM} _${RANDOM} _${RANDOM} _${RANDOM} _${RANDOM} "
41
-
42
- # TODO: double-check if new_current_object_name only has underscores, dots, etc. (printf %q?)
43
- if ! eval " declare -gA $new_current_object_name =()" ; then
44
- printf ' %s\n' ' Error: bash-object: eval declare failed'
45
- return 1
46
- fi
47
-
48
- current_object[" $key " ]=$' \x1C\x1D ' " type=object;&$new_current_object_name "
49
-
50
- current_object_name=" $new_current_object_name "
51
- # shellcheck disable=SC2178
52
- local -n current_object=" $new_current_object_name "
38
+ echo " could not traverse property does not exist"
39
+ return 2
40
+ # # The variable is 'new_current_object_name', but it also could
41
+ # # be the name of a new _array_
42
+ # local new_current_object_name="__bash_object_${root_object_name}_tree_${key}_${RANDOM}_${RANDOM}_${RANDOM}_${RANDOM}_${RANDOM}"
43
+
44
+ # # TODO: double-check if new_current_object_name only has underscores, dots, etc. (printf %q?)
45
+ # if ! eval "declare -gA $new_current_object_name=()"; then
46
+ # printf '%s\n' 'Error: bash-object: eval declare failed'
47
+ # return 1
48
+ # fi
49
+
50
+ # current_object["$key"]=$'\x1C\x1D'"type=object;&$new_current_object_name"
51
+
52
+ # current_object_name="$new_current_object_name"
53
+ # # shellcheck disable=SC2178
54
+ # local -n current_object="$new_current_object_name"
53
55
# If we are at the last element in the query
54
56
elif (( i+ 1 == ${# REPLIES[@]} )) ; then
55
- # TODO: object, arrays
56
- current_object[" $key " ]=" $final_value "
57
+ if [ " $final_value_type " = object ]; then
58
+ # TODO: late bash srandom
59
+ local new_current_object_name=" __bash_object_${root_object_name} _tree_${key} _${RANDOM} _${RANDOM} _${RANDOM} _${RANDOM} _${RANDOM} "
60
+
61
+ # TODO: test if name is already used
62
+
63
+ # TODO: double-check if new_current_object_name only has underscores, dots, etc. (printf %q?)
64
+
65
+ # 1. Create object
66
+ if ! eval " declare -gA $new_current_object_name =()" ; then
67
+ printf ' %s\n' ' Error: bash-object: eval declare failed'
68
+ return 1
69
+ fi
70
+
71
+ # 2. Create virtual object
72
+ current_object[" $key " ]=$' \x1C\x1D ' " type=object;&$new_current_object_name "
73
+
74
+ local -n new_current_object=" $new_current_object_name "
75
+ local -n object_to_copy=" $final_value "
76
+ for key in " ${! new_current_object[@]} " ; do
77
+ new_current_object[" $key " ]=" ${object_to_copy["$key"]} "
78
+ done
79
+ elif [ " $final_value_type " = array ]; then
80
+ # local new_current_object_name="__bash_object_${root_object_name}_tree_${key}_${RANDOM}_${RANDOM}_${RANDOM}_${RANDOM}_${RANDOM}"
81
+ # current_object["$key"]=$'\x1C\x1D'"type=array;&$new_current_object_name"
82
+
83
+ # local -n new_array = "$final_value"
84
+ :
85
+ # current_object["$key"]=("${new_array[@]}")
86
+ elif [ " $final_value_type " = string ]; then
87
+ current_object[" $key " ]=" $final_value "
88
+ else
89
+ bash_object.util.traverse_fail ' ERROR_INTERNAL_INVALID_PARAM' " final_value_type: $final_value_type "
90
+ return
91
+ fi
57
92
fi
58
- # If 'key' is already a member of object, use it if it's a virtual object. If
59
- # it's not a virtual object, then a throw an error
93
+ # If 'key' is already a member of object or index of array
60
94
else
61
- if (( i+ 1 < ${# REPLIES[@]} )) ; then
62
- local key_value=" ${current_object["$key"]} "
63
-
64
- if [ " ${key_value:: 2} " = $' \x1C\x1D ' ]; then
65
- virtual_item=" ${key_value# ??} "
95
+ local key_value=" ${current_object["$key"]} "
66
96
67
- bash_object.parse_virtual_object " $virtual_item "
68
- local current_object_name=" $REPLY1 "
69
- local vmd_dtype=" $REPLY2 "
97
+ # If 'key_value' is a virtual object, dereference it
98
+ if [ " ${key_value:: 2} " = $' \x1C\x1D ' ]; then
99
+ if [ -n " ${TRACE_BASH_OBJECT_TRAVERSE+x} " ]; then
100
+ stdtrace.log 2 " BLOCK: OBJECT/ARRAY"
101
+ fi
70
102
71
- local -n current_object=" $current_object_name "
103
+ virtual_item=" ${key_value# ??} "
104
+ bash_object.parse_virtual_object " $virtual_item "
105
+ local current_object_name=" $REPLY1 "
106
+ local vmd_dtype=" $REPLY2 "
107
+ local -n current_object=" $current_object_name "
72
108
73
- # Get the next value (number, string), and construct the next
74
- # element accordingly
109
+ if (( i+ 1 < ${# REPLIES[@]} )) ; then
110
+ # TODO: test these internal invalid errors (error when type=array references object, etc.)?
111
+ :
112
+ elif (( i+ 1 == ${# REPLIES[@]} )) ; then
75
113
case " $vmd_dtype " in
76
114
object)
115
+ bash_object.util.traverse_fail ' ERROR_VALUE_INCORRECT_TYPE' ' Was going to set-string, but found existing object'
116
+ return
117
+ ;;
118
+ array)
119
+ bash_object.util.traverse_fail ' ERROR_VALUE_INCORRECT_TYPE' ' Was going to set-string, but found existing array'
120
+ return
121
+ ;;
122
+ * )
123
+ bash_object.util.traverse_fail ' ERROR_INTERNAL_INVALID_VOBJ' " vmd_dtype: $vmd_dtype "
124
+ return
77
125
;;
78
- array) ;;
79
126
esac
80
- else
81
- # TODO: throw error
82
- echo " phi" >&3
83
- # return 1
84
127
fi
85
- :
86
- elif (( i+ 1 == ${# REPLIES[@]} )) ; then
87
- local key_value=" ${current_object["$key"]} "
88
-
89
- if [ " ${key_value:: 2} " = $' \x1C\x1D ' ]; then
90
- virtual_item=" ${key_value# ??} "
91
-
92
- bash_object.parse_virtual_object " $virtual_item "
93
- local current_object_name=" $REPLY1 "
94
- local vmd_dtype=" $REPLY2 "
95
-
96
- local -n current_object=" $current_object_name "
128
+ # Otherwise, 'key_value' is a string
129
+ else
130
+ if [ -n " ${TRACE_BASH_OBJECT_TRAVERSE+x} " ]; then
131
+ stdtrace.log 2 " BLOCK: STRING"
132
+ fi
97
133
134
+ if (( i+ 1 < ${# REPLIES[@]} )) ; then
135
+ echo " omicron" >&3
136
+ :
137
+ elif (( i+ 1 == ${# REPLIES[@]} )) ; then
98
138
if [ " $final_value_type " = object ]; then
99
139
case " $vmd_dtype " in
100
140
object)
@@ -124,22 +164,13 @@ bash_object.traverse-set() {
124
164
esac
125
165
fi
126
166
current_object[" $key " ]=" $final_value "
127
- else
128
- # TODO: throw error
129
- echo " omicron" >&3
130
- return 1
131
167
fi
132
168
fi
133
169
fi
134
170
171
+ bash_object.trace_current_object
135
172
if [ -n " ${TRACE_BASH_OBJECT_TRAVERSE+x} " ]; then
136
- stdtrace.log 1 ' AFTER OPERATION'
137
- stdtrace.log 1 " current_object_name: '$current_object_name '"
138
- stdtrace.log 1 " current_object=("
139
- for debug_key in " ${! current_object[@]} " ; do
140
- stdtrace.log 1 " [$debug_key ]='${current_object["$debug_key"]} '"
141
- done
142
- stdtrace.log 1 " )"
173
+ stdtrace.log 0 " END BLOCK"
143
174
fi
144
175
done
145
176
}
0 commit comments