Skip to content

Commit a16e11a

Browse files
fix(dag_from_json): simplify by adding vertex constructors
1 parent 6fa94c7 commit a16e11a

File tree

3 files changed

+33
-36
lines changed

3 files changed

+33
-36
lines changed

src/dag_s.f90

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -124,34 +124,7 @@ end function toposort
124124

125125
select type (vertices => maybe_vertices%value_())
126126
type is (json_array_t)
127-
128-
associate(nvertices => vertices%length())
129-
allocate(dag%vertices(nvertices))
130-
block
131-
integer i
132-
call dag%vertices%set_vertex_id( [(i,i=1,nvertices)] )
133-
end block
134-
end associate
135-
136-
block
137-
integer i
138-
do i = 1, vertices%length()
139-
associate(maybe_vertex => vertices%get_element(i))
140-
associate(errors => maybe_vertex%errors())
141-
call assert(.not. errors%has_any(), "dag_s construct_from_json: .not. errors%has_any()", char(errors%to_string()))
142-
end associate
143-
144-
select type (vertex_json => maybe_vertex%value_())
145-
class default
146-
call assert(.false., "dag_s construct_from_json: vertex was not an object", char(vertex_json%to_compact_string()))
147-
type is (json_object_t)
148-
associate(dag_vertex => vertex_t(vertex_json))
149-
call dag%vertices(i)%set_edges(dag_vertex%edges)
150-
end associate
151-
end select
152-
end associate
153-
end do
154-
end block
127+
dag%vertices = vertex_t(vertices%get_elements())
155128

156129
class default
157130
call assert(.false., "dag%from_json: vertices was not an array", char(vertices%to_compact_string()))

src/vertex_m.f90

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module vertex_m
33
!! version: v1.0
44
!! date: 2020-Nov-30
55
!! license: Copyright (c) 2020-2021, Sourcery Institute, BSD 3-clause license Copyright (c) 2018 Jacob Williams
6-
use jsonff, only : json_object_t
6+
use jsonff, only : json_element_t, json_object_t, json_value_t
77
use iso_varying_string, only : varying_string, len
88

99
implicit none
@@ -38,13 +38,19 @@ module vertex_m
3838
end type vertex_t
3939

4040
interface vertex_t
41-
42-
module function from_json(json_object) result(vertex)
41+
42+
impure elemental module function from_json_element(json_element) result(vertex)
43+
implicit none
44+
type(json_element_t), intent(in) :: json_element
45+
type(vertex_t) :: vertex
46+
end function
47+
48+
module function from_json_value(json_value) result(vertex)
4349
implicit none
44-
type(json_object_t), intent(in) :: json_object
50+
class(json_value_t), intent(in) :: json_value
4551
type(vertex_t) :: vertex
4652
end function
47-
53+
4854
pure module function construct_from_components(identifier, edges, label, attributes) result(vertex)
4955
implicit none
5056
integer, intent(in) :: identifier
@@ -53,11 +59,17 @@ pure module function construct_from_components(identifier, edges, label, attribu
5359
type(varying_string), intent(in), optional :: attributes
5460
type(vertex_t) vertex
5561
end function
56-
62+
5763
end interface
5864

5965
interface
6066

67+
module function from_json_object(json_object) result(vertex)
68+
implicit none
69+
type(json_object_t), intent(in) :: json_object
70+
type(vertex_t) :: vertex
71+
end function
72+
6173
module subroutine set_edge_vector(self,edges)
6274
!! Define the vertices on which this vertex depends on
6375
implicit none

src/vertex_s.f90

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
fallible_json_string_t, &
44
fallible_json_value_t, &
55
json_array_t, &
6-
json_element_t, &
76
json_number_t, &
87
json_string_t, &
98
json_integer_t
@@ -69,7 +68,20 @@
6968
vertex%defined_ = .true.
7069
end procedure
7170

72-
module procedure from_json
71+
module procedure from_json_element
72+
vertex = vertex_t(json_element%value_())
73+
end procedure
74+
75+
module procedure from_json_value
76+
select type (json_value)
77+
type is (json_object_t)
78+
vertex = from_json_object(json_value)
79+
class default
80+
call assert(.false., "vertex%from_json_value: vertex was not an object", char(json_value%to_compact_string()))
81+
end select
82+
end procedure
83+
84+
module procedure from_json_object
7385
type(error_list_t) :: errors
7486
type(fallible_json_value_t) :: maybe_edge
7587
type(fallible_json_value_t) :: maybe_edges

0 commit comments

Comments
 (0)