# 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, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from instana_client.models.metrics_test_result_item import MetricsTestResultItem
from typing import Optional, Set
from typing_extensions import Self

class MetricsResultItem(BaseModel):
    """
    MetricsResultItem
    """ # noqa: E501
    custom_tags: Optional[Dict[str, StrictStr]] = Field(default=None, description="A map of custom properties composed of the custom property name and its value.  This information will be included if the custom properties were requested or if the query was grouped and the grouping included one or more custom properties.", alias="customTags")
    metrics: List[Dict[str, Dict[str, Any]]] = Field(description="A map of the requested metrics, composed of the metric name and its value.")
    run_type: Optional[StrictStr] = Field(default=None, description="Indicates whether the test was scheduled to run or run now", alias="runType")
    tests: Optional[List[MetricsTestResultItem]] = Field(default=None, description="A description of the Synthetic test associated with the result item.  This information will be included if the request was grouped by synthetic.testId or if includeAggregatedTestId was true on the request.")
    __properties: ClassVar[List[str]] = ["customTags", "metrics", "runType", "tests"]

    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 MetricsResultItem 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 tests (list)
        _items = []
        if self.tests:
            for _item_tests in self.tests:
                if _item_tests:
                    _items.append(_item_tests.to_dict())
            _dict['tests'] = _items
        return _dict

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

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

        _obj = cls.model_validate({
            "customTags": obj.get("customTags"),
            "metrics": obj.get("metrics"),
            "runType": obj.get("runType"),
            "tests": [MetricsTestResultItem.from_dict(_item) for _item in obj["tests"]] if obj.get("tests") is not None else None
        })
        return _obj


