From 7302a050247a185aeb1e4424d28a9cc0a6d5be3a Mon Sep 17 00:00:00 2001 From: Kenny Workman Date: Wed, 1 Dec 2021 08:48:45 -0800 Subject: [PATCH] feat: proto model Signed-off-by: Kenny Workman --- flytekit/models/annotation.py | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 flytekit/models/annotation.py diff --git a/flytekit/models/annotation.py b/flytekit/models/annotation.py new file mode 100644 index 0000000000..a61affe9e3 --- /dev/null +++ b/flytekit/models/annotation.py @@ -0,0 +1,45 @@ +import json as _json +from typing import Any, Dict + +from flyteidl.core import types_pb2 as _types_pb2 +from google.protobuf import json_format as _json_format +from google.protobuf import struct_pb2 as _struct + + +class TypeAnnotation: + """Python class representation of the flyteidl TypeAnnotation message.""" + + def __init__(self, annotations: Dict[str, Any]): + self._annotations = annotations + + @property + def annotations(self) -> Dict[str, Any]: + """ + :rtype: dict[str, Any] + """ + return self._annotations + + def to_flyte_idl(self) -> _types_pb2.TypeAnnotation: + """ + :rtype: flyteidl.core.types_pb2.TypeAnnotation + """ + + if self._annotations is not None: + annotations = _json_format.Parse(_json.dumps(self.annotations), _struct.Struct()) + else: + annotations = None + + t = _types_pb2.TypeAnnotation( + annotations=annotations, + ) + + return t + + @classmethod + def from_flyte_idl(cls, proto): + """ + :param flyteidl.core.types_pb2.TypeAnnotation proto: + :rtype: TypeAnnotation + """ + + return cls(annotations=proto.annotations)