rewrite icon indicating copy to clipboard operation
rewrite copied to clipboard

Declarative Preconditions - `or` association

Open nmck257 opened this issue 2 years ago • 0 comments

What problem are you trying to solve?

Declarative preconditions currently associate via AND if you declare multiple on the same recipe. If you want to achieve an OR relationship, you can do it, but through a much more verbose "diamond" pattern.

Sample:

---
type: specs.openrewrite.org/v1beta/recipe
name: org.sample.DoSomething
displayName: Do Something
recipeList:
  - org.sample.DoSomething.condition.1
  - org.sample.DoSomething.condition.2
  - org.sample.DoSomething.condition.3

---
type: specs.openrewrite.org/v1beta/recipe
name: org.sample.DoSomething.condition.1
displayName: Do Something (condition 1)
preconditions:
  - org.openrewrite.FindSourceFiles:
      filePattern: "**/my.json"
recipeList:
  - org.sample.DoSomething.for.real

---
type: specs.openrewrite.org/v1beta/recipe
name: org.sample.DoSomething.condition.2
displayName: Do Something (condition 2)
preconditions:
  - org.openrewrite.FindSourceFiles:
      filePattern: "**/your.json"
recipeList:
  - org.sample.DoSomething.for.real  

---
type: specs.openrewrite.org/v1beta/recipe
name: org.sample.DoSomething.condition.3
displayName: Do Something (condition 3)
preconditions:
  - org.openrewrite.FindSourceFiles:
      filePattern: "**/our.json"
recipeList:
  - org.sample.DoSomething.for.real

---
type: specs.openrewrite.org/v1beta/recipe
name: org.sample.DoSomething.for.real
displayName: Do Something (for real)
recipeList:
  - org.openrewrite.json.ChangeKey:
      qwe: qwe

(if you imagine graphing how those 5 recipes relate to each other, it kinda looks like a diamond)

Describe the solution you'd like

---
type: specs.openrewrite.org/v1beta/recipe
name: org.sample.DoSomething
displayName: Do Something
preconditionAssociation: OR
preconditions:
  - org.openrewrite.FindSourceFiles:
      filePattern: "**/my.json"
  - org.openrewrite.FindSourceFiles:
      filePattern: "**/your.json"
  - org.openrewrite.FindSourceFiles:
      filePattern: "**/our.json"
recipeList:
  - org.openrewrite.json.ChangeKey:
      qwe: qwe

Have you considered any alternatives or workarounds?

Support for passing nested recipes as recipe arguments in declarative recipes? :) This could allow something like:

---
type: specs.openrewrite.org/v1beta/recipe
name: org.sample.DoSomething
displayName: Do Something
preconditionAssociation: OR
preconditions:
  - org.openrewrite.Preconditions.Or:
      recipeList:
        - org.openrewrite.FindSourceFiles:
            filePattern: "**/my.json"
        - org.openrewrite.FindSourceFiles:
            filePattern: "**/your.json"
        - org.openrewrite.FindSourceFiles:
            filePattern: "**/our.json"
recipeList:
  - org.openrewrite.json.ChangeKey:
      qwe: qwe

Additional context

cc @sambsnyd

Are you interested in contributing this feature to OpenRewrite?

Assuming we can bless a design, yeah, I suspect the code won't be too treacherous

nmck257 avatar Feb 14 '24 20:02 nmck257