Skip to content

Commit

Permalink
add JSON serialization flag; keep it unactive
Browse files Browse the repository at this point in the history
  • Loading branch information
perazz committed Apr 13, 2023
1 parent 5c95577 commit 7368775
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 20 deletions.
2 changes: 1 addition & 1 deletion fpm.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ toml-f.rev = "54686e45993f3a9a1d05d5c7419f39e7d5a4eb3f"
M_CLI2.git = "https://github.com/urbanjost/M_CLI2.git"
M_CLI2.rev = "7264878cdb1baff7323cc48596d829ccfe7751b8"
jonquil.git = "https://github.com/toml-f/jonquil"
jonquil.rev = "05d30818bb12fb877226ce284b9a3a41b971a889"
jonquil.rev = "4c27c8c1e411fa8790dffcf8c3fa7a27b6322273"

[[test]]
name = "cli-test"
Expand Down
2 changes: 1 addition & 1 deletion src/fpm/git.f90
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ subroutine load_from_toml(self, table, error)
self%descriptor = parse_descriptor(descriptor_name)

if (self%descriptor==git_descriptor%error) then
call fatal_error(error,"invalid descriptor ID in TOML entry")
call fatal_error(error,"invalid descriptor ID <"//descriptor_name//"> in TOML entry")
return
end if

Expand Down
83 changes: 69 additions & 14 deletions src/fpm/toml.f90
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ module fpm_toml
use tomlf, only: toml_table, toml_array, toml_key, toml_stat, get_value, &
& set_value, toml_parse, toml_error, new_table, add_table, add_array, &
& toml_serialize, len, toml_load
use tomlf_de_parser, only: parse
use jonquil, only: json_serialize, json_error, json_value, json_object, json_load
use jonquil_lexer, only: json_lexer, new_lexer_from_unit
use iso_fortran_env, only: int64
implicit none
private
Expand Down Expand Up @@ -154,51 +157,69 @@ end subroutine test_serialization


!> Write serializable object to a formatted Fortran unit
subroutine dump_to_unit(self, unit, error)
subroutine dump_to_unit(self, unit, error, json)
!> Instance of the dependency tree
class(serializable_t), intent(inout) :: self
!> Formatted unit
integer, intent(in) :: unit
!> Error handling
type(error_t), allocatable, intent(out) :: error
!> Optional JSON format requested?
logical, optional, intent(in) :: json

type(toml_table) :: table
logical :: is_json

is_json = .false.; if (present(json)) is_json = json

table = toml_table()
call self%dump(table, error)

write (unit, '(a)') toml_serialize(table)
if (is_json) then

!> Deactivate JSON serialization for now
call fatal_error(error, 'JSON serialization option is not yet available')
return

write (unit, '(a)') json_serialize(table)
else
write (unit, '(a)') toml_serialize(table)
end if

call table%destroy()

end subroutine dump_to_unit

!> Write serializable object to file
subroutine dump_to_file(self, file, error)
subroutine dump_to_file(self, file, error, json)
!> Instance of the dependency tree
class(serializable_t), intent(inout) :: self
!> File name
character(len=*), intent(in) :: file
!> Error handling
type(error_t), allocatable, intent(out) :: error
!> Optional JSON format
logical, optional, intent(in) :: json

integer :: unit

open (file=file, newunit=unit)
call self%dump(unit, error)
call self%dump(unit, error, json)
close (unit)
if (allocated(error)) return

end subroutine dump_to_file

!> Read dependency tree from file
subroutine load_from_file(self, file, error)
subroutine load_from_file(self, file, error, json)
!> Instance of the dependency tree
class(serializable_t), intent(inout) :: self
!> File name
character(len=*), intent(in) :: file
!> Error handling
type(error_t), allocatable, intent(out) :: error
!> Optional JSON format
logical, optional, intent(in) :: json

integer :: unit
logical :: exist
Expand All @@ -207,30 +228,64 @@ subroutine load_from_file(self, file, error)
if (.not. exist) return

open (file=file, newunit=unit)
call self%load(unit, error)
call self%load(unit, error, json)
close (unit)
end subroutine load_from_file

!> Read dependency tree from file
subroutine load_from_unit(self, unit, error)
subroutine load_from_unit(self, unit, error, json)
!> Instance of the dependency tree
class(serializable_t), intent(inout) :: self
!> File name
integer, intent(in) :: unit
!> Error handling
type(error_t), allocatable, intent(out) :: error
!> Optional JSON format
logical, optional, intent(in) :: json

type(toml_error), allocatable :: parse_error
type(toml_error), allocatable :: toml_error
type(toml_table), allocatable :: table
type(json_lexer) :: lexer
logical :: is_json

call toml_load(table, unit, error=parse_error)
is_json = .false.; if (present(json)) is_json = json

if (allocated(parse_error)) then
allocate (error)
call move_alloc(parse_error%message, error%message)
return
end if
if (is_json) then

!> Deactivate JSON deserialization for now
call fatal_error(error, 'JSON deserialization option is not yet available')
return

!> init JSON interpreter
call new_lexer_from_unit(lexer, unit, toml_error)
if (allocated(toml_error)) then
allocate (error)
call move_alloc(toml_error%message, error%message)
return
end if

!> Parse JSON to TOML table
call parse(lexer, table, error=toml_error)
if (allocated(toml_error)) then
allocate (error)
call move_alloc(toml_error%message, error%message)
return
end if

else

!> use default TOML parser
call toml_load(table, unit, error=toml_error)

if (allocated(toml_error)) then
allocate (error)
call move_alloc(toml_error%message, error%message)
return
end if

endif

!> Read object from TOML table
call self%load(table, error)
if (allocated(error)) return

Expand Down
5 changes: 1 addition & 4 deletions test/fpm_test/test_toml.f90
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,7 @@ subroutine dependency_tree_roundtrip(error)
sha1="7264878cdb1baff7323cc48596d829ccfe7751b8")

call deps%test_serialization("full dependency tree", error)
if (allocated(error)) then
print *, error%message
stop 'catastrophic'
end if
if (allocated(error)) return

! Remove dependencies (including all them)
do ii = 1, ALLOCATED_DEPS
Expand Down

0 comments on commit 7368775

Please sign in to comment.