-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[API] Support Object Formatting on List Requests & Specifically Minim…
…al Function Format (#5659) * function format * function format * function format * fix defaults * split formatters * big oops * less diff --------- Co-authored-by: quaark <a.melnick@icloud.com>
- Loading branch information
Showing
8 changed files
with
193 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Copyright 2024 Iguazio | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
from .function import FunctionFormat # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Copyright 2024 Iguazio | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
import typing | ||
|
||
|
||
class ObjectFormat: | ||
full = "full" | ||
|
||
@staticmethod | ||
def format_method(_format: str) -> typing.Optional[typing.Callable]: | ||
return { | ||
ObjectFormat.full: None, | ||
}[_format] | ||
|
||
@classmethod | ||
def format_obj(cls, obj: typing.Any, _format: str) -> typing.Any: | ||
_format = _format or cls.full | ||
format_method = cls.format_method(_format) | ||
if not format_method: | ||
return obj | ||
|
||
return format_method(obj) | ||
|
||
@staticmethod | ||
def filter_obj_method(_filter: list[list[str]]) -> typing.Callable: | ||
def _filter_method(obj: dict) -> dict: | ||
formatted_obj = {} | ||
for key_list in _filter: | ||
obj_recursive_iterator = obj | ||
formatted_obj_recursive_iterator = formatted_obj | ||
for idx, key in enumerate(key_list): | ||
if key not in obj_recursive_iterator: | ||
break | ||
value = ( | ||
{} if idx < len(key_list) - 1 else obj_recursive_iterator[key] | ||
) | ||
formatted_obj_recursive_iterator.setdefault(key, value) | ||
|
||
obj_recursive_iterator = obj_recursive_iterator[key] | ||
formatted_obj_recursive_iterator = formatted_obj_recursive_iterator[ | ||
key | ||
] | ||
|
||
return formatted_obj | ||
|
||
return _filter_method |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Copyright 2024 Iguazio | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
import typing | ||
|
||
import mlrun.common.types | ||
|
||
from .base import ObjectFormat | ||
|
||
|
||
class FunctionFormat(ObjectFormat, mlrun.common.types.StrEnum): | ||
minimal = "minimal" | ||
|
||
@staticmethod | ||
def format_method(_format: str) -> typing.Optional[typing.Callable]: | ||
return { | ||
FunctionFormat.full: None, | ||
FunctionFormat.minimal: FunctionFormat.filter_obj_method( | ||
[ | ||
["kind"], | ||
["metadata"], | ||
["status"], | ||
["spec", "description"], | ||
["spec", "image"], | ||
["spec", "default_handler"], | ||
["spec", "entry_points"], | ||
] | ||
), | ||
}[_format] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters