Pergunta

Hey guys I'm new to Grails and I'm getting to the point where I'm learning about the BootStrap.groovy file. I see that you can create each model and save them one at a time into the database, but is there anyway to load them from a YAML file or something similar. I'm coming from the Play framework which supports something like that. I already have a YAML file with about 80 objects to seed the database with and I really don't want to re write that into new MyObject({blah:blah, blah:blah}).save() for all of them. Is there anything out there that will handle this for me?

Foi útil?

Solução

So I discovered the Groovy Library for JYaml, which allows you to instantiate classes from YAML files. Here is the example they give:

@Grab('org.jyaml:jyaml:1.3')
import org.ho.yaml.*

class Staff {
    def firstname, lastname, position
}

input = '''
firstname: John
lastname: Connor
position: Resistance Leader
'''
Staff s = Yaml.loadType(input, Staff)
println s.dump()
// => <Staff@c05d3b firstname=John lastname=Connor position=Resistance Leader>

----EDIT----

As stated in the comments, JYAML is no longer being supported and SnakeYAML is the way to go for parsing YAML files. See the SnakeYAML examples here.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top