Configuration Records
Config Record Validation
Counting Passed Checks
A derived type can group related configuration fields before validation checks run.
Program
Play the program to change the worker count and validate the record.
config_record_validation.f90
Replay: real traced execution (multi-file project)
program config_record_validation_demo
implicit none
type :: app_config
integer :: port
integer :: workers
integer :: timeout
end type app_config
type(app_config) :: cfg
integer :: valid_count
cfg%port = 8080
cfg%workers = 4
cfg%timeout = 30
valid_count = 0
if (cfg%port > 0) valid_count = valid_count + 1
if (cfg%workers > 0) valid_count = valid_count + 1
if (cfg%timeout <= 60) valid_count = valid_count + 1
print '(I0, 1X, I0)', cfg%workers, valid_count
end program config_record_validation_demo
program config_record_validation_demo
implicit none
type :: app_config
integer :: port
integer :: workers
integer :: timeout
end type app_config
type(app_config) :: cfg
integer :: valid_count
cfg%port = 8080
cfg%workers = 0
cfg%timeout = 30
valid_count = 0
if (cfg%port > 0) valid_count = valid_count + 1
if (cfg%workers > 0) valid_count = valid_count + 1
if (cfg%timeout <= 60) valid_count = valid_count + 1
print '(I0, 1X, I0)', cfg%workers, valid_count
end program config_record_validation_demo
program config_record_validation_demo
implicit none
type :: app_config
integer :: port
integer :: workers
integer :: timeout
end type app_config
type(app_config) :: cfg
integer :: valid_count
cfg%port = 8080
cfg%workers = 8
cfg%timeout = 30
valid_count = 0
if (cfg%port > 0) valid_count = valid_count + 1
if (cfg%workers > 0) valid_count = valid_count + 1
if (cfg%timeout <= 60) valid_count = valid_count + 1
print '(I0, 1X, I0)', cfg%workers, valid_count
end program config_record_validation_demo
cfg%port ← 8080
11cfg%port = 808012cfg%workers = 4values this step8080cfg%portcfg%workers ← 4
11cfg%port = 808012cfg%workers = 413cfg%timeout = 30values this step4cfg%workerscfg%timeout ← 30
12cfg%workers = 413cfg%timeout = 3014valid_count = 0values this step30cfg%timeoutvalid_count ← 0
13cfg%timeout = 3014valid_count = 015if (cfg%port > 0) valid_count = valid_count + 1values this step0valid_countvalid_count ← 1
14valid_count = 015if (cfg%port > 0) valid_count = valid_count + 116if (cfg%workers > 0) valid_count = valid_count + 1values this step1valid_count.true.cfg%port > 0valid_count ← 2
15if (cfg%port > 0) valid_count = valid_count + 116if (cfg%workers > 0) valid_count = valid_count + 117if (cfg%timeout <= 60) valid_count = valid_count + 1values this step2valid_count.true.cfg%workers > 0valid_count ← 3
16if (cfg%workers > 0) valid_count = valid_count + 117if (cfg%timeout <= 60) valid_count = valid_count + 118print '(I0, 1X, I0)', cfg%workers, valid_countvalues this step3valid_count.true.cfg%timeout <= 60print '(I0, 1X, I0)', cfg%workers, valid_count
17 if (cfg%timeout <= 60) valid_count = valid_count + 118 print '(I0, 1X, I0)', cfg%workers, valid_count19end program config_record_validation_demooutput4 3values this step4cfg%workers3valid_count
cfg%port ← 8080
11cfg%port = 808012cfg%workers = 0values this step8080cfg%portcfg%workers ← 0
11cfg%port = 808012cfg%workers = 013cfg%timeout = 30values this step0cfg%workerscfg%timeout ← 30
12cfg%workers = 013cfg%timeout = 3014valid_count = 0values this step30cfg%timeoutvalid_count ← 0
13cfg%timeout = 3014valid_count = 015if (cfg%port > 0) valid_count = valid_count + 1values this step0valid_countvalid_count ← 1
14valid_count = 015if (cfg%port > 0) valid_count = valid_count + 116if (cfg%workers > 0) valid_count = valid_count + 1values this step1valid_count.true.cfg%port > 0if (cfg%workers > 0) valid_count = valid_count + 1
15if (cfg%port > 0) valid_count = valid_count + 116if (cfg%workers > 0) valid_count = valid_count + 117if (cfg%timeout <= 60) valid_count = valid_count + 1values this step.false.cfg%workers > 0valid_count ← 2
16if (cfg%workers > 0) valid_count = valid_count + 117if (cfg%timeout <= 60) valid_count = valid_count + 118print '(I0, 1X, I0)', cfg%workers, valid_countvalues this step2valid_count.true.cfg%timeout <= 60print '(I0, 1X, I0)', cfg%workers, valid_count
17 if (cfg%timeout <= 60) valid_count = valid_count + 118 print '(I0, 1X, I0)', cfg%workers, valid_count19end program config_record_validation_demooutput0 2values this step0cfg%workers2valid_count
cfg%port ← 8080
11cfg%port = 808012cfg%workers = 8values this step8080cfg%portcfg%workers ← 8
11cfg%port = 808012cfg%workers = 813cfg%timeout = 30values this step8cfg%workerscfg%timeout ← 30
12cfg%workers = 813cfg%timeout = 3014valid_count = 0values this step30cfg%timeoutvalid_count ← 0
13cfg%timeout = 3014valid_count = 015if (cfg%port > 0) valid_count = valid_count + 1values this step0valid_countvalid_count ← 1
14valid_count = 015if (cfg%port > 0) valid_count = valid_count + 116if (cfg%workers > 0) valid_count = valid_count + 1values this step1valid_count.true.cfg%port > 0valid_count ← 2
15if (cfg%port > 0) valid_count = valid_count + 116if (cfg%workers > 0) valid_count = valid_count + 117if (cfg%timeout <= 60) valid_count = valid_count + 1values this step2valid_count.true.cfg%workers > 0valid_count ← 3
16if (cfg%workers > 0) valid_count = valid_count + 117if (cfg%timeout <= 60) valid_count = valid_count + 118print '(I0, 1X, I0)', cfg%workers, valid_countvalues this step3valid_count.true.cfg%timeout <= 60print '(I0, 1X, I0)', cfg%workers, valid_count
17 if (cfg%timeout <= 60) valid_count = valid_count + 118 print '(I0, 1X, I0)', cfg%workers, valid_count19end program config_record_validation_demooutput8 3values this step8cfg%workers3valid_count
Follow the Checks
- The record starts with
port=8080,workers=4, andtimeout=30. valid_countstarts at0.port > 0is true, so the count becomes1.workers > 0is true, so the count becomes2.timeout <= 60is true, so the count becomes3and the program prints4 3. | workers | port check | workers check | timeout check | output | | ---: | --- | --- | --- | --- | | 0 | pass | fail | pass | 0 2 | | 4 | pass | pass | pass | 4 3 | | 8 | pass | pass | pass | 8 3 |
derived type
`app_config` groups related configuration fields.
field access
`cfg%workers` reads or writes one field on the record.
validation count
`valid_count` summarizes how many checks passed.
Exercise: config_record_validation.f90
Reproduce 4 3, then use workers 0 and 8 to predict 0 2 and 8 3.