Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
schemas
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
Analyze
Value stream analytics
Contributor analytics
CI/CD 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
hive
schemas
Commits
e540fd4d
Commit
e540fd4d
authored
2 years ago
by
Mateusz Kudela
Browse files
Options
Downloads
Patches
Plain Diff
Add schema reducer and cover it with tests
parent
1eeedcd5
No related branches found
No related tags found
1 merge request
!24
Schemas modification tools
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
package/schemas/__private/modify/schema_reducer.py
+32
-0
32 additions, 0 deletions
package/schemas/__private/modify/schema_reducer.py
tests/test_schema_modification.py
+72
-0
72 additions, 0 deletions
tests/test_schema_modification.py
with
104 additions
and
0 deletions
package/schemas/__private/modify/schema_reducer.py
0 → 100644
+
32
−
0
View file @
e540fd4d
from
schemas.__private.fundamental_schemas
import
Array
,
ArrayStrict
,
Map
,
Schema
from
schemas.__private.custom_schemas
import
ApiOperationObject
,
Proposal
from
.local_tools
import
get_schema
def
get_schema_to_reduce
(
source
:
Schema
,
path
:
str
):
# Remove last element from path, we don't want to get schema that will be deleted. We want to get box which holds
# this element.
try
:
path
=
path
[:
path
.
rindex
(
'
.
'
)]
except
ValueError
:
path
=
'
source
'
schema_to_reduce
=
get_schema
(
source
,
path
)
return
schema_to_reduce
def
remove_schema_from_map
(
source
:
Schema
,
*
,
path
:
str
=
'
source
'
):
key_to_delete
=
path
.
split
(
'
.
'
)[
-
1
]
schema_to_reduce
=
get_schema_to_reduce
(
source
,
path
)
assert
isinstance
(
schema_to_reduce
,
(
Map
,
ApiOperationObject
,
Proposal
)),
'
You are trying to remove schema from
'
\
'
object which is not a Map,
'
\
'
ApiOperationObject or Proposal.
'
del
schema_to_reduce
[
key_to_delete
]
def
remove_schema_from_array
(
source
:
Schema
,
*
,
path
:
str
=
'
source
'
):
index_to_delete
=
int
(
path
.
split
(
'
.
'
)[
-
1
])
schema_to_reduce
=
get_schema_to_reduce
(
source
,
path
)
assert
isinstance
(
schema_to_reduce
,
(
Array
,
ArrayStrict
)),
'
You are trying to remove schema from object which
'
\
'
is not an Array or ArrayStrict
'
del
schema_to_reduce
[
index_to_delete
]
This diff is collapsed.
Click to expand it.
tests/test_schema_modification.py
+
72
−
0
View file @
e540fd4d
from
schemas.__private.modify.schema_expander
import
add_schema_to_array
,
add_schema_to_map
from
schemas.__private.modify.schema_reducer
import
remove_schema_from_map
,
remove_schema_from_array
from
schemas.predefined
import
*
...
...
@@ -108,3 +109,74 @@ def test_single_addition_to_array_nested_in_different_types():
)
)
def
test_single_deletion_in_outer_map
():
schema
=
Map
({
'
number
'
:
Int
()})
remove_schema_from_map
(
schema
,
path
=
'
number
'
)
assert
schema
==
Map
({})
def
test_single_deletion_in_inner_map
():
schema
=
Map
({
'
inner_map
'
:
Map
({
'
id
'
:
Int
()
})
})
remove_schema_from_map
(
schema
,
path
=
'
inner_map.id
'
)
assert
schema
==
Map
({
'
inner_map
'
:
Map
({})
})
def
test_single_deletion_in_map_nested_in_different_types
():
schema
=
Array
(
ArrayStrict
(
Map
({
'
values
'
:
AnyOf
(
Str
(),
Int
(),
Map
({
'
key_to_delete
'
:
Str
(),
})
),
'
name
'
:
AccountName
(),
})
)
)
remove_schema_from_map
(
schema
,
path
=
'
0.0.values.2.key_to_delete
'
)
assert
schema
==
Array
(
ArrayStrict
(
Map
({
'
values
'
:
AnyOf
(
Str
(),
Int
(),
Map
({
})
),
'
name
'
:
AccountName
(),
})
)
)
def
test_single_deletion_in_outer_array
():
schema
=
Array
(
Int
(),
Str
())
remove_schema_from_array
(
schema
,
path
=
'
1
'
)
assert
schema
==
Array
(
Int
())
def
test_single_deletion_in_inner_array
():
schema
=
Array
(
Array
(
Hex
(),
Str
(),
Int
(),
))
remove_schema_from_array
(
schema
,
path
=
'
0.2
'
)
assert
schema
==
Array
(
Array
(
Hex
(),
Str
(),
)
)
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