openrewrite / rewrite

Automated mass refactoring of source code.

Home Page:https://docs.openrewrite.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add or merge content to yaml file

nicolasb29 opened this issue · comments

What problem are you trying to solve?

I have a yaml file and I want to append (or merge) yaml content to it

What precondition(s) should be checked before applying this recipe?

If the input content is yaml

Describe the situation before applying the recipe

existing-content:
  languages:
    - french
    - english

Describe the situation after applying the recipe

With input content

new-content:
  country:
    - France
    - UK

I want to have

existing-content:
  languages:
    - french
    - english
new-content:
  country:
    - France
    - UK

Have you considered any alternatives or workarounds?

I tried with AppendToTextFile but I have an exception java.lang.ClassCastException: class org.openrewrite.yaml.tree.Yaml$Documents cannot be cast to class org.openrewrite.text.PlainText

It is working with org.openrewrite.java.spring.AddSpringProperty

Hi! Did you already look at our existing MergeYaml recipe? Does that (not) work for you?

Hi!

Is it possible to use this recipe in a declarative recipe in Yaml without writing my own recipe in Java? Because I don't see an option for a file (or file pattern).

Right now that recipe is usable from declarative recipes as well: https://docs.openrewrite.org/recipes/yaml/mergeyaml
You can limit what files this is applied to through preconditions: https://docs.openrewrite.org/reference/yaml-format-reference#preconditions

Oh good ! I will try and close this issue if all is ok !

Hi!

It's working fine!! Thanks!

Only one question about preconditions, is there a way to do a negation, for example "if not found in files" with this

We don't yet have negation in the declarative yaml recipes; we only have that when writing Java recipes

public static TreeVisitor<?, ExecutionContext> not(TreeVisitor<?, ExecutionContext> v) {

We'd like to keep logic out of the yaml files if we can, as that's fairly hard to maintain going forward.