Symfony YAML格式转换 [英] Symfony YAML format conversion
问题描述
MyService:
class:Some \Class\\ \\
工厂:
- SomeFactoryHere
- 方法
调用:
- [add,[@ = service('AnotherService1')。create(service(' serviceService('AnotherService3'),service('AnotherService3'))]]
恕我直言,这可以更可读,如果转换为这样的事情:
MyService:
class:Some \Class \Here
factory:
- SomeFactoryHere
- method
calls:
-
add,
@ = service('AnotherService1' ).create(
service('AnotherService2'),
service('AnotherService3'),
service('AnotherService3')
)
$ b但是,这是无效的YAML(Symfony解析器失败),我的问题是如何将这个配置可以转换为上面的东西?
UPD#1
查看 Symfony YAML格式转换:调用"用字符串参数 - 重要的细微差别在那里。
解决方案打破字符串的最好方法
(ServiceService('AnotherService2')),service('AnotherService3'),service('AnotherService3()); $ service $ '))
是通过使用剥离的折叠块标量。这个限制是你不能使用反斜线来转义任何特殊字符,但是这些不在你的例子中(你在标量周围需要的原因是它以一个
@
这是一个保留字符)。
然后你也必须正确地重新表示你的结构有@flyx已经指出: 给出: 调用
的值是一个序列,第一个元素是标量 add $
$ b $ pre $ import yaml
yaml_str =\
MyService:
class:Some \Class\Here
factory:
- SomeFactoryHere
- method
调用:
- - 添加
- - > -
@ = service('AnotherService1')创建(
service('AnotherService2'),
服务('Ano (ServiceService3'),
service('AnotherService3'))
data = yaml.safe_load(yaml_str)
print(data)
请注意这给了 .create(
和 service(
。
Symphony使用的YAML解析器似乎不能解析上面的(虽然它是正确的)。您也可以尝试:
MyService:
class:Some \Class\Here
factory:
- SomeFactoryHere
- method
calls:
-
- add
-
- > -
@ = service( 'AnotherService1')。create(
service('AnotherService2'),
service('AnotherService3'),
service('AnotherService3'))
I have some service definition that looks like this:
MyService:
class: Some\Class\Here
factory:
- SomeFactoryHere
- method
calls:
- [add, ["@=service('AnotherService1').create(service('AnotherService2'), service('AnotherService3'), service('AnotherService3'))"]]
IMHO, this can be more readable if converted to something like this:
MyService:
class: Some\Class\Here
factory:
- SomeFactoryHere
- method
calls:
-
add,
"@=service('AnotherService1').create(
service('AnotherService2'),
service('AnotherService3'),
service('AnotherService3')
)"
But, this is not valid YAML (Symfony parser fails), and my question is how this config can be converted to something like above?
UPD#1
Look at Symfony YAML format conversion: "calls" with string params - important nuances are there.
The best way to break up your string
"@=service('AnotherService1').create(service('AnotherService2'), service('AnotherService3'), service('AnotherService3'))"
is by using a stripped folded block scalar. The limitation for this is that you cannot escape any special characters with backslashes, but those are not in your example (the reason you need ""
around your scalar is because it starts with an @
which is a reserved character).
Then you also have to correctly re-represent the structure that you have, as @flyx already indicated: the value for calls
is a sequence, the first element of which is a list of the scalar add
and sequence consisting of the aforementioned long scalar that you want to break up for readability:
import yaml
yaml_str = """\
MyService:
class: Some\Class\Here
factory:
- SomeFactoryHere
- method
calls:
- - add
- - >-
@=service('AnotherService1').create(
service('AnotherService2'),
service('AnotherService3'),
service('AnotherService3'))
"""
data = yaml.safe_load(yaml_str)
print(data)
gives:
"@=service('AnotherService1').create( service('AnotherService2'), service('AnotherService3'), service('AnotherService3'))"
Please note that this gives an extra space between .create(
and service(
.
The YAML parser that Symphony uses seems not to be able to parse the above (although it is correct). You can alternatively try:
MyService:
class: Some\Class\Here
factory:
- SomeFactoryHere
- method
calls:
-
- add
-
- >-
@=service('AnotherService1').create(
service('AnotherService2'),
service('AnotherService3'),
service('AnotherService3'))
这篇关于Symfony YAML格式转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!