Updating list inside optional record field

I have a file: blueprint.dhall which contains a default kubernetes.Pod according to the types in dhall-kubernetes/package.dhall at v6.0.0 · dhall-lang/dhall-kubernetes · GitHub
I have several files that will modify this default pod, e.g. by adding another container to it.

This means to update a list inside the optional spec field in the pod record, dhall-kubernetes/io.k8s.api.core.v1.Pod.dhall at v6.0.0 · dhall-lang/dhall-kubernetes · GitHub.

This is my approach:

let addOrMergeContainerToPod : kubernetes.Container.Type -> kubernetes.Pod.Type -> kubernetes.Pod.Type =
      \(c : kubernetes.Container.Type) ->
      \(p : kubernetes.Pod.Type) ->
      p with spec =
      (
            if Prelude.Optional.null kubernetes.PodSpec.Type p.spec != True
            then Prelude.Optional.map kubernetes.PodSpec.Type kubernetes.PodSpec.Type (\(spec : kubernetes.PodSpec.Type) -> spec with containers = [c] # spec.containers) p.spec
            else Some kubernetes.PodSpec::{containers = [c]  }
      )

But this feels cumbersome.
Is there a smarter approach?

The way you are updating a default value makes me think you don’t need the output be Optional anymore. How about using default?

Something like

let default = kubernetes.PodSpec::{containers = [c]  }
let modified = Prelude.Optional.map kubernetes.PodSpec.Type kubernetes.PodSpec.Type (\(spec : kubernetes.PodSpec.Type) -> spec with containers = [c] # spec.containers) p.spec
in Prelude.Optional.default kubernetes.PodSpec.Type  default modified
1 Like

That looks reasonable. Let me give it a go and report back.

Adopting it. Thanks :smiley: