0xFableOrg / roll-op

The simplest way to spin your own dev / prod rollup, fully configurable.
BSD 3-Clause Clear License
92 stars 25 forks source link

Use a dataclass for address configuration #75

Open norswap opened 11 months ago

norswap commented 11 months ago

Right now, we have a bunch of attributes to configure a single address, e.g.

self.l1_rpc_protocol = "http"
self.l1_rpc_host = "127.0.0.1"
self.l1_rpc_path = ""
self.l1_rpc_port = 8545

It would be good to be able to replace this with a single data class. The key question is if we can get the TOML parser to automatically parse this data structure.

Fatal1ty commented 11 months ago

I would recommend to take a look at mashumaro (which has TOML support out of the box) or another library. It will take a lot of manual work and error handling away from you.

In your case, it might look something like this:

from dataclasses import dataclass
from enum import Enum
from ipaddress import IPv4Address

from mashumaro.mixins.toml import DataClassTOMLMixin

class L1RPCProtocol(Enum):
    HTTP = "http"

@dataclass
class Config(DataClassTOMLMixin):
    l1_rpc_protocol: L1RPCProtocol
    l1_rpc_host: IPv4Address
    l1_rpc_path: str
    l1_rpc_port: int

config = Config.from_toml(...)
norswap commented 11 months ago

Thanks, that's useful to know!