Skip to content

Commit

Permalink
Define YAMLSerializerField in its own file (#696)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsong-rh authored Feb 28, 2024
1 parent 790198e commit 0830ae6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 30 deletions.
7 changes: 2 additions & 5 deletions src/aap_eda/api/serializers/event_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@
MissingEventStreamRulebookSource,
)
from aap_eda.api.serializers.credential import CredentialSerializer
from aap_eda.api.serializers.utils import (
YAMLSerializerField,
substitute_extra_vars,
swap_sources,
)
from aap_eda.api.serializers.fields.yaml import YAMLSerializerField
from aap_eda.api.serializers.utils import substitute_extra_vars, swap_sources
from aap_eda.core import models, validators

logger = logging.getLogger(__name__)
Expand Down
39 changes: 39 additions & 0 deletions src/aap_eda/api/serializers/fields/yaml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2024 Red Hat, Inc.
#
# 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 yaml
from rest_framework import serializers
from rest_framework.exceptions import ValidationError


class YAMLSerializerField(serializers.Field):
"""Serializer for YAML a superset of JSON."""

def to_internal_value(self, data) -> dict:
if data:
try:
parsed_args = yaml.safe_load(data)
except yaml.YAMLError:
raise ValidationError("Invalid YAML format for input data")

if not isinstance(parsed_args, dict):
raise ValidationError(
"The input field must be a YAML object (dictionary)"
)

return parsed_args
return data

def to_representation(self, value) -> str:
return yaml.dump(value)
2 changes: 1 addition & 1 deletion src/aap_eda/api/serializers/rulebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from drf_spectacular.utils import extend_schema_field
from rest_framework import serializers

from aap_eda.api.serializers.utils import YAMLSerializerField
from aap_eda.api.serializers.fields.yaml import YAMLSerializerField
from aap_eda.core import models


Expand Down
24 changes: 0 additions & 24 deletions src/aap_eda/api/serializers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,10 @@
import yaml
from django.conf import settings
from jinja2.nativetypes import NativeTemplate
from rest_framework import serializers
from rest_framework.exceptions import ValidationError

LOGGER = logging.getLogger(__name__)


class YAMLSerializerField(serializers.Field):
"""Serializer for YAML a superset of JSON."""

def to_internal_value(self, data) -> dict:
if data:
try:
parsed_args = yaml.safe_load(data)
except yaml.YAMLError:
raise ValidationError("Invalid YAML format for input data")

if not isinstance(parsed_args, dict):
raise ValidationError(
"The input field must be a YAML object (dictionary)"
)

return parsed_args
return data

def to_representation(self, value) -> str:
return yaml.dump(value)


def _render_string(value: str, context: dict) -> str:
if "{{" in value and "}}" in value:
return NativeTemplate(value, undefined=jinja2.StrictUndefined).render(
Expand Down

0 comments on commit 0830ae6

Please sign in to comment.