Coder Social home page Coder Social logo

Comments (2)

awvwgk avatar awvwgk commented on June 19, 2024

TOML Fortrans getter interface by default will eagerly create values which are absent, if you request a table and its key is not in use the table will be created and the pointer associated to it. In most cases that is what you would manually do if you find an entry is missing when reading values from a config file anyway, so it is the default. It has the advantage that serializing the root table afterwards reflects the full configuration as seen by the program.

You can add the requested=.false. argument to not create a table if it is not present, you then can check by associated(ptr) whether the table is actually present and insert your custom logic to populate the table pointer.

This should do more or less what you are looking for:

module demo
   use tomlf
   implicit none

   type :: component_type
      character(:), allocatable :: name
      integer :: val
   end type component_type

   type :: system_type
      type(component_type), allocatable :: component(:)
   end type system_type

contains

subroutine load_main(system, table, stat)
   type(system_type), intent(out) :: system
   type(toml_table), intent(inout) :: table
   integer, intent(out) :: stat

   type(toml_table), allocatable, target :: extern
   type(toml_table), pointer :: child
   type(toml_array), pointer :: array
   integer :: icomp

   call get_value(table, "system", child, stat=stat)
   if (stat /= toml_stat%success) return

   call get_value(child, "components", array, stat=stat)
   if (stat /= toml_stat%success) return

   allocate(system%component(len(array)))
   do icomp = 1, size(system%component)
      call get_value(array, icomp, system%component(icomp)%name, stat=stat)
      if (stat /= toml_stat%success) return
   end do

   do icomp = 1, size(system%component)
      associate(component => system%component(icomp))
         call get_value(table, component%name, child, &
            & requested=.false., stat=stat)
         if (stat /= toml_stat%success) return

         ! If component table is not provided fallback to read it from extra file
         if (.not.associated(child)) then
            call toml_load(extern, component%name // ".toml")
            if (.not.allocated(extern)) then
               stat = toml_stat%fatal
               return
            end if
            child => extern
         end if

         call get_value(child, "value", component%val, stat=stat)
         if (stat /= toml_stat%success) return
      end associate
   end do

end subroutine load_main

end module demo

from toml-f.

awvwgk avatar awvwgk commented on June 19, 2024

Presence of entries has been asked for a few times (#115, #110), so it might be good to have a how-to recipe on this case. Personally, I think with a carefully designed layout in the configuration file, checking for presence should never seldom be necessary, but there are of course edge cases where it can be useful.

from toml-f.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.