Updated restbuck example and fixed the policy driven variant.

This commit is contained in:
2017-02-16 16:56:47 +01:00
parent ae7aeaea30
commit c160f63ddc

View File

@@ -535,23 +535,134 @@ feature -- Helpers
feature {NONE} -- Conversion feature {NONE} -- Conversion
order_to_json (obj: ORDER): JSON_VALUE order_to_json (obj: ORDER): JSON_OBJECT
local
j_order: JSON_OBJECT
j_item: JSON_OBJECT
ja: JSON_ARRAY
do do
Result := order_serialization.to_json (obj) create Result.make_with_capacity (4)
Result.put_string (obj.id, id_key)
if attached obj.location as loc then
Result.put_string (loc, location_key)
end
Result.put_string (obj.status, status_key)
if attached obj.items as l_items and then not l_items.is_empty then
create ja.make (l_items.count)
Result.put (ja, items_key)
across
l_items as ic
loop
if attached {ORDER_ITEM} ic.item as l_item then
create j_item.make_with_capacity (4)
j_item.put_string (l_item.name, name_key)
j_item.put_string (l_item.size, size_key)
j_item.put_integer (l_item.quantity, quantity_key)
j_item.put_string (l_item.option, option_key)
ja.extend (j_item)
end
end end
order_from_json (jv: JSON_VALUE): detachable ORDER
do
if attached {ORDER} order_serialization.from_json (jv, {ORDER}) as o then
Result := o
end end
end end
order_serialization: JSON_SERIALIZATION order_from_json (a_json: JSON_VALUE): detachable ORDER
local
l_status: detachable STRING_32
q: NATURAL_8
is_valid_from_json: BOOLEAN
l_name, l_size, l_option: detachable READABLE_STRING_32
do do
create Result is_valid_from_json := True
Result.register (create {ORDER_JSON_SERIALIZATION}, {ORDER}) if attached {JSON_OBJECT} a_json as jobj then
-- Either new order (i.e no id and no status)
-- or an existing order with `id` and `status` (could be Void, thus use default).
if attached {JSON_STRING} jobj.item (status_key) as j_status then
l_status := j_status.unescaped_string_32
end end
if
attached {JSON_STRING} jobj.item (id_key) as j_id
then
-- Note: the id has to be valid string 8 value!
create Result.make (j_id.unescaped_string_8, l_status)
elseif attached {JSON_NUMBER} jobj.item (id_key) as j_id then
-- Be flexible and accept json number as id.
create Result.make (j_id.integer_64_item.out, l_status)
else
create Result.make_empty
if l_status /= Void then
Result.set_status (l_status)
end
end
if attached {JSON_STRING} jobj.item (location_key) as j_location then
Result.set_location (j_location.unescaped_string_32)
end
if attached {JSON_ARRAY} jobj.item (items_key) as j_items then
across
j_items as ic
loop
if attached {JSON_OBJECT} ic.item as j_item then
if
attached {JSON_NUMBER} j_item.item (quantity_key) as j_quantity and then
j_quantity.integer_64_item < {NATURAL_8}.Max_value
then
q := j_quantity.integer_64_item.to_natural_8
else
q := 0
end
if
attached {JSON_STRING} j_item.item (name_key) as j_name and then
attached {JSON_STRING} j_item.item (size_key) as j_size and then
attached {JSON_STRING} j_item.item (option_key) as j_option
then
l_name := j_name.unescaped_string_32
l_size := j_size.unescaped_string_32
l_option := j_option.unescaped_string_32
if is_valid_item_customization (l_name, l_size, l_option, q) then
Result.add_item (create {ORDER_ITEM}.make (l_name, l_size, l_option, q))
else
is_valid_from_json := False
end
else
is_valid_from_json := False
end
end
end
end
if not is_valid_from_json or Result.items.is_empty then
Result := Void
end
else
is_valid_from_json := a_json = Void or else attached {JSON_NULL} a_json
Result := Void
end
end
is_valid_item_customization (name: READABLE_STRING_GENERAL; size: READABLE_STRING_GENERAL; option: READABLE_STRING_GENERAL; quantity: NATURAL_8): BOOLEAN
local
ic: ORDER_ITEM_VALIDATION
do
create ic
Result := ic.is_valid_coffee_type (name) and
ic.is_valid_milk_type (option) and
ic.is_valid_size_option (size) and
quantity > 0
end
id_key: STRING = "id"
location_key: STRING = "location"
status_key: STRING = "status"
items_key: STRING = "items"
name_key: STRING = "name"
size_key: STRING = "size"
quantity_key: STRING = "quantity"
option_key: STRING = "option"
note note
copyright: "2011-2017, Javier Velilla and others" copyright: "2011-2017, Javier Velilla and others"