Skip to content

Commit 5766cdd

Browse files
committed
Updated EG_COLOR class to define if we have a default setting.
Updated Eiffel to JSON mapping.
1 parent f3f0b37 commit 5766cdd

File tree

2 files changed

+75
-16
lines changed

2 files changed

+75
-16
lines changed

sheets/src/json/eg_sheets_json.e

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -299,17 +299,19 @@ feature {NONE} -- JSON To Eiffel
299299
-- Create an object `EG_COLOR` from a json rerpesentation `a_json`.
300300
do
301301
create Result
302-
if attached real_value_from_json (a_json, "red") as l_val then
303-
Result.set_red (l_val)
304-
end
305-
if attached real_value_from_json (a_json, "green") as l_val then
306-
Result.set_green (l_val)
307-
end
308-
if attached real_value_from_json (a_json, "blue") as l_val then
309-
Result.set_blue (l_val)
310-
end
311-
if attached real_value_from_json (a_json, "alpha") as l_val then
312-
Result.set_alpha (l_val)
302+
if not a_json.is_empty then
303+
if attached color_value_from_json (a_json, "red") as l_val then
304+
Result.set_red (l_val)
305+
end
306+
if attached color_value_from_json (a_json, "green") as l_val then
307+
Result.set_green (l_val)
308+
end
309+
if attached color_value_from_json (a_json, "blue") as l_val then
310+
Result.set_blue (l_val)
311+
end
312+
if attached color_value_from_json (a_json, "alpha") as l_val then
313+
Result.set_alpha (l_val)
314+
end
313315
end
314316
end
315317

@@ -638,6 +640,21 @@ feature {NONE} -- Implementation
638640
end
639641
end
640642

643+
color_value_from_json (a_json_data: detachable JSON_VALUE; a_id: STRING): REAL
644+
do
645+
if
646+
attached {JSON_NUMBER} json_value (a_json_data, a_id) as v and then
647+
v.numeric_type = v.real_type
648+
then
649+
Result := v.item.to_real
650+
elseif attached {JSON_NUMBER} json_value (a_json_data, a_id) as v and then
651+
v.numeric_type = v.integer_type
652+
then
653+
Result := v.item.to_integer
654+
end
655+
656+
end
657+
641658
integer_value_from_json (a_json_data: detachable JSON_VALUE; a_id: STRING): INTEGER
642659
do
643660
if

sheets/src/objects/eg_color.e

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ note
1818
class
1919
EG_COLOR
2020

21+
inherit
22+
23+
ANY
24+
redefine
25+
default_create
26+
end
27+
28+
create
29+
default_create
30+
31+
feature {NONE} -- Initialization
32+
33+
default_create
34+
do
35+
is_default := True
36+
end
2137

2238
feature -- Access
2339

@@ -37,47 +53,73 @@ feature -- Access
3753
-- This uses a wrapper message rather than a simple float scalar so that it is possible to distinguish between a default value and the value being unset.
3854
-- If omitted, this color object is to be rendered as a solid color (as if the alpha value had been explicitly given with a value of 1.0).
3955

56+
is_default: BOOLEAN
57+
-- Are the attributes in default values?
58+
4059

4160
feature -- Element Change
4261

4362
set_red (a_val: REAL)
63+
-- Set `red` to `a_val`.
4464
do
65+
is_default := False
4566
red := a_val
4667
ensure
68+
not_default: not is_default
4769
red_set: red = a_val
4870
end
4971

5072
set_green (a_val: REAL)
5173
do
74+
is_default := False
5275
green := a_val
5376
ensure
77+
not_default: not is_default
5478
green_set: green = a_val
5579
end
5680

5781
set_blue (a_val: REAL)
5882
do
83+
is_default := False
5984
blue := a_val
6085
ensure
86+
not_default: not is_default
6187
blue_set: blue = a_val
6288
end
6389

6490
set_alpha (a_val: REAL)
6591
do
92+
is_default := False
6693
alpha := a_val
6794
ensure
95+
not_default: not is_default
6896
alpha_set: alpha = a_val
6997
end
7098

99+
reset
100+
-- Reset the attributes to default values.
101+
do
102+
is_default := True
103+
red := 0
104+
green := 0
105+
blue := 0
106+
alpha := 0
107+
end
108+
71109
feature -- Eiffel to JSON
72110

73111
to_json: JSON_OBJECT
74112
-- Json representation of current object.
75113
do
76-
create Result.make_with_capacity (4)
77-
Result.put (create {JSON_NUMBER}.make_real (red), "red")
78-
Result.put (create {JSON_NUMBER}.make_real (green), "green")
79-
Result.put (create {JSON_NUMBER}.make_real (blue), "blue")
80-
Result.put (create {JSON_NUMBER}.make_real (alpha), "alpha")
114+
if is_default then
115+
create Result.make
116+
else
117+
create Result.make_with_capacity (4)
118+
Result.put (create {JSON_NUMBER}.make_real (red), "red")
119+
Result.put (create {JSON_NUMBER}.make_real (green), "green")
120+
Result.put (create {JSON_NUMBER}.make_real (blue), "blue")
121+
Result.put (create {JSON_NUMBER}.make_real (alpha), "alpha")
122+
end
81123
end
82124

83125
invariant

0 commit comments

Comments
 (0)