Skip to content

Commit 4107d0d

Browse files
committed
feat(string_t): get_json_value logical mold/result
1 parent 81631db commit 4107d0d

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

src/sourcery/sourcery_string_m.f90

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ module sourcery_string_m
1515
procedure :: is_allocated
1616
procedure :: get_json_key
1717
procedure :: get_json_string_scalar_value
18-
generic :: get_json_value => get_json_string_scalar_value
18+
procedure :: get_json_logical_scalar_value
19+
generic :: get_json_value => &
20+
get_json_string_scalar_value, get_json_logical_scalar_value
1921
procedure :: equivalent
2022
generic :: operator(==) => equivalent
2123
end type
@@ -62,6 +64,13 @@ elemental module function get_json_string_scalar_value(self, key, mold) result(v
6264
type(string_t) :: value_
6365
end function
6466

67+
elemental module function get_json_logical_scalar_value(self, key, mold) result(value_)
68+
implicit none
69+
class(string_t), intent(in) :: self, key
70+
logical, intent(in) :: mold
71+
logical value_
72+
end function
73+
6574
elemental module function equivalent(lhs, rhs) result(lhs_eqv_rhs)
6675
implicit none
6776
class(string_t), intent(in) :: lhs, rhs

src/sourcery/sourcery_string_s.f90

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
character(len=:), allocatable :: raw_line
5656

57-
call assert(key==self%get_json_key(), "key==self%get_json_key()", key)
57+
call assert(key==self%get_json_key(), "key==self%get_string_scalar_json_value()", key)
5858

5959
raw_line = self%string()
6060
associate(text_after_colon => raw_line(index(raw_line, ':')+1:))
@@ -71,6 +71,27 @@
7171

7272
end procedure
7373

74+
module procedure get_json_logical_scalar_value
75+
character(len=:), allocatable :: raw_line, string_value
76+
77+
call assert(key==self%get_json_key(), "get_json_logical_scalar_value: key==self%get_json_key()", key)
78+
79+
raw_line = self%string()
80+
associate(text_after_colon => raw_line(index(raw_line, ':')+1:))
81+
associate(comma => index(text_after_colon, ','))
82+
if (comma == 0) then
83+
string_value = trim(adjustl((text_after_colon)))
84+
else
85+
string_value = trim(adjustl((text_after_colon(:comma-1))))
86+
end if
87+
call assert(string_value=="true" .or. string_value=="false", &
88+
'get_json_logical_scalar_value: string_value=="true" .or. string_value="false"', string_value)
89+
value_ = string_value == "true"
90+
end associate
91+
end associate
92+
93+
end procedure
94+
7495
module procedure equivalent
7596
lhs_eqv_rhs = lhs%string() == rhs%string()
7697
end procedure

test/string_test.f90

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module string_test_m
22
use sourcery_m, only : test_t, test_result_t, string_t
33
implicit none
44

5+
56
private
67
public :: string_test_t
78

@@ -24,7 +25,8 @@ function results() result(test_results)
2425
test_results = [ &
2526
test_result_t("is_allocated() result .true. if & only if the string_t component(s) is/are allocated", check_allocation()), &
2627
test_result_t("extracting key string from colon-separated key/value pair", extracts_key()), &
27-
test_result_t("extracting string value from colon-separated key/value pair", extracts_string_scalar_value()) &
28+
test_result_t("extracting string value from colon-separated key/value pair", extracts_string_scalar_value()), &
29+
test_result_t("extracting logical value from colon-separated key/value pair", extracts_logical_scalar_value()) &
2830
]
2931
end function
3032

@@ -54,4 +56,22 @@ function extracts_string_scalar_value() result(passed)
5456
end associate
5557
end function
5658

59+
function extracts_logical_scalar_value() result(passed)
60+
logical passed
61+
62+
associate( &
63+
key_true_pair => string_t('"yada yada" : true'), &
64+
key_false_pair => string_t('"blah blah" : false'), &
65+
trailing_comma => string_t('"trailing comma" : true,') &
66+
)
67+
associate( &
68+
true => key_true_pair%get_json_value(key=string_t("yada yada"), mold=.true.), &
69+
false => key_false_pair%get_json_value(key=string_t("blah blah"), mold=.true.), &
70+
true_too => trailing_comma%get_json_value(key=string_t("trailing comma"), mold=.true.) &
71+
)
72+
passed = true .and. true_too .and. .not. false
73+
end associate
74+
end associate
75+
end function
76+
5777
end module string_test_m

0 commit comments

Comments
 (0)