File: mysqltest_json.test

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (246 lines) | stat: -rw-r--r-- 7,308 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# ==== Purpose ====
#
# This is intended both a functional test and a usage example for the
# framework to process JSON specifications in mtr. It currently covers
# three scripts:
#
#   include/create_json_iterator.inc
#   include/create_json_unpacker.inc
#   include/create_json_unpacking_iterator.inc
#
# See comments in those files for the full usage of each files.

--echo ==== Minimal example of JSON iterator ====

# Construct the iterator
--let $json_label = planet
--source include/create_json_iterator.inc

# Reset the iterator to the first element of this array.
--let $json_array = ["Mercury", "Venus", "Earth", "Mars" ]
--source $json_planet_start
while (!$json_planet_done) {
  # Do something useful with the current value.
  --echo Planet: $json_planet_value
  # Step forward to the next element.
  --source $json_planet_next
}
--source include/destroy_json_functions.inc

--echo ==== Example of nested JSON iterator ====

# Create iterator of outer loop.
--let $json_label = continent
--source include/create_json_iterator.inc
# Create iterator of inner loop.
--let $json_label = country
--source include/create_json_iterator.inc

# Reset the outer iterator to the first element of the outer array.
let $json_array = [
  ["Afghanistan", "Bangladesh", "Cambodia"],
  ["Albania", "Belgium", "Cyprus"],
  ["Argentina", "Bolivia", "Colombia"],
  ["Angola", "Burundi", "Cameroon"]
];
--source $json_continent_start
while (!$json_continent_done) {
  # Print the current inner array.
  --echo These countries belong to the same continent: $json_continent_value
  # Reset the inner iterator to the first element of the current inner array.
  --let $json_array = $json_continent_value
  --source $json_country_start
  while (!$json_country_done) {
    # Do something useful with the current value.
    --echo Country: $json_country_value
    # Step forward to the next element of the inner array.
    --source $json_country_next
  }
  # Step forward to the next element of the outer array.
  --source $json_continent_next
}
--source include/destroy_json_functions.inc

--echo ==== Minimal example of JSON unpacker ====

# Create the unpacker.
--let $json_label = country
--let $json_keys = name, capital
--source include/create_json_unpacker.inc

# Use the unpacker to unpack values from a JSON object.
--let $json_object = { "name": "Madagascar", "capital": "Antananarivo" }
--source $json_country_unpack

# Do something useful with the unpacked values.
--echo $capital is the capital of $name.

# Use the same unpacker to unpack values from another JSON object.
--let $json_object = { "name": "Iceland", "capital": "Reykjavik" }
--source $json_country_unpack

# Do something useful with the unpacked values.
--echo $capital is the capital of $name.

# Clean up the generated files
--source include/destroy_json_functions.inc

--echo ==== Minimal example of unpacking JSON iterator ====

# Create the unpacking iterator.
--let $json_label = book
--let $json_keys = author, title
--source include/create_json_unpacking_iterator.inc

# Reset the iterator to the first element of this array.
let $json_array = [
  {
    "author": "Kerstin Ekman",
    "title": "Grand final i skojarbranschen"
  },
  {
    "author": "Bjarne Stroustrup",
    "title": "The C++ Programming Language"
  },
  {
    "author": "Pelle Holmberg and Hans Marklund",
    "title": "Nya svampboken"
  }
];
--source $json_book_start
while (!$json_book_done) {
  # Do something useful with the unpacked values.
  --echo $author wrote $title
  # Step forward to the next element.
  --source $json_book_next
}

--echo ==== Example of missing keys to unpack ====

# Create an unpacking iterator
--let $json_label = plant
--let $json_keys = name, fruit_color, flower_color
--source include/create_json_unpacking_iterator.inc

# Reset the iterator to the first element of this array.
let $json_array = [
  {
    "name": "Apple",
    "fruit_color": "red",
    "flower_color": "white"
  },
  {
    "name": "Tussilago",
    "flower_color": "yellow"
  },
  {
    "name": "Chanterelle",
    "fruit_color": "yellow"
  }
];
--source $json_plant_start
while (!$json_plant_done) {
  # Do something useful with the unpacked values.
  # Non-existing values are empty.
  if ($fruit_color) {
    --echo $name has $fruit_color fruits.
  }
  if (!$fruit_color) {
    --echo $name has no fruits.
  }
  if ($flower_color) {
    --echo $name has $flower_color flowers.
  }
  if (!$flower_color) {
    --echo $name has no flowers.
  }
  # Step forward to the next plant.
  --source $json_plant_next
}

--echo ==== Example of default values and required keys ====

# Create iterator.
--let $json_label = animal
--let $json_keys = species, legs, color
# Require that every object has a "species" key.
--let $json_required = species
# For objects that don't have a "legs" key, assume legs=4.
--let $json_defaults = {"legs": 4}
--source include/create_json_unpacking_iterator.inc
--let $json_required =
--let $json_defaults =

# Reset the iterator to the first element of this array.
let $json_array = [
  { "species": "ant", "color": "brown", "legs": 6 },
  { "species": "bear", "color": "black" },
  { "species": "cat", "color": "orange" }
];
--source $json_animal_start
while (!$json_animal_done) {
  # Do something useful with the unpacked values.
  --echo The $species has $legs legs and is $color
  # Step forward to the next animal.
  --source $json_animal_next
}

--echo ==== Example of quoting and verbosity ====

# Create the unpacker.
--let $json_label = songs
--let $json_keys = first, second, third, fourth, fifth
--let $json_output_json_quoted = first, second
--let $json_output_single_quoted = first, third
--let $json_verbose = first, second, third, fourth
--source include/create_json_unpacker.inc

let $json_object = {
  "first": "Ain't Talkin'",
  "second": "Don't Think Twice, It's All Right",
  "third": "It Ain't Me, Babe",
  "fourth": "Rollin' and Tumblin'",
  "fifth": "Where are you?"
};
--source $json_songs_unpack

--echo ==== Example of reverse iteration, long steps, and forced position ====

# Create iterator.
--let $json_label = vegetable
--source include/create_json_iterator.inc

--echo # Reverse alphabetic vegetables
# Reset the iterator to the last element of this array and step backwards.
--let $json_array = ["artichoke", "broccoli", "cauliflower", "durian"]
--let $json_stride = -1
--source $json_vegetable_start
while (!$json_vegetable_done) {
  --echo Vegetable: $json_vegetable_value
  --source $json_vegetable_next
}

--echo # Alphabetic vegetables, even-numbered initial letter
# Reset the iterator to the first element of this array and jump two steps at a time.
--let $json_stride = 2
--source $json_vegetable_start
while (!$json_vegetable_done) {
  --echo Vegetable: $json_vegetable_value
  --source $json_vegetable_next
}

--echo # Alphabetic vegetables, odd-numbered initial letter
# Reset the iterator to the first element of this array, jump two steps at a time.
--let $json_stride = 2
--source $json_vegetable_start
# Jump to the element with index 1 (i.e., broccoli)
--let $json_vegetable_index = 1
--source $json_vegetable_next
while (!$json_vegetable_done) {
  --echo Vegetable: $json_vegetable_value
  --source $json_vegetable_next
}

--echo ==== Clean up ====

--source include/destroy_json_functions.inc