Read a YAML file in Groovy

15,141

The withReader block implicitly returns the last line of the closure, which in your case is:

        println "YamlTapeLoader : inside readFrom : "+reader

And println returns null, so change the code to:

    def tape = file.withReader(FILE_CHARSET) { reader ->
        def ret = YamlTape.readFrom(reader)
        println "YamlTapeLoader : inside readFrom : "+reader
        ret // Return the result of YamlTape.readFrom
    }

And it should work

Edit

Your readFrom method has the same error... Change it to:

static YamlTape readFrom(Reader reader) {
  try {
    println "YamlTape : inside readFrom reader.size() = "+reader+" YamlTape: "+YamlTape

    def ret = Yaml.loadAs(reader, YamlTape)
    println "YamlTape : after readfrom"

    ret // Return the YamlTape
  } catch (YAMLException e) {
    println "YamlTape : inside catch block"
    throw new TapeLoadException('Invalid tape', e)
  }
}
Share:
15,141

Related videos on Youtube

Warrior
Author by

Warrior

I am a software engineer.I have to learn lot in this field.

Updated on May 25, 2022

Comments

  • Warrior
    Warrior almost 2 years

    I am trying to load an existing YAML file (which uses snakeYaml library) in my Groovy project. I have a class called YamlTape.groovy which contains method to load the YAML file using the following code.

    static YamlTape readFrom(Reader reader) {
        try {
            println "YamlTape : inside readFrom reader.size() = "+reader+" YamlTape: "+YamlTape
    
            yaml.loadAs(reader, YamlTape)
            println "YamlTape : after readfrom"
        } catch (YAMLException e) {
            println "YamlTape : inside catch block"
            throw new TapeLoadException('Invalid tape', e)
        }
    }
    

    and trying to call this method from another groovy class.

    Code:

        YamlTape loadTape(String name) {
        println "YamlTapeLoader : inside loadTape"
        def file = fileFor(name)
        println "YamlTapeLoader : inside loadTape filename -name: "+name
        println "YamlTapeLoader : inside loadTape filename -file: "+file
    
        file.setReadable(true);
        file.setWritable(true);
    
        if (file.isFile()) {
            println "YamlTapeLoader : inside file.isFile() : "+file.isFile()
            def tape = file.withReader(FILE_CHARSET) { reader ->
                YamlTape.readFrom(reader)
    
                println "YamlTapeLoader : inside readFrom : "+reader
            }
            println "YamlTapeLoader : tape : "+tape
    
    
            tape
        } else {
            println "YamlTapeLoader : inside ELSE : "
            new YamlTape(name: name)
        }
    }
    

    But the tape variable in load tape method always returns null. I have added some logs and found the code is able to access the yaml file but unable to parse Yaml document and return as Java Object.

    Logs are :

    YamlTapeLoader : inside loadTape
    YamlTapeLoader : inside loadTape filename -name: kar
    YamlTapeLoader : inside loadTape filename -file: /Users/Shared/AATest/Record/kar.yaml
    YamlTapeLoader : inside file.isFile() : true
    YamlTape : inside readFrom reader.size() = java.io.LineNumberReader@34189cab YamlTape: class co.freeside.betamax.tape.yaml.YamlTape
    YamlTape : inside getYaml 
    YamlTape : representer = co.freeside.betamax.tape.yaml.TapeRepresenter@201a503f
    YamlTape : constructor = org.yaml.snakeyaml.constructor.Constructor@16e7eec9
    YamlTape : dumperOption = org.yaml.snakeyaml.DumperOptions@39d7af3
    YamlTape : after readfrom
    YamlTapeLoader : inside readFrom : java.io.LineNumberReader@34189cab
    YamlTapeLoader : tape : null
    
  • Warrior
    Warrior over 10 years
    Still the def tape is returning null. Actually i am trying to load an existing .yaml file using snakeYaml in my groovy project. I have a class called YamlTape.groovy which contains method to load the Yaml file
  • tim_yates
    tim_yates over 10 years
    Are you actually using betamax, or are you just using it as sideways way to load yaml? If you're just trying to load yaml, you don't need to import the whole of betamax
  • tim_yates
    tim_yates over 10 years
    @Warrior actually, you're doing the same in your readFrom method...see edit
  • Warrior
    Warrior over 10 years
    We are using betamax to record the restful service in the yaml file. I am able to record it but when try to reload the yaml file it crashes.