Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
frama-c
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pub
frama-c
Commits
70645473
Commit
70645473
authored
4 years ago
by
Loïc Correnson
Committed by
Michele Alberti
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
[dome] JSON encoders
parent
bfa340f2
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
ivette/src/dome/src/renderer/data/json.ts
+108
-5
108 additions, 5 deletions
ivette/src/dome/src/renderer/data/json.ts
ivette/src/dome/src/renderer/data/states.ts
+31
-9
31 additions, 9 deletions
ivette/src/dome/src/renderer/data/states.ts
with
139 additions
and
14 deletions
ivette/src/dome/src/renderer/data/json.ts
+
108
−
5
View file @
70645473
...
...
@@ -40,7 +40,7 @@ export function stringify(js: any) {
}
/**
Export JSON (or any data) as a string with indent
ed content
.
Export JSON (or any data) as a string with indent
ation
.
*/
export
function
pretty
(
js
:
any
)
{
return
JSON
.
stringify
(
js
,
undefined
,
2
);
...
...
@@ -189,6 +189,38 @@ export function jTry<A>(fn: Loose<A>, defaultValue?: A): Loose<A> {
};
}
/**
Converts maps to dictionnaries.
*/
export
function
jMap
<
A
>
(
fn
:
Loose
<
A
>
):
Safe
<
Map
<
string
,
A
>>
{
return
(
js
:
json
)
=>
{
const
m
=
new
Map
<
string
,
A
>
();
if
(
js
!==
null
&&
typeof
js
===
'
object
'
&&
!
Array
.
isArray
(
js
))
{
for
(
let
k
of
Object
.
keys
(
js
))
{
const
v
=
fn
(
js
[
k
]);
if
(
v
!==
undefined
)
m
.
set
(
k
,
v
);
}
}
return
m
;
};
}
/**
Converts dictionnaries to maps.
*/
export
function
eMap
<
A
>
(
fn
:
Encoder
<
A
>
):
Encoder
<
Map
<
string
,
undefined
|
A
>>
{
return
m
=>
{
const
js
:
json
=
{};
m
.
forEach
((
v
,
k
)
=>
{
if
(
v
!==
undefined
)
{
const
u
=
fn
(
v
);
if
(
u
!==
undefined
)
js
[
k
]
=
u
;
}
});
return
js
;
};
}
/**
Apply the decoder on each item of a JSON array, or return `[]` otherwize.
Can be also applied on a _loose_ decoder, but you will get
...
...
@@ -215,6 +247,22 @@ export function jList<A>(fn: Loose<A>): Safe<A[]> {
};
}
/**
Exports all non-undefined elements.
*/
export
function
eList
<
A
>
(
fn
:
Encoder
<
A
>
):
Encoder
<
(
A
|
undefined
)[]
>
{
return
m
=>
{
const
js
:
json
[]
=
[];
m
.
forEach
(
v
=>
{
if
(
v
!==
undefined
)
{
const
u
=
fn
(
v
);
if
(
u
!==
undefined
)
js
.
push
(
u
);
}
});
return
js
;
};
}
/** Apply a pair of decoders to JSON pairs, or return `undefined`. */
export
function
jPair
<
A
,
B
>
(
fa
:
Safe
<
A
>
,
...
...
@@ -289,8 +337,13 @@ export function jObject<A>(fp: Props<A>): Loose<A> {
const
buffer
=
{}
as
A
;
for
(
var
k
of
Object
.
keys
(
fp
))
{
const
fn
=
fp
[
k
as
keyof
A
];
const
fv
=
fn
(
js
[
k
]);
buffer
[
k
as
keyof
A
]
=
fv
;
if
(
fn
!==
undefined
)
{
const
fj
=
js
[
k
];
if
(
fj
!==
undefined
)
{
const
fv
=
fn
(
fj
);
if
(
fv
!==
undefined
)
buffer
[
k
as
keyof
A
]
=
fv
;
}
}
}
return
buffer
;
}
...
...
@@ -298,6 +351,35 @@ export function jObject<A>(fp: Props<A>): Loose<A> {
};
}
/**
Encoders for each property of object type `A`.
*/
export
type
EProps
<
A
>
=
{
[
P
in
keyof
A
]?:
Encoder
<
A
[
P
]
>
;
}
/**
Encode an object given the provided encoders by fields.
The exported JSON object has only original
fields with some specified encoder.
*/
export
function
eObject
<
A
>
(
fp
:
EProps
<
A
>
):
Encoder
<
A
>
{
return
(
m
:
A
)
=>
{
const
js
:
json
=
{};
for
(
var
k
of
Object
.
keys
(
fp
))
{
const
fn
=
fp
[
k
as
keyof
A
];
if
(
fn
!==
undefined
)
{
const
fv
=
m
[
k
as
keyof
A
];
if
(
fv
!==
undefined
)
{
const
r
=
fn
(
fv
);
if
(
r
!==
undefined
)
js
[
k
]
=
r
;
}
}
}
return
js
;
}
}
/** Type of dictionaries. */
export
type
dict
<
A
>
=
{
[
key
:
string
]:
A
};
...
...
@@ -310,12 +392,33 @@ export function jDictionary<A>(fn: Loose<A>): Safe<dict<A>> {
const
buffer
:
dict
<
A
>
=
{};
if
(
js
!==
null
&&
typeof
js
===
'
object
'
&&
!
Array
.
isArray
(
js
))
{
for
(
var
k
of
Object
.
keys
(
js
))
{
const
fv
=
fn
(
js
[
k
]);
if
(
fv
)
buffer
[
k
]
=
fv
;
const
fd
=
js
[
k
];
if
(
fd
!==
undefined
)
{
const
fv
=
fn
(
fd
);
if
(
fv
!==
undefined
)
buffer
[
k
]
=
fv
;
}
}
}
return
buffer
;
};
}
/**
Encode a dictionary into JSON, dicarding all inconsistent entries.
If the dictionary contains no valid entry, still returns `{}`.
*/
export
function
eDictionary
<
A
>
(
fn
:
Encoder
<
A
>
):
Encoder
<
dict
<
A
>>
{
return
(
d
:
dict
<
A
>
)
=>
{
const
js
:
json
=
{};
for
(
var
k
of
Object
.
keys
(
d
))
{
const
fv
=
d
[
k
];
if
(
fv
!==
undefined
)
{
const
fv
=
fn
(
d
[
k
]);
if
(
fv
!==
undefined
)
js
[
k
]
=
fv
;
}
}
return
js
;
};
}
// --------------------------------------------------------------------------
This diff is collapsed.
Click to expand it.
ivette/src/dome/src/renderer/data/states.ts
+
31
−
9
View file @
70645473
...
...
@@ -119,10 +119,12 @@ abstract class Settings<A> {
private
readonly
role
:
symbol
;
protected
readonly
decoder
:
JSON
.
Safe
<
A
>
;
protected
readonly
encoder
:
JSON
.
Encoder
<
A
>
;
constructor
(
role
:
string
,
decoder
:
JSON
.
Safe
<
A
>
)
{
constructor
(
role
:
string
,
decoder
:
JSON
.
Safe
<
A
>
,
encoder
:
JSON
.
Encoder
<
A
>
)
{
this
.
role
=
Symbol
(
role
);
this
.
decoder
=
decoder
;
this
.
encoder
=
encoder
;
}
validateKey
(
k
?:
string
):
string
|
undefined
{
...
...
@@ -150,9 +152,13 @@ abstract class Settings<A> {
return
this
.
decoder
(
key
?
this
.
loadData
(
key
)
:
undefined
)
}
saveValue
(
key
:
string
,
value
:
A
)
{
this
.
saveData
(
key
,
this
.
encoder
(
value
));
}
}
export
function
useSettings
<
A
extends
JSON
.
json
>
(
export
function
useSettings
<
A
>
(
S
:
Settings
<
A
>
,
dataKey
?:
string
,
):
[
A
,
(
update
:
A
)
=>
void
]
{
...
...
@@ -172,7 +178,7 @@ export function useSettings<A extends JSON.json>(
const
updateValue
=
React
.
useCallback
((
update
:
A
)
=>
{
if
(
!
isEqual
(
value
,
update
))
{
setValue
(
update
);
if
(
theKey
)
S
.
save
Data
(
theKey
,
update
);
if
(
theKey
)
S
.
save
Value
(
theKey
,
update
);
}
},
[
S
,
theKey
]);
...
...
@@ -180,10 +186,10 @@ export function useSettings<A extends JSON.json>(
}
export
class
WindowSettings
<
A
>
extends
Settings
<
A
>
{
export
class
WindowSettings
Data
<
A
>
extends
Settings
<
A
>
{
constructor
(
role
:
string
,
decoder
:
JSON
.
Safe
<
A
>
)
{
super
(
role
,
decoder
);
constructor
(
role
:
string
,
decoder
:
JSON
.
Safe
<
A
>
,
encoder
:
JSON
.
Encoder
<
A
>
)
{
super
(
role
,
decoder
,
encoder
);
}
event
=
Symbol
(
'
dome.settings
'
);
...
...
@@ -192,10 +198,10 @@ export class WindowSettings<A> extends Settings<A> {
}
export
class
GlobalSettings
<
A
>
extends
Settings
<
A
>
{
export
class
GlobalSettings
Data
<
A
>
extends
Settings
<
A
>
{
constructor
(
role
:
string
,
decoder
:
JSON
.
Safe
<
A
>
)
{
super
(
role
,
decoder
);
constructor
(
role
:
string
,
decoder
:
JSON
.
Safe
<
A
>
,
encoder
:
JSON
.
Encoder
<
A
>
)
{
super
(
role
,
decoder
,
encoder
);
}
event
=
Symbol
(
'
dome.globals
'
);
...
...
@@ -204,4 +210,20 @@ export class GlobalSettings<A> extends Settings<A> {
}
export
class
WindowSettings
<
A
extends
JSON
.
json
>
extends
WindowSettingsData
<
A
>
{
constructor
(
role
:
string
,
decoder
:
JSON
.
Safe
<
A
>
)
{
super
(
role
,
decoder
,
JSON
.
identity
);
}
}
export
class
GlobalSettings
<
A
extends
JSON
.
json
>
extends
GlobalSettingsData
<
A
>
{
constructor
(
role
:
string
,
decoder
:
JSON
.
Safe
<
A
>
)
{
super
(
role
,
decoder
,
JSON
.
identity
);
}
}
// --------------------------------------------------------------------------
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