# coding: utf-8

"""
    Instana REST API documentation

    Documentation for INSTANA REST API

    The version of the OpenAPI document: 1.309.1268
    Contact: support@instana.com
    Generated by OpenAPI Generator (https://openapi-generator.tech)

    Do not edit the class manually.
"""  # noqa: E501


from __future__ import annotations
import pprint
import re  # noqa: F401
import json

from pydantic import BaseModel, ConfigDict, Field, StrictBool, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from typing_extensions import Annotated
from instana_client.models.service_matching_rule import ServiceMatchingRule
from typing import Optional, Set
from typing_extensions import Self

class ServiceConfig(BaseModel):
    """
    ServiceConfig
    """ # noqa: E501
    comment: Optional[Annotated[str, Field(min_length=0, strict=True, max_length=2048)]] = Field(default=None, description="A small description of the service configuration would be present in this field if it was provided during creation of the custom service rule. If it was not provided, this field will remain empty. It is considered as best practice to add a comment to document the reasoning behind creating the rule. ")
    enabled: StrictBool = Field(description="If enabled, calls will be mapped to the rule.")
    id: StrictStr = Field(description="A unique string for the service configuration. Eg: `G510hmXYSDysLZ5kuj0BaQ`")
    label: StrictStr = Field(description="It contains the tags defined in `matchSpecification` concatenated with a dash. Eg: if the `matchSpecification` contains keys `kubernetes.namespace.name` and `docker.label`, `label` would be `kubernetes.namespace.name-docker.label`. ")
    match_specification: Annotated[List[ServiceMatchingRule], Field(min_length=0, max_length=20)] = Field(description="Calls will be matched with the array of key-value tags present in this field.", alias="matchSpecification")
    name: Annotated[str, Field(min_length=1, strict=True, max_length=128)] = Field(description="The name of the service configuration. Eg: `Rule ABC`")
    __properties: ClassVar[List[str]] = ["comment", "enabled", "id", "label", "matchSpecification", "name"]

    model_config = ConfigDict(
        populate_by_name=True,
        validate_assignment=True,
        protected_namespaces=(),
    )


    def to_str(self) -> str:
        """Returns the string representation of the model using alias"""
        return pprint.pformat(self.model_dump(by_alias=True))

    def to_json(self) -> str:
        """Returns the JSON representation of the model using alias"""
        # TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
        return json.dumps(self.to_dict())

    @classmethod
    def from_json(cls, json_str: str) -> Optional[Self]:
        """Create an instance of ServiceConfig from a JSON string"""
        return cls.from_dict(json.loads(json_str))

    def to_dict(self) -> Dict[str, Any]:
        """Return the dictionary representation of the model using alias.

        This has the following differences from calling pydantic's
        `self.model_dump(by_alias=True)`:

        * `None` is only added to the output dict for nullable fields that
          were set at model initialization. Other fields with value `None`
          are ignored.
        """
        excluded_fields: Set[str] = set([
        ])

        _dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
        )
        # override the default output from pydantic by calling `to_dict()` of each item in match_specification (list)
        _items = []
        if self.match_specification:
            for _item_match_specification in self.match_specification:
                if _item_match_specification:
                    _items.append(_item_match_specification.to_dict())
            _dict['matchSpecification'] = _items
        return _dict

    @classmethod
    def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
        """Create an instance of ServiceConfig from a dict"""
        if obj is None:
            return None

        if not isinstance(obj, dict):
            return cls.model_validate(obj)

        _obj = cls.model_validate({
            "comment": obj.get("comment"),
            "enabled": obj.get("enabled"),
            "id": obj.get("id"),
            "label": obj.get("label"),
            "matchSpecification": [ServiceMatchingRule.from_dict(_item) for _item in obj["matchSpecification"]] if obj.get("matchSpecification") is not None else None,
            "name": obj.get("name")
        })
        return _obj


