将不同的架构与联合值相关联的惯用方式是什么?

时间:2019-11-09 19:55:54

标签: dhall

我正在尝试使用Dhall类型表示Zuul-CI项目的管道系统: 管道可以使用具有不同触发事件的不同连接。

我想提供一个默认管道,该管道可以通过以下方式为每种类型的连接设置正确的触发事件:

  ⊢ RenderPipeline CheckPipeline::{ connections = [ ConnectionTypes.GitHub ] }

  - pipeline:
    name: check
    trigger:
      github:
        action: open
        event: pull-request

  ⊢ RenderPipeline CheckPipeline::{ connections = [ ConnectionTypes.Gerrit ] }

  - pipeline:
    name: check
    trigger:
      gerrit:
        event: patchset-created

  ⊢ RenderPipeline CheckPipeline::{ connections = [ ConnectionTypes.Gerrit, ConnectionTypes.GitHub ] }

  - pipeline:
      name: check
      trigger:
        gerrit:
          event: patchset-created
        github:
          action: open
          event: pull-request

我必须对UnionTrigger使用Union,因为merge函数希望该值是单一类型。

是否可以通过某种方式将GerritTrigger与ConnectionTypes.Gerrit关联,并避免在CheckPipeline定义中键入事件类型(例如,删除ConnectionTrigger.Gerrit注释)?

let Prelude =
      https://raw.githubusercontent.com/dhall-lang/dhall-lang/v11.1.0/Prelude/package.dhall sha256:99462c205117931c0919f155a6046aec140c70fb8876d208c7c77027ab19c2fa

let ConnectionTypes
    : Type
    = < Gerrit | GitHub >

let GerritTrigger
    : Type
    = { event : Text }

let GitHubTrigger
    : Type
    = { event : Text, action : Text }

let ConnectionTrigger
    : Type
    = < Gerrit : GerritTrigger | GitHub : GitHubTrigger >

let Pipeline
    : Type
    = { name : Text
      , trigger : { Gerrit : ConnectionTrigger, GitHub : ConnectionTrigger }
      , connections : List ConnectionTypes
      }

let CheckPipeline =
      { Type = Pipeline
      , default =
          { name = "check"
          , trigger =
              -- "Here, can this be improved so that Gerrit is not mentioned twice?"
              { Gerrit = ConnectionTrigger.Gerrit { event = "patchset-created" }
              , GitHub =
                  ConnectionTrigger.GitHub
                    { event = "pull-request", action = "open" }
              }
          }
      }

let PipelineRenderTrigger
    : Type
    = { mapKey : Text, mapValue : ConnectionTrigger }

let RenderPipeline =
        λ(pipeline : Pipeline)
      → [ { pipeline =
              { name = pipeline.name
              , trigger =
                  Prelude.List.map
                    ConnectionTypes
                    PipelineRenderTrigger
                    (   λ(connection : ConnectionTypes)
                      → { mapKey =
                            merge
                              { Gerrit = "gerrit", GitHub = "github" }
                              connection
                        , mapValue = merge pipeline.trigger connection
                        }
                    )
                    pipeline.connections
              }
          }
        ]

in  RenderPipeline CheckPipeline::{ connections = [ ConnectionTypes.GitHub ] }

预先感谢:)

1 个答案:

答案 0 :(得分:0)

是的,您可以在将sample(c(-1,2), 100, rep=TRUE)字段作为处理程序的记录传递到win.lose <- function(){ total <- 90 for(i in c(1:100)){ bet_output <- sample(c(-1,2), 1, rep=TRUE) total <- total + 10 * bet_output if(total<0) return(total) } return(total) } sim_y <- unlist(lapply(c(1:10000), function(i){win.lose()} )) sum(sim_y < 0)/length(sim_y) 之前进行转换。这样,用户不必自己包装触发器。 triggers函数为他们做到了:

merge