VirtusLab / besom

Besom - a Pulumi SDK for Scala. Also, incidentally, a broom made of twigs tied round a stick. Brooms and besoms are used for protection, to ward off evil spirits, and cleansing of ritual spaces.
https://virtuslab.github.io/besom/
Apache License 2.0
114 stars 7 forks source link

Consider adding lifted methods for `Output[List[A]]` and `Output[Option[List[A]]]`, and `Output[Option[A]]` #424

Closed pawelprazak closed 2 months ago

pawelprazak commented 3 months ago

In the following use case:

val nginxService = Service(
    "nginx",
    ServiceArgs(
      spec = ServiceSpecArgs(
        selector = labels,
        `type` = k8s.core.v1.enums.ServiceSpecType.LoadBalancer,
        ...
      ),...
  )

  Stack.exports(
    nginxUrl = nginxService.status.loadBalancer.ingress.head.hostname
  )

what is missing is:

  extension [A](output: besom.types.Output[scala.Option[scala.collection.immutable.List[A]]])
    def head: besom.types.Output[scala.Option[A]] = output.map(_.map(_.head))

We should consider adding lifts for both Output[List[A]] and Output[Option[List[A]]], for the essential methods: i.e.: head, tail, get, etc. (mapList?), but adding every single method does not feel practical.

EDIT:

A more complex use case:

  extension [A](output: Output[Option[A]])
    def orElse[B >: A](alternative: Output[Option[B]]): Output[Option[B]] =
      output.flatMap(o => alternative.map(a => o.orElse(a)))

  extension [A](output: Output[Option[List[A]]]) 
    def headOption: Output[Option[A]] = output.map(_.flatMap(_.headOption))

  val maybeIngress = nginxService.status.loadBalancer.ingress.headOption
  val hostnameOrIp = maybeIngress.hostname.orElse(maybeIngress.ip)

  Stack.exports(
    nginxHostname = p"http://${hostnameOrIp.getOrElse("unknown")}:$nginxPortNumber"
  )
lbialy commented 3 months ago

Definitely!

On Thu 28. Mar 2024 at 14:45, Paweł Prażak @.***> wrote:

In the following use case:

val nginxService = Service( "nginx", ServiceArgs( spec = ServiceSpecArgs( selector = labels, type = k8s.core.v1.enums.ServiceSpecType.LoadBalancer, ... ),... )

Stack.exports( nginxUrl = nginxService.status.loadBalancer.ingress.head.hostname )

what is missing is:

extension [A](output: besom.types.Output[scala.Option[scala.collection.immutable.List[A]]]) def head: besom.types.Output[scala.Option[A]] = output.map(.map(.head))

We should consider adding lifts for both Output[List[A]] and Output[Option[List[A]]], for the essential methods: i.e.: head, tail, get, etc., but adding every single method does not feel practical.

— Reply to this email directly, view it on GitHub https://github.com/VirtusLab/besom/issues/424, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACBVNURZAUNKF3RK2FYEQ7DY2QGHNAVCNFSM6AAAAABFM2E2YSVHI2DSMVQWIX3LMV43ASLTON2WKOZSGIYTGMRYGUZTONI . You are receiving this because you are subscribed to this thread.Message ID: @.***>