Skip to content

Commit 25a0496

Browse files
committed
divide the base line code from dubbo codec in interface
1 parent c2f1424 commit 25a0496

File tree

2 files changed

+120
-89
lines changed

2 files changed

+120
-89
lines changed

src/dubbo/codec/_interface.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import abc
18+
import logging
19+
from dataclasses import dataclass
20+
from typing import Any, Callable, Optional, List, Tuple
21+
22+
__all__ = [
23+
"ParameterDescriptor",
24+
"MethodDescriptor",
25+
"TransportCodec",
26+
"SerializationEncoder",
27+
"SerializationDecoder",
28+
]
29+
30+
logger = logging.getLogger(__name__)
31+
32+
33+
@dataclass
34+
class ParameterDescriptor:
35+
"""Information about a method parameter"""
36+
37+
name: str
38+
annotation: Any
39+
is_required: bool = True
40+
default_value: Any = None
41+
42+
43+
@dataclass
44+
class MethodDescriptor:
45+
"""Method descriptor with function details"""
46+
47+
function: Callable
48+
name: str
49+
parameters: List[ParameterDescriptor]
50+
return_parameter: ParameterDescriptor
51+
documentation: Optional[str] = None
52+
53+
54+
class TransportCodec(abc.ABC):
55+
"""
56+
The transport codec interface.
57+
"""
58+
59+
@classmethod
60+
@abc.abstractmethod
61+
def get_transport_type(cls) -> str:
62+
"""
63+
Get transport type of current codec
64+
:return: The transport type.
65+
:rtype: str
66+
"""
67+
raise NotImplementedError()
68+
69+
@abc.abstractmethod
70+
def get_encoder(self) -> "SerializationEncoder":
71+
"""
72+
Get encoder instance
73+
:return: The encoder.
74+
:rtype: SerializationEncoder
75+
"""
76+
raise NotImplementedError()
77+
78+
@abc.abstractmethod
79+
def get_decoder(self) -> "SerializationDecoder":
80+
"""
81+
Get decoder instance
82+
:return: The decoder.
83+
:rtype: SerializationDecoder
84+
"""
85+
raise NotImplementedError()
86+
87+
88+
class SerializationEncoder(abc.ABC):
89+
"""
90+
The serialization encoder interface.
91+
"""
92+
93+
@abc.abstractmethod
94+
def encode(self, arguments: Tuple[Any, ...]) -> bytes:
95+
"""
96+
Encode arguments to bytes.
97+
:param arguments: The arguments to encode.
98+
:type arguments: Tuple[Any, ...]
99+
:return: The encoded bytes.
100+
:rtype: bytes
101+
"""
102+
raise NotImplementedError()
103+
104+
105+
class SerializationDecoder(abc.ABC):
106+
"""
107+
The serialization decoder interface.
108+
"""
109+
110+
@abc.abstractmethod
111+
def decode(self, data: bytes) -> Any:
112+
"""
113+
Decode bytes to object.
114+
:param data: The data to decode.
115+
:type data: bytes
116+
:return: The decoded object.
117+
:rtype: Any
118+
"""
119+
raise NotImplementedError()

src/dubbo/codec/dubbo_codec.py

Lines changed: 1 addition & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import logging
2020
from dataclasses import dataclass
2121
from typing import Any, Callable, Optional, List, Tuple
22+
from _interface import ParameterDescriptor, MethodDescriptor, SerializationDecoder, SerializationEncoder, TransportCodec
2223

2324
__all__ = [
2425
"ParameterDescriptor",
@@ -32,95 +33,6 @@
3233
logger = logging.getLogger(__name__)
3334

3435

35-
@dataclass
36-
class ParameterDescriptor:
37-
"""Information about a method parameter"""
38-
39-
name: str
40-
annotation: Any
41-
is_required: bool = True
42-
default_value: Any = None
43-
44-
45-
@dataclass
46-
class MethodDescriptor:
47-
"""Method descriptor with function details"""
48-
49-
function: Callable
50-
name: str
51-
parameters: List[ParameterDescriptor]
52-
return_parameter: ParameterDescriptor
53-
documentation: Optional[str] = None
54-
55-
56-
class TransportCodec(abc.ABC):
57-
"""
58-
The transport codec interface.
59-
"""
60-
61-
@classmethod
62-
@abc.abstractmethod
63-
def get_transport_type(cls) -> str:
64-
"""
65-
Get transport type of current codec
66-
:return: The transport type.
67-
:rtype: str
68-
"""
69-
raise NotImplementedError()
70-
71-
@abc.abstractmethod
72-
def get_encoder(self) -> "SerializationEncoder":
73-
"""
74-
Get encoder instance
75-
:return: The encoder.
76-
:rtype: SerializationEncoder
77-
"""
78-
raise NotImplementedError()
79-
80-
@abc.abstractmethod
81-
def get_decoder(self) -> "SerializationDecoder":
82-
"""
83-
Get decoder instance
84-
:return: The decoder.
85-
:rtype: SerializationDecoder
86-
"""
87-
raise NotImplementedError()
88-
89-
90-
class SerializationEncoder(abc.ABC):
91-
"""
92-
The serialization encoder interface.
93-
"""
94-
95-
@abc.abstractmethod
96-
def encode(self, arguments: Tuple[Any, ...]) -> bytes:
97-
"""
98-
Encode arguments to bytes.
99-
:param arguments: The arguments to encode.
100-
:type arguments: Tuple[Any, ...]
101-
:return: The encoded bytes.
102-
:rtype: bytes
103-
"""
104-
raise NotImplementedError()
105-
106-
107-
class SerializationDecoder(abc.ABC):
108-
"""
109-
The serialization decoder interface.
110-
"""
111-
112-
@abc.abstractmethod
113-
def decode(self, data: bytes) -> Any:
114-
"""
115-
Decode bytes to object.
116-
:param data: The data to decode.
117-
:type data: bytes
118-
:return: The decoded object.
119-
:rtype: Any
120-
"""
121-
raise NotImplementedError()
122-
123-
12436
class DubboSerializationService:
12537
"""Dubbo serialization service with type handling"""
12638

0 commit comments

Comments
 (0)