#Return custom type
1 messages · Page 1 of 1 (latest)
Yes, as long as the other object is in the same module. From that error ("expected iterable") I assume what you have is different from what you shared here. Can you share the signature of the function you're calling, and the command you're using?
@object_type
class ServiceContainer:
name: str = field()
port: int = field()
ctr: dagger.Service
@object_type
class Localservice:
services: list[ServiceContainer] = field(
default=lambda: ServiceContainer(
name="localstack",
port=4566,
ctr=dag.container()
.from_("localstack/localstack:latest")
.with_exposed_port(4566)
.as_service(),
)
)
@function
async def add_service(self, name: str, path: Directory) -> "Localservice":
port = random.choice(PORTS)
ctr = dag.container().build(path).with_exposed_port(port).as_service()
self.services.append(ServiceContainer(name=name, port=port, ctr=ctr))
PORTS.remove(port)
return self
@function
async def list_services(self) -> str:
return ", ".join(
[f"{service.name} ({service.port})" for service in self.services]
)
Context: local testing of multiple containers + localstack
Have somewhat reworked this to avoid it:
@object_type
class Localservice:
proxy = dag.proxy()
@function
async def add_service(self, name: str, path: Directory) -> "Localservice":
port = random.choice(PORTS)
ctr = dag.container().build(path).with_exposed_port(port).as_service()
self.proxy.with_service(ctr, name, port, 80)
PORTS.remove(port)
return self
@function
async def run(self) -> dagger.Service:
return self.proxy.service()
But out of interest I'd like to see the solution if it's possible
Your default value for services is returning a single ServiceContainer but it's defined as list[ServiceContainer].
That did it. Always something small. Thanks!
Unrelated but you seem to be mutating a global PORTS. I'm afraid you may get unexpected behavior because each function runs in their own runtime container. They don't share the same process/memory. In order to preserve that state it would have to be a field in an object.
I'm going to rework that and ask the user for a port anyway 🙂
You can also let the engine assign a random one. Better chance to avoid a conflict. If random is what you actually want.
Remind me how to do that? Don't think I recall ever doing so before