我想用SnakeYAML(或其他一些解析器)解析Docker-compose.yml。我遇到的问题是属性可以是String或更复杂的对象,如configs,它支持“短”语法和“长”语法:
短
version: "3.3"
services:
redis:
image: redis:latest
deploy:
replicas: 1
configs:
- my_config
- my_other_config
configs:
my_config:
file: ./my_config.txt
my_other_config:
external: true
龙
version: "3.3"
services:
redis:
image: redis:latest
deploy:
replicas: 1
configs:
- source: my_config
target: /redis_config
uid: '103'
gid: '103'
mode: 0440
configs:
my_config:
file: ./my_config.txt
my_other_config:
external: true
我如何将其映射到SnakeYAML?目前我已经创建了Java Beans来模拟不同的元素,并将模型配置为List<String>
,但这显然只适用于短语法。
如果SnakeYaml遇到它希望反序列化对象的String,它会查找带有String参数的构造函数。
因此,在示例中,为config的长语法定义JavaBean就足够了,然后将构造函数添加到以短语法作为参数的构造函数中。