Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
O
OpenFisca JSON Model
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Tax-Benefit
OpenFisca JSON Model
Commits
e9ee4378
Commit
e9ee4378
authored
1 year ago
by
Emmanuel Raviart
Browse files
Options
Downloads
Patches
Plain Diff
Add DBnomics export of scales, arrays & dicts
parent
28441455
Branches
Branches containing commit
Tags
tax-benefit_5.0.8
Tags containing commit
No related merge requests found
Pipeline
#111989
passed
1 year ago
Stage: build
Changes
1
Pipelines
2
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
packages/tools/src/scripts/convert_raw_parameters_to_dbnomics.ts
+235
-34
235 additions, 34 deletions
...s/tools/src/scripts/convert_raw_parameters_to_dbnomics.ts
with
235 additions
and
34 deletions
packages/tools/src/scripts/convert_raw_parameters_to_dbnomics.ts
+
235
−
34
View file @
e9ee4378
import
{
auditChain
,
auditRequire
,
strictAudit
}
from
"
@auditors/core
"
import
{
AmountBracket
,
auditRawParameterToEditable
,
auditUnits
,
jsonReplacer
,
ParameterClass
,
RateBracket
,
rawParameterFromYaml
,
ScaleType
,
unitsFromYaml
,
ValueType
,
type
Parameter
,
type
Unit
,
}
from
"
@tax-benefit/openfisca-json-model
"
...
...
@@ -50,32 +54,140 @@ const optionsDefinitions = [
]
const
options
=
commandLineArgs
(
optionsDefinitions
)
function
assertNever
(
name
:
string
,
value
:
never
):
never
{
throw
`Unexpected value for "
${
name
}
" :
${
value
}
`
}
async
function
convertParameterToSeries
(
seriesFilePath
:
string
,
parameter
:
Parameter
,
ids
:
string
[],
):
Promise
<
void
>
{
switch
(
parameter
.
class
)
{
case
ParameterClass
.
Node
:
case
ParameterClass
.
Node
:
{
for
(
const
[
id
,
child
]
of
Object
.
entries
(
parameter
.
children
??
{}))
{
await
convertParameterToSeries
(
seriesFilePath
,
child
,
[...
ids
,
id
])
}
return
case
ParameterClass
.
Scale
:
// TODO
}
case
ParameterClass
.
Scale
:
{
switch
(
parameter
.
type
)
{
case
ScaleType
.
LinearAverageRate
:
case
ScaleType
.
MarginalRate
:
{
const
fields
=
[
"
base
"
,
"
threshold
"
,
"
rate
"
]
as
Array
<
keyof
RateBracket
>
const
hasBase
=
parameter
.
brackets
.
some
(
(
bracket
)
=>
bracket
.
base
!==
undefined
,
)
if
(
!
hasBase
)
{
fields
.
splice
(
0
,
1
)
// Remove "base" field.
}
for
(
const
[
bracketIndex
,
bracket
]
of
parameter
.
brackets
.
entries
())
{
for
(
const
field
of
fields
)
{
const
name
=
[...
ids
,
bracketIndex
.
toString
(),
field
].
join
(
"
.
"
)
await
fs
.
appendFile
(
seriesFilePath
,
JSON
.
stringify
({
code
:
name
,
name
:
`
${
parameter
.
description
}
,
${
field
}
${
bracketIndex
}
`
,
// attributes: {
// A1: "V1",
// ref_leg: "https://",
// },
dimensions
:
[
name
,
// path
],
observations
:
[
[
"
PERIOD
"
,
"
VALUE
"
,
"
OBS_STATUS
"
,
"
REFERENCE_LEGISLATIVE
"
],
...
Object
.
entries
(
bracket
[
field
]
??
{})
.
sort
(([
instant1
],
[
instant2
])
=>
instant1
.
localeCompare
(
instant2
),
)
.
map
(([
instant
,
value
])
=>
{
return
[
instant
,
value
===
"
expected
"
?
"
NA
"
:
value
.
value
,
value
===
"
expected
"
?
"
E
"
:
null
,
null
,
]
}),
],
}),
)
await
fs
.
appendFile
(
seriesFilePath
,
"
\n
"
)
}
}
return
}
case
ScaleType
.
MarginalAmount
:
case
ScaleType
.
SingleAmount
:
{
const
fields
=
[
"
threshold
"
,
"
amount
"
]
as
Array
<
keyof
AmountBracket
>
for
(
const
[
bracketIndex
,
bracket
]
of
parameter
.
brackets
.
entries
())
{
for
(
const
field
of
fields
)
{
const
name
=
[...
ids
,
bracketIndex
.
toString
(),
field
].
join
(
"
.
"
)
await
fs
.
appendFile
(
seriesFilePath
,
JSON
.
stringify
({
code
:
name
,
name
:
`
${
parameter
.
description
}
,
${
field
}
${
bracketIndex
}
`
,
// attributes: {
// A1: "V1",
// ref_leg: "https://",
// },
dimensions
:
[
name
,
// path
],
observations
:
[
[
"
PERIOD
"
,
"
VALUE
"
,
"
OBS_STATUS
"
,
"
REFERENCE_LEGISLATIVE
"
],
...
Object
.
entries
(
bracket
[
field
])
.
sort
(([
instant1
],
[
instant2
])
=>
instant1
.
localeCompare
(
instant2
),
)
.
map
(([
instant
,
value
])
=>
{
return
[
instant
,
value
===
"
expected
"
?
"
NA
"
:
value
.
value
,
value
===
"
expected
"
?
"
E
"
:
null
,
null
,
]
}),
],
}),
)
await
fs
.
appendFile
(
seriesFilePath
,
"
\n
"
)
}
}
return
case
ParameterClass
.
Value
:
}
default
:
assertNever
(
"
ScaleParameter.type
"
,
parameter
)
}
}
case
ParameterClass
.
Value
:
{
switch
(
parameter
.
type
)
{
case
ValueType
.
Boolean
:
case
ValueType
.
Number
:
{
const
name
=
ids
.
join
(
"
.
"
)
await
fs
.
appendFile
(
seriesFilePath
,
JSON
.
stringify
({
code
:
ids
.
join
(
"
.
"
)
,
code
:
name
,
name
:
parameter
.
description
,
// attributes: {
// A1: "V1",
// ref_leg: "https://",
// },
dimensions
:
[
ids
.
join
(
"
.
"
)
,
// path
name
,
// path
],
observations
:
[
[
"
PERIOD
"
,
"
VALUE
"
,
"
OBS_STATUS
"
,
"
REFERENCE_LEGISLATIVE
"
],
...
...
@@ -97,6 +209,95 @@ async function convertParameterToSeries(
await
fs
.
appendFile
(
seriesFilePath
,
"
\n
"
)
return
}
case
ValueType
.
StringArray
:
{
let
maxLength
=
0
for
(
const
value
of
Object
.
values
(
parameter
.
values
))
{
if
(
value
.
value
!==
null
&&
value
.
value
.
length
>
maxLength
)
{
maxLength
=
value
.
value
.
length
}
}
for
(
let
index
=
0
;
index
<
maxLength
;
index
++
)
{
const
name
=
[...
ids
,
index
].
join
(
"
.
"
)
await
fs
.
appendFile
(
seriesFilePath
,
JSON
.
stringify
({
code
:
name
,
name
:
`
${
parameter
.
description
}
, index
${
index
}
`
,
// attributes: {
// A1: "V1",
// ref_leg: "https://",
// },
dimensions
:
[
name
,
// path
],
observations
:
[
[
"
PERIOD
"
,
"
VALUE
"
,
"
OBS_STATUS
"
,
"
REFERENCE_LEGISLATIVE
"
],
...
Object
.
entries
(
parameter
.
values
)
.
sort
(([
instant1
],
[
instant2
])
=>
instant1
.
localeCompare
(
instant2
),
)
.
map
(([
instant
,
value
])
=>
{
return
[
instant
,
value
.
value
?.[
index
]
??
null
,
null
,
null
]
}),
],
}),
)
await
fs
.
appendFile
(
seriesFilePath
,
"
\n
"
)
}
return
}
case
ValueType
.
StringByString
:
{
const
keys
=
new
Set
<
string
>
()
for
(
const
value
of
Object
.
values
(
parameter
.
values
))
{
if
(
value
.
value
!==
null
)
{
for
(
const
key
of
Object
.
keys
(
value
.
value
))
{
keys
.
add
(
key
)
}
}
}
for
(
const
key
of
keys
)
{
const
name
=
[...
ids
,
key
].
join
(
"
.
"
)
await
fs
.
appendFile
(
seriesFilePath
,
JSON
.
stringify
({
code
:
name
,
name
:
`
${
parameter
.
description
}
, key
${
key
}
`
,
// attributes: {
// A1: "V1",
// ref_leg: "https://",
// },
dimensions
:
[
name
,
// path
],
observations
:
[
[
"
PERIOD
"
,
"
VALUE
"
,
"
OBS_STATUS
"
,
"
REFERENCE_LEGISLATIVE
"
],
...
Object
.
entries
(
parameter
.
values
)
.
sort
(([
instant1
],
[
instant2
])
=>
instant1
.
localeCompare
(
instant2
),
)
.
map
(([
instant
,
value
])
=>
{
return
[
instant
,
value
.
value
?.[
key
]
??
null
,
null
,
null
]
}),
],
}),
)
await
fs
.
appendFile
(
seriesFilePath
,
"
\n
"
)
}
return
}
default
:
assertNever
(
"
ValueParameter.type
"
,
parameter
)
}
}
default
:
assertNever
(
"
Parameter.class
"
,
parameter
)
}
}
async
function
main
():
Promise
<
number
>
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment