简体   繁体   English

Symfony YAML格式转换

[英]Symfony YAML format conversion

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? 但是,这不是有效的YAML(Symfony解析器失败),我的问题是如何将此配置转换为类似上面的内容?

UPD#1 UPD#1

Look at Symfony YAML format conversion: "calls" with string params - important nuances are there. 看一下Symfony YAML格式转换:带有字符串参数的“调用” -那里很重要。

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: 然后,你还必须正确地重新代表你具有这样的结构,如@flyx已经指出:该值calls是一个序列,第一要素之一是标量的列表add由上述长标和序列您想分拆以提高可读性:

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( . 请注意,这在.create(service(

The YAML parser that Symphony uses seems not to be able to parse the above (although it is correct). Symphony使用的YAML解析器似乎无法解析以上内容(尽管它是正确的)。 You can alternatively try: 您也可以尝试:

MyService:
    class: Some\Class\Here
    factory:
        - SomeFactoryHere
        - method
    calls:
        - 
          - add
          - 
            - >-
              @=service('AnotherService1').create(
              service('AnotherService2'),
              service('AnotherService3'),
              service('AnotherService3'))

What you wrote is valid YAML. 您写的是有效的YAML。 If the Symfony parser fails, it has a bug. 如果Symfony解析器失败,则说明存在错误。 But anyway, the second YAML does not represent the same structure as the first YAML. 但是无论如何,第二个YAML并不代表与第一个YAML相同的结构。

In the first YAML, calls is a sequence. 在第一个YAML中, calls是一个序列。 The first sequence item of calls is also a sequence, which contains the scalar add and yet another sequence. calls的第一个序列项也是一个序列,其中包含标量add和另一个序列。 In the second YAML, calls is also a sequence, but the item it contains is a scalar which contains everything from add, to )" . The comma is content here, because you did not start a flow sequence (with [ ). Here is block-style YAML which is equivalent to the first YAML: 在第二个YAML中, calls也是一个序列,但是它包含的项是一个标量,其中包含从add,)"所有内容。此处的逗号是内容,因为您没有启动流序列(使用[ ]。)块样式的YAML,它等效于第一个YAML:

MyService:
    class: Some\Class\Here
    factory:
        - SomeFactoryHere
        - method
    calls:
        - - add
          - - "@=service('AnotherService1').create(\
                 service('AnotherService2'),
                 service('AnotherService3'),
                 service('AnotherService3')\
              )"

The backslashes at the end of the string's lines cause no whitespace to be inserted. 字符串行尾的反斜杠不会插入空格。 By default, YAML inserts one space character for each newline in a double-quoted string it encounters. 默认情况下,YAML在遇到的双引号字符串中为每个换行插入一个空格字符。 This YAML scalar yield exactly the same string as your first YAML contains. 该YAML标量产生的字符串与您的第一个YAML包含的字符串完全相同。

- - is compact style, which starts two nested sequence items at once. - -是紧凑样式,可一次启动两个嵌套序列项。 So now, calls is a sequence again with a sequence as first item. 因此,现在, calls是一个序列,其中一个序列作为第一项。 that nested sequence contains a scalar add as first item, and another sequence as second item. 该嵌套序列包含一个标量add作为第一项,另一个序列作为第二项。 And that sequence contains the double-quoted scalar. 该序列包含双引号标量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM