Skip to content
Browse files

Code cleaning (formatting)

Use [i] instead of .at (i)
  • Loading branch information...
1 parent 59ebe65 commit 7b7d3d9077568c8cfdb4370c456e08888ec19b08 @jocelyn jocelyn committed Apr 3, 2012
Showing with 164 additions and 158 deletions.
  1. +54 −57 library/converter/json_hal_resource_converter.e
  2. +18 −0 library/hal-safe.ecf
  3. +3 −3 library/hal.ecf
  4. +15 −12 library/link.e
  5. +9 −4 library/link_attribute.e
  6. +62 −79 test/application.e
  7. +3 −3 test/test_suite.ecf
View
111 library/converter/json_hal_resource_converter.e
@@ -9,25 +9,31 @@ note
class
JSON_HAL_RESOURCE_CONVERTER
+
inherit
JSON_CONVERTER
+
create
make
+
feature -- Initialization
+
make
do
create object.make
end
+
feature -- Access
- object : RESOURCE
- value : detachable JSON_OBJECT
- from_json (j: attached like value): detachable like object
+ object: RESOURCE
+ value: detachable JSON_OBJECT
+
+ from_json (j: attached like value): detachable like object
-- Convert from JSON value. Returns Void if unable to convert
local
- l_list : LIST[like object]
- j_array, l_embedded_keys : ARRAY [JSON_STRING]
- i,k,l : INTEGER
+ l_list: LIST [like object]
+ j_array, l_embedded_keys: ARRAY [JSON_STRING]
+ i,k,l: INTEGER
do
--| TODO: Refactor to make it simpler
create Result.make
@@ -37,9 +43,9 @@ feature -- Access
until
i > j_array.count
loop
- if not (j_array.at (i).is_equal (links_key) or j_array.at (i).is_equal (embedded_key)) then
- if attached j.item (j_array.at (i)) as l_rep then
- Result.add_properties (j_array.at (i).item, l_rep.representation)
+ if not (j_array [i].is_equal (links_key) or j_array [i].is_equal (embedded_key)) then
+ if attached j.item (j_array [i]) as l_rep then
+ Result.add_properties (j_array [i].item, l_rep.representation)
end
end
i := i + 1
@@ -54,14 +60,14 @@ feature -- Access
until
k > l_embedded_keys.count
loop
- if attached {JSON_OBJECT} ll_embedded.item (l_embedded_keys.at (k)) as jo then
- create {ARRAYED_LIST[like object]}l_list.make(1)
+ if attached {JSON_OBJECT} ll_embedded.item (l_embedded_keys [k]) as jo then
+ create {ARRAYED_LIST [like object]} l_list.make(1)
if attached from_json (jo) as jo_r then
l_list.force (jo_r)
- Result.add_embedded(l_embedded_keys.at (k).item, l_list)
+ Result.add_embedded(l_embedded_keys [k].item, l_list)
end
- elseif attached {JSON_ARRAY} ll_embedded.item (l_embedded_keys.at (k)) as jea then
- create {ARRAYED_LIST[like object]}l_list.make(jea.count)
+ elseif attached {JSON_ARRAY} ll_embedded.item (l_embedded_keys [k]) as jea then
+ create {ARRAYED_LIST [like object]} l_list.make (jea.count)
from
l := 1
until
@@ -74,22 +80,18 @@ feature -- Access
end
l := l + 1
end
- Result.add_embedded(l_embedded_keys.at (k).item, l_list)
+ Result.add_embedded(l_embedded_keys [k].item, l_list)
end
- k := k + 1
+ k := k + 1
end
end
end
to_json (o: like object): like value
-- Convert to JSON value
- local
- ja : JSON_ARRAY
- l : LINK
- jv: JSON_OBJECT
do
create Result.make
- Result.put (to_json_links (o.links),links_key)
+ Result.put (to_json_links (o.links), links_key)
if attached o.embedded_resource as l_embedded_resource then
Result.put (to_json_embedded_resource (l_embedded_resource),embedded_key)
end
@@ -105,7 +107,7 @@ feature -- Access
end
end
- to_json_embedded_resource ( an_embedded_resource : HASH_TABLE[LIST[RESOURCE],STRING]) : JSON_OBJECT
+ to_json_embedded_resource ( an_embedded_resource : HASH_TABLE [LIST [RESOURCE], STRING]) : JSON_OBJECT
do
create Result.make
from
@@ -118,9 +120,8 @@ feature -- Access
end
end
- to_json_embedded_resource_internal ( e_resource : LIST[RESOURCE]) : JSON_VALUE
+ to_json_embedded_resource_internal ( e_resource : LIST [RESOURCE]) : JSON_VALUE
local
- l_result_obj : JSON_OBJECT
l_result_arr : JSON_ARRAY
do
if e_resource.count = 1 then
@@ -145,7 +146,7 @@ feature -- Access
end
- to_json_links ( a_links : HASH_TABLE[LINK,STRING]) : JSON_OBJECT
+ to_json_links ( a_links : HASH_TABLE [LINK, STRING]) : JSON_OBJECT
do
create Result.make
from
@@ -160,47 +161,46 @@ feature -- Access
end
- to_json_link_internal ( a_link : LINK) : JSON_VALUE
+ to_json_link_internal (a_link : LINK) : JSON_VALUE
local
- l_result_obj : JSON_OBJECT
l_result_arr : JSON_ARRAY
do
if a_link.attributes.count = 1 then
- Result := to_json_link_attribute (a_link.attributes.at (1))
+ Result := to_json_link_attribute (a_link.attributes.first)
else
create {JSON_ARRAY} l_result_arr.make_array
from
a_link.attributes.start
until
a_link.attributes.after
loop
- l_result_arr.add(to_json_link_attribute(a_link.attributes.item_for_iteration))
+ l_result_arr.add (to_json_link_attribute (a_link.attributes.item_for_iteration))
a_link.attributes.forth
end
Result := l_result_arr
end
end
- to_json_link_attribute ( a_link_attribute : LINK_ATTRIBUTE):JSON_OBJECT
+ to_json_link_attribute ( a_link_attribute : LINK_ATTRIBUTE): JSON_OBJECT
do
create Result.make
Result.put (json.value (a_link_attribute.href), href_key)
if attached a_link_attribute.name as l_name then
Result.put (json.value (a_link_attribute.href), name_key)
end
if attached a_link_attribute.hreflang as l_hreflang then
- Result.put (json.value (a_link_attribute.hreflang),hreflang_key)
+ Result.put (json.value (a_link_attribute.hreflang), hreflang_key)
end
if attached a_link_attribute.title as l_title then
- Result.put (json.value (a_link_attribute.title),title_key)
+ Result.put (json.value (a_link_attribute.title), title_key)
end
end
- from_json_link (j: attached like value) : HASH_TABLE[LINK, STRING]
+ from_json_link (j: attached like value) : HASH_TABLE [LINK, STRING]
local
- l_keys : ARRAY[JSON_STRING]
+ l_keys : ARRAY [JSON_STRING]
i,k : INTEGER
l_link : LINK
do
@@ -211,13 +211,13 @@ feature -- Access
until
i > l_keys.count
loop
- if attached {JSON_OBJECT} j.item (l_keys.at (i)) as jo then
+ if attached {JSON_OBJECT} j.item (l_keys [i]) as jo then
if attached create_link_attributes (jo) as la then
- create l_link.make_with_attribute (l_keys.at (i).item, la)
- Result.force (l_link, l_keys.at (i).item)
+ create l_link.make_with_attribute (l_keys [i].item, la)
+ Result.force (l_link, l_keys [i].item)
end
- elseif attached {JSON_ARRAY} j.item (l_keys.at (i)) as ja then
- create l_link.make (l_keys.at (i).item)
+ elseif attached {JSON_ARRAY} j.item (l_keys [i]) as ja then
+ create l_link.make (l_keys [i].item)
from
k := 1
until
@@ -230,38 +230,33 @@ feature -- Access
end
k := k + 1
end
- Result.force (l_link, l_keys.at (i).item)
+ Result.force (l_link, l_keys [i].item)
end
i := i +1
end
end
- create_link_attributes (j: attached JSON_OBJECT) : detachable LINK_ATTRIBUTE
+ create_link_attributes (j: JSON_OBJECT) : detachable LINK_ATTRIBUTE
-- create an object LINK_ATTRIBUTE from a JSON_OBJECT
- local
- href: STRING
- name : detachable STRING
- title: detachable STRING
- hreflang : detachable STRING
do
--|TODO refactor to make it simpler
- if attached {JSON_STRING} j.item (href_key) as js1 then
- create Result.make (js1.item)
- if attached {JSON_STRING} j.item (name_key) as js2 then
- Result.set_name (js2.item)
+ if attached {JSON_STRING} j.item (href_key) as j_href then
+ create Result.make (j_href.item)
+ if attached {JSON_STRING} j.item (name_key) as j_name then
+ Result.set_name (j_name.item)
end
- if attached {JSON_STRING} j.item (title_key) as js3 then
- Result.set_title (js3.item)
+ if attached {JSON_STRING} j.item (title_key) as j_title then
+ Result.set_title (j_title.item)
end
- if attached {JSON_STRING} j.item (hreflang_key) as js4 then
- Result.set_hreflang (js4.item)
+ if attached {JSON_STRING} j.item (hreflang_key) as j_hreflang then
+ Result.set_hreflang (j_hreflang.item)
end
end
end
- feature {NONE} -- Implementation
--- RESOURCE
+feature {NONE} -- Implementation: RESOURCE
+
links_key: JSON_STRING
once
create Result.make_json ("_links")
@@ -272,7 +267,8 @@ feature -- Access
create Result.make_json ("_embedded")
end
--- LINK LINK_ATTRIBUTE
+feature {NONE} -- Implementation: LINK
+
href_key: JSON_STRING
once
create Result.make_json ("href")
@@ -283,6 +279,7 @@ feature -- Access
create Result.make_json ("ref")
end
+feature {NONE} -- Implementation: LINK_ATTRIBUTE
name_key : JSON_STRING
View
18 library/hal-safe.ecf
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<system xmlns="http://www.eiffel.com/developers/xml/configuration-1-9-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.eiffel.com/developers/xml/configuration-1-9-0 http://www.eiffel.com/developers/xml/configuration-1-9-0.xsd" name="hal" uuid="94C384A6-8CAD-48C2-B448-EFA81264AFFD" library_target="hal">
+ <target name="hal">
+ <root all_classes="true"/>
+ <option warning="true" is_attached_by_default="true" void_safety="all" syntax="transitional">
+ <assertions precondition="true" postcondition="true" check="true" invariant="true" loop="true" supplier_precondition="true"/>
+ </option>
+ <library name="base" location="$ISE_LIBRARY\library\base\base-safe.ecf"/>
+ <library name="json" location="$ISE_LIBRARY\contrib\library\text\parser\json\library\json-safe.ecf"/>
+ <cluster name="hal" location=".\" recursive="true">
+ <file_rule>
+ <exclude>/EIFGENs$</exclude>
+ <exclude>/CVS$</exclude>
+ <exclude>/.svn$</exclude>
+ </file_rule>
+ </cluster>
+ </target>
+</system>
View
6 library/hal.ecf
@@ -2,11 +2,11 @@
<system xmlns="http://www.eiffel.com/developers/xml/configuration-1-9-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.eiffel.com/developers/xml/configuration-1-9-0 http://www.eiffel.com/developers/xml/configuration-1-9-0.xsd" name="hal" uuid="94C384A6-8CAD-48C2-B448-EFA81264AFFD" library_target="hal">
<target name="hal">
<root all_classes="true"/>
- <option warning="true" is_attached_by_default="true" void_safety="all" syntax="transitional">
+ <option warning="true" syntax="transitional">
<assertions precondition="true" postcondition="true" check="true" invariant="true" loop="true" supplier_precondition="true"/>
</option>
- <library name="base" location="$ISE_LIBRARY\library\base\base-safe.ecf"/>
- <library name="json" location="C:\Users\jvelilla\work\languages\Eiffel71\contrib\library\text\parser\json\library\json-safe.ecf"/>
+ <library name="base" location="$ISE_LIBRARY\library\base\base.ecf"/>
+ <library name="json" location="$ISE_LIBRARY\contrib\library\text\parser\json\library\json.ecf"/>
<cluster name="hal" location=".\" recursive="true">
<file_rule>
<exclude>/EIFGENs$</exclude>
View
27 library/link.e
@@ -10,7 +10,7 @@ note
{ "href": "/product/832", "title": "Hover donkey" }
]
}
-
+
]"
author: ""
date: "$Date$"
@@ -25,33 +25,36 @@ create
make_with_list
feature {NONE} -- Initialization
+
make (a_rel : STRING)
do
- set_rel(a_rel)
- create {ARRAYED_LIST[LINK_ATTRIBUTE]} attributes.make(2)
+ set_rel (a_rel)
+ create {ARRAYED_LIST [LINK_ATTRIBUTE]} attributes.make(2)
end
- make_with_attribute (a_rel : STRING; an_attribute : LINK_ATTRIBUTE)
+ make_with_attribute (a_rel: STRING; an_attribute: LINK_ATTRIBUTE)
do
- set_rel(a_rel)
- create {ARRAYED_LIST[LINK_ATTRIBUTE]} attributes.make(1)
+ set_rel (a_rel)
+ create {ARRAYED_LIST [LINK_ATTRIBUTE]} attributes.make (1)
attributes.force (an_attribute)
end
- make_with_list (a_rel : STRING; array_attributes : ARRAY[LINK_ATTRIBUTE])
+ make_with_list (a_rel : STRING; array_attributes : ARRAY [LINK_ATTRIBUTE])
do
- set_rel(a_rel)
- create {ARRAYED_LIST[LINK_ATTRIBUTE]} attributes.make_from_array(array_attributes)
+ set_rel (a_rel)
+ create {ARRAYED_LIST [LINK_ATTRIBUTE]} attributes.make_from_array(array_attributes)
end
-feature -- Access
+feature -- Access
+
rel:STRING
- attributes : LIST [LINK_ATTRIBUTE]
+
+ attributes: LIST [LINK_ATTRIBUTE]
feature -- Element change
- set_rel (a_rel:STRING)
+ set_rel (a_rel: STRING)
-- Set rel with `a_rel'
do
rel := a_rel
View
13 library/link_attribute.e
@@ -6,46 +6,51 @@ note
class
LINK_ATTRIBUTE
+
create
make
+
feature {NONE}-- Initialization
+
make (a_ref : STRING)
do
set_href (a_ref)
end
feature -- Access
+
href: STRING
name : detachable STRING
title: detachable STRING
hreflang : detachable STRING
feature -- Element change
- set_href (a_href:STRING)
+
+ set_href (a_href: STRING)
-- Set href with `a_href'
do
href := a_href
ensure
assigned: href ~ a_href
end
- set_name (a_name:STRING)
+ set_name (a_name: STRING)
-- Set name with `a_name'
do
name := a_name
ensure
assigned: name ~ a_name
end
- set_title (a_title:STRING)
+ set_title (a_title: STRING)
-- Set title with `a_title'
do
title := a_title
ensure
assigned: title ~ a_title
end
- set_hreflang (a_hreflang:STRING)
+ set_hreflang (a_hreflang: STRING)
-- Set hreflang with `a_hreflang'
do
hreflang := a_hreflang
View
141 test/application.e
@@ -8,15 +8,16 @@ class
inherit
SHARED_EJSON
+
create
make
feature {NONE} -- Initialization
make
local
- hlink : LINK
- hresource : RESOURCE
+-- hlink : LINK
+-- hresource : RESOURCE
do
create file_reader
-- test_json_min
@@ -25,82 +26,62 @@ feature {NONE} -- Initialization
test_hal_loco
end
-
-
test_hal_loco
local
- hal : JSON_HAL_RESOURCE_CONVERTER
- parse_json: like new_json_parser
+ hal: JSON_HAL_RESOURCE_CONVERTER
do
create hal.make
- json.add_converter(HAL)
- if attached json_file_from ("hal_example.json") as json_file then
- parse_json := new_json_parser (json_file)
- json_object ?= parse_json.parse_json
- if attached json_object as jo then
- if attached{RESOURCE} json.object (jo, "RESOURCE") as r then
- print (r.out)
- if attached json.value (r) as jv then
- print (jv.representation)
- end
+ json.add_converter (hal)
+ if attached json_file_from ("hal_example.json") as json_file then
+ if attached {JSON_OBJECT} json_value_from_file (json_file) as jo then
+ if attached {RESOURCE} json.object (jo, "RESOURCE") as r then
+ print (r.out)
+ if attached json.value (r) as jv then
+ print (jv.representation)
end
end
end
-
+ end
end
test_json_hal
--
- local
- parse_json: like new_json_parser
do
if attached json_file_from ("hal_example.json") as json_file then
- parse_json := new_json_parser (json_file)
- json_object ?= parse_json.parse_json
- check parse_json.is_parsed = True end
- if attached json_object as jo then
+ if attached {JSON_OBJECT} json_value_from_file (json_file) as jo then
print (jo.representation)
end
end
end
-
test_json_link
--
local
- parse_json: like new_json_parser
- r : HASH_TABLE[LINK, STRING]
+ r : HASH_TABLE [LINK, STRING]
do
if attached json_file_from ("hal_multi_links.json") as json_file then
- parse_json := new_json_parser (json_file)
- json_object ?= parse_json.parse_json
- check parse_json.is_parsed = True end
- if attached {JSON_OBJECT} json_object.item ("_links") as jo then
- r := from_json_link (jo)
- print (r.out)
+ if attached {JSON_OBJECT} json_value_from_file (json_file) as jo then
+ if attached {JSON_OBJECT} jo.item ("_links") as j_links then
+ r := from_json_link (j_links)
+ print (r.out)
+ end
end
end
end
test_json_min
--
- local
- parse_json: like new_json_parser
do
if attached json_file_from ("min_hal.json") as json_file then
- parse_json := new_json_parser (json_file)
- json_object ?= parse_json.parse_json
- check parse_json.is_parsed = True end
- if attached json_object as jo then
+ if attached {JSON_OBJECT} json_value_from_file (json_file) as jo then
print (jo.representation)
end
end
end
feature -- Implementation
- file_reader: JSON_FILE_READER
- json_object: detachable JSON_OBJECT
+ file_reader: JSON_FILE_READER
json_file_from (fn: STRING): detachable STRING
do
@@ -114,49 +95,54 @@ feature -- Implementation
create Result.make_parser (a_string)
end
+ json_value_from_file (json_file: STRING): detachable JSON_VALUE
+ local
+ p: like new_json_parser
+ do
+ p := new_json_parser (json_file)
+ Result := p.parse_json
+ check json_is_parsed: p.is_parsed end
+ end
feature -- Converter Implementation
- from_json_link (j: attached JSON_OBJECT) : HASH_TABLE[LINK, STRING]
+
+ from_json_link (j: JSON_OBJECT): HASH_TABLE [LINK, STRING]
local
- l_keys : ARRAY[JSON_STRING]
- i,k : INTEGER
- l_link : LINK
+ l_keys: ARRAY [JSON_STRING]
+ l_key_i: JSON_STRING
+ i,k: INTEGER
+ l_link: LINK
do
- create Result.make(2)
- from
- i := 1
- l_keys := j.current_keys
- until
- i > l_keys.count
- loop
- if attached {JSON_OBJECT} j.item (l_keys.at (i)) as jo then
- create l_link.make_with_attribute (l_keys.at (i).item, create_link_attributes (jo))
- Result.force (l_link, l_keys.at (i).item)
- elseif attached {JSON_ARRAY} j.item (l_keys.at (i)) as ja then
- create l_link.make (l_keys.at (i).item)
- from
- k := 1
- until
- k > ja.count
- loop
- if attached {JSON_OBJECT} ja.i_th (k) as ji then
- l_link.add_attribute (create_link_attributes (ji))
+ create Result.make(2)
+ from
+ i := 1
+ l_keys := j.current_keys
+ until
+ i > l_keys.count
+ loop
+ l_key_i := l_keys[i]
+ if attached {JSON_OBJECT} j.item (l_key_i) as jo then
+ create l_link.make_with_attribute (l_key_i.item, create_link_attributes (jo))
+ Result.force (l_link, l_key_i.item)
+ elseif attached {JSON_ARRAY} j.item (l_key_i) as ja then
+ create l_link.make (l_key_i.item)
+ from
+ k := 1
+ until
+ k > ja.count
+ loop
+ if attached {JSON_OBJECT} ja.i_th (k) as ji then
+ l_link.add_attribute (create_link_attributes (ji))
+ end
+ k := k + 1
end
- k := k + 1
+ Result.force (l_link, l_key_i.item)
end
- Result.force (l_link, l_keys.at (i).item)
- end
- i := i +1
- end
- end
-
+ i := i +1
+ end
+ end
- create_link_attributes (j: attached JSON_OBJECT) : LINK_ATTRIBUTE
- local
- href: STRING
- name : detachable STRING
- title: detachable STRING
- hreflang : detachable STRING
+ create_link_attributes (j: JSON_OBJECT): LINK_ATTRIBUTE
do
if attached {JSON_STRING} j.item ("href") as js1 then
create Result.make (js1.item)
@@ -172,7 +158,4 @@ feature -- Converter Implementation
end
end
-
-
-
end
View
6 test/test_suite.ecf
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
-<system xmlns="http://www.eiffel.com/developers/xml/configuration-1-9-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.eiffel.com/developers/xml/configuration-1-9-0 http://www.eiffel.com/developers/xml/configuration-1-9-0.xsd" name="test" uuid="94C384A6-8CAD-4822-B448-EFA81264AFFD">
+<system xmlns="http://www.eiffel.com/developers/xml/configuration-1-9-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.eiffel.com/developers/xml/configuration-1-9-0 http://www.eiffel.com/developers/xml/configuration-1-9-0.xsd" name="test" uuid="68E0C364-BA4A-4472-B21F-ADB370B0426F">
<target name="test_suite">
<root cluster="test" class="APPLICATION" feature="make"/>
<file_rule>
@@ -11,8 +11,8 @@
<assertions precondition="true" postcondition="true" check="true" invariant="true" loop="true" supplier_precondition="true"/>
</option>
<library name="base" location="$ISE_LIBRARY\library\base\base-safe.ecf"/>
- <library name="hal" location="C:\Users\jvelilla\Documents\Eiffel User Files\7.1\projects\HAL\library\hal.ecf"/>
- <library name="json" location="C:\Users\jvelilla\work\languages\Eiffel71\contrib\library\text\parser\json\library\json-safe.ecf"/>
+ <library name="hal" location="..\library\hal-safe.ecf" readonly="false"/>
+ <library name="json" location="$ISE_LIBRARY\contrib\library\text\parser\json\library\json-safe.ecf"/>
<library name="testing" location="$ISE_LIBRARY\library\testing\testing-safe.ecf"/>
<cluster name="test" location=".\" recursive="true"/>
</target>

0 comments on commit 7b7d3d9

Please sign in to comment.
Something went wrong with that request. Please try again.