Skip to content

Commit d81eac5

Browse files
committed
fix: second attempt at cyclomatic for token info
Signed-off-by: exploreriii <[email protected]>
1 parent f249c86 commit d81eac5

File tree

1 file changed

+76
-53
lines changed

1 file changed

+76
-53
lines changed

src/hiero_sdk_python/tokens/token_info.py

Lines changed: 76 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from hiero_sdk_python._deprecated import _DeprecatedAliasesMixin
2929
from hiero_sdk_python.tokens.custom_fee import CustomFee
3030
from hiero_sdk_python.hapi.services import token_get_info_pb2 as hapi_pb
31-
from hiero_sdk_python.hapi.services import basic_types_pb2 as basic_pb
31+
3232

3333
@dataclass(init=False)
3434
class TokenInfo(_DeprecatedAliasesMixin):
@@ -90,7 +90,7 @@ class TokenInfo(_DeprecatedAliasesMixin):
9090
"pauseStatus": "pause_status",
9191
"supplyType": "supply_type",
9292
"customFees": "custom_fees",
93-
}
93+
}
9494

9595
def __init__(self, **kwargs: Any) -> None:
9696
# 1) Translate deprecated camelCase names → snake_case, with warnings
@@ -132,7 +132,7 @@ def set_kyc_key(self, kyc_key: PublicKey) -> "TokenInfo":
132132
"""Set the KYC key."""
133133
self.kyc_key = kyc_key
134134
return self
135-
135+
136136
# alias for backwards compatibility
137137
set_kycKey = set_kyc_key
138138

@@ -245,6 +245,40 @@ def set_custom_fees(self, custom_fees: List[Any]) -> "TokenInfo":
245245
# alias for backwards compatibility
246246
set_customFees = set_custom_fees
247247

248+
# === helpers ===
249+
@staticmethod
250+
def _copy_key_if_present(dst: "TokenInfo", setter: str, key_msg) -> None:
251+
# In proto3, keys are a oneof; check presence via WhichOneof
252+
if key_msg is not None and hasattr(key_msg, "WhichOneof") and key_msg.WhichOneof("key"):
253+
getattr(dst, setter)(PublicKey._from_proto(key_msg))
254+
255+
@staticmethod
256+
def _parse_custom_fees(proto_obj) -> List[CustomFee]:
257+
out: List[CustomFee] = []
258+
for fee_proto in getattr(proto_obj, "custom_fees", []): # snake_case matches your generated proto
259+
if fee_proto.HasField("fixed_fee"):
260+
out.append(CustomFixedFee._from_proto(fee_proto))
261+
elif fee_proto.HasField("fractional_fee"):
262+
out.append(CustomFractionalFee._from_proto(fee_proto))
263+
elif fee_proto.HasField("royalty_fee"):
264+
out.append(CustomRoyaltyFee._from_proto(fee_proto))
265+
return out
266+
267+
@staticmethod
268+
def _copy_msg_to_proto(src_obj, dst_proto, src_attr: str, dst_field: str) -> None:
269+
val = getattr(src_obj, src_attr)
270+
if val:
271+
getattr(dst_proto, dst_field).CopyFrom(val._to_proto())
272+
273+
@staticmethod
274+
def _get(proto_obj, *names):
275+
"""Get the first present attribute from a list of possible names (camelCase/snake_case)."""
276+
for n in names:
277+
if hasattr(proto_obj, n):
278+
return getattr(proto_obj, n)
279+
return None
280+
281+
# === conversions ===
248282
@classmethod
249283
def _from_proto(cls, proto_obj: hapi_pb.TokenInfo) -> "TokenInfo":
250284
"""
@@ -267,43 +301,35 @@ def _from_proto(cls, proto_obj: hapi_pb.TokenInfo) -> "TokenInfo":
267301
metadata=proto_obj.metadata,
268302
)
269303

270-
fees: List[CustomFee] = []
271-
for fee_proto in getattr(proto_obj, "custom_fees", []): # snake_case matches your generated proto
272-
if fee_proto.HasField("fixed_fee"):
273-
fees.append(CustomFixedFee._from_proto(fee_proto)) # pass the wrapper
274-
elif fee_proto.HasField("fractional_fee"):
275-
fees.append(CustomFractionalFee._from_proto(fee_proto)) # pass the wrapper
276-
elif fee_proto.HasField("royalty_fee"):
277-
fees.append(CustomRoyaltyFee._from_proto(fee_proto)) # pass the wrapper
278-
279-
tokenInfoObject.set_custom_fees(fees)
280-
281-
for proto_attr, setter in [
282-
("adminKey", "set_admin_key"),
283-
("kycKey", "set_kyc_key"),
284-
("freezeKey", "set_freeze_key"),
285-
("wipeKey", "set_wipe_key"),
286-
("supplyKey", "set_supply_key"),
287-
("metadata_key", "set_metadata_key"),
288-
("fee_schedule_key", "set_fee_schedule_key"),
289-
("pause_key", "set_pause_key"),
290-
]:
291-
key_field = getattr(proto_obj, proto_attr)
292-
if key_field.WhichOneof("key"):
293-
getattr(tokenInfoObject, setter)(PublicKey._from_proto(key_field))
294-
295-
for proto_attr, setter, conv in [
296-
("defaultFreezeStatus", "set_default_freeze_status", TokenFreezeStatus._from_proto),
297-
("defaultKycStatus", "set_default_kyc_status", TokenKycStatus._from_proto),
298-
("autoRenewAccount", "set_auto_renew_account", AccountId._from_proto),
299-
("autoRenewPeriod", "set_auto_renew_period", Duration._from_proto),
300-
("expiry", "set_expiry", Timestamp._from_protobuf),
301-
("pause_status", "set_pause_status", TokenPauseStatus._from_proto),
302-
("supplyType", "set_supply_type", SupplyType),
303-
]:
304-
val = getattr(proto_obj, proto_attr)
304+
tokenInfoObject.set_custom_fees(cls._parse_custom_fees(proto_obj))
305+
306+
key_sources = [
307+
(("adminKey",), "set_admin_key"),
308+
(("kycKey",), "set_kyc_key"),
309+
(("freezeKey",), "set_freeze_key"),
310+
(("wipeKey",), "set_wipe_key"),
311+
(("supplyKey",), "set_supply_key"),
312+
(("metadataKey", "metadata_key"), "set_metadata_key"),
313+
(("feeScheduleKey", "fee_schedule_key"),"set_fee_schedule_key"),
314+
(("pauseKey", "pause_key"), "set_pause_key"),
315+
]
316+
for names, setter in key_sources:
317+
key_msg = cls._get(proto_obj, *names)
318+
cls._copy_key_if_present(tokenInfoObject, setter, key_msg)
319+
320+
conv_map = [
321+
(("defaultFreezeStatus",), tokenInfoObject.set_default_freeze_status, TokenFreezeStatus._from_proto),
322+
(("defaultKycStatus",), tokenInfoObject.set_default_kyc_status, TokenKycStatus._from_proto),
323+
(("autoRenewAccount",), tokenInfoObject.set_auto_renew_account, AccountId._from_proto),
324+
(("autoRenewPeriod",), tokenInfoObject.set_auto_renew_period, Duration._from_proto),
325+
(("expiry",), tokenInfoObject.set_expiry, Timestamp._from_protobuf),
326+
(("pauseStatus", "pause_status"), tokenInfoObject.set_pause_status, TokenPauseStatus._from_proto),
327+
(("supplyType",), tokenInfoObject.set_supply_type, SupplyType),
328+
]
329+
for names, setter, conv in conv_map:
330+
val = cls._get(proto_obj, *names)
305331
if val is not None:
306-
getattr(tokenInfoObject, setter)(conv(val))
332+
setter(conv(val))
307333

308334
return tokenInfoObject
309335

@@ -322,28 +348,25 @@ def _to_proto(self) -> hapi_pb.TokenInfo:
322348
tokenType=(self.token_type.value if self.token_type else None),
323349
supplyType=(self.supply_type.value if self.supply_type else None),
324350
maxSupply=self.max_supply,
325-
expiry=self.expiry._to_protobuf() if self.expiry else None,
326351
ledger_id=self.ledger_id,
327352
metadata=self.metadata,
328353
)
329354

330-
for fee in self.custom_fees:
355+
for fee in self.custom_fees or []:
331356
proto.custom_fees.append(fee._to_proto())
332357

333-
for attr, field in [
334-
("admin_key", "adminKey"),
335-
("kyc_key", "kycKey"),
336-
("freeze_key", "freezeKey"),
337-
("wipe_key", "wipeKey"),
338-
("supply_key", "supplyKey"),
339-
("metadata_key", "metadata_key"),
340-
("fee_schedule_key", "fee_schedule_key"),
341-
("pause_key", "pause_key"),
358+
for src_attr, dst_field in [
359+
("admin_key", "adminKey"),
360+
("kyc_key", "kycKey"),
361+
("freeze_key", "freezeKey"),
362+
("wipe_key", "wipeKey"),
363+
("supply_key", "supplyKey"),
364+
("metadata_key", "metadata_key"),
365+
("fee_schedule_key", "fee_schedule_key"),
366+
("pause_key", "pause_key"),
342367
("auto_renew_account", "autoRenewAccount"),
343368
]:
344-
val = getattr(self, attr)
345-
if val:
346-
getattr(proto, field).CopyFrom(val._to_proto())
369+
self._copy_msg_to_proto(self, proto, src_attr, dst_field)
347370

348371
if self.is_deleted is not None:
349372
proto.deleted = self.is_deleted

0 commit comments

Comments
 (0)