dougang6821 2016-08-08 06:53
浏览 86
已采纳

Symfony YAML格式转换

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.

  • 写回答

2条回答 默认 最新

  • drbfxb977777 2016-08-08 08:18
    关注

    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'))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建