Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Scope
Commits
abf6fd23
Commit
abf6fd23
authored
8 years ago
by
jpellizzari
Browse files
Options
Download
Email Patches
Plain Diff
Added shape check PoC
parent
5053f816
2492-state-shape
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
client/app/scripts/utils/__tests__/storage-utils-test.js
+67
-0
client/app/scripts/utils/__tests__/storage-utils-test.js
client/app/scripts/utils/storage-utils.js
+27
-0
client/app/scripts/utils/storage-utils.js
with
94 additions
and
0 deletions
+94
-0
client/app/scripts/utils/__tests__/storage-utils-test.js
0 → 100644
+
67
-
0
View file @
abf6fd23
import
expect
from
'
expect
'
;
import
{
isCompatibleShape
}
from
'
../storage-utils
'
;
describe
(
'
storage-utils
'
,
()
=>
{
let
state
;
beforeEach
(()
=>
{
state
=
{
controlPipe
:
null
,
nodeDetails
:
[],
topologyViewMode
:
'
topo
'
,
pinnedMetricType
:
'
CPU
'
,
pinnedSearches
:
[],
searchQuery
:
''
,
selectedNodeId
:
null
,
gridSortedBy
:
null
,
gridSortedDesc
:
null
,
topologyId
:
'
containers
'
,
topologyOptions
:
{
processes
:
{
unconnected
:
'
hide
'
},
containers
:
{
system
:
[
'
all
'
],
stopped
:
[
'
running
'
],
pseudo
:
[
'
hide
'
]
}
},
contrastMode
:
false
};
});
it
(
'
is ok when state has not changed
'
,
()
=>
{
// Same state should be ok
expect
(
isCompatibleShape
(
state
,
state
)).
toBe
(
true
);
});
it
(
'
catches state shape changes
'
,
()
=>
{
// State shape changed, should not be compatible;
const
changed
=
{
...
state
,
topologyOptions
:
{
...
state
.
topologyOptions
,
processes
:
{
// Changes from a string to an array; simulates the actual real-world case
unconnected
:
[
'
hide
'
]
}
}
};
expect
(
isCompatibleShape
(
state
,
changed
)).
toBe
(
false
);
});
it
(
'
ignores trivial shape differences
'
,
()
=>
{
const
trivial
=
{
...
state
,
nodeDetails
:
[{
a
:
1
,
b
:
2
}],
controlPipe
:
{
id
:
123
}
};
expect
(
isCompatibleShape
(
state
,
trivial
)).
toBe
(
true
);
});
});
This diff is collapsed.
Click to expand it.
client/app/scripts/utils/storage-utils.js
+
27
-
0
View file @
abf6fd23
import
debug
from
'
debug
'
;
import
reduce
from
'
lodash/reduce
'
;
const
log
=
debug
(
'
scope:storage-utils
'
);
...
...
@@ -44,3 +45,29 @@ export function storageSetObject(key, obj) {
}
return
false
;
}
function
typeOf
(
obj
)
{
return
Object
.
prototype
.
toString
.
call
(
obj
).
slice
(
8
,
-
1
).
toLowerCase
();
}
// Checks the shape of an object. Ignores the signature of elements in arrays.
function
shapeOf
(
obj
)
{
return
reduce
(
obj
,
(
result
,
val
,
key
)
=>
{
const
type
=
typeOf
(
val
);
if
(
type
===
'
null
'
||
type
===
'
undefined
'
)
{
// Do nothing
return
result
;
}
else
if
(
type
===
'
object
'
)
{
result
[
key
]
=
shapeOf
(
val
);
}
else
{
result
[
key
]
=
type
;
}
return
result
;
},
{});
}
export
function
isCompatibleShape
(
object
,
target
)
{
const
shape
=
JSON
.
stringify
(
shapeOf
(
object
));
const
targetString
=
JSON
.
stringify
(
shapeOf
(
target
));
return
shape
===
targetString
;
}
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help