dongshao9106 2019-05-27 23:49 采纳率: 100%
浏览 157
已采纳

如何以父子顺序构造yaml文件?

I want to form a struct using golang via yaml file but I find it difficult to figure out to do it.

api:
  local:
    host: localhost
    port: 8085
  develop:
    host:
    port:
  production:
    host:
    port:
rest-api:
  local:
    host: localhost
    port: 8085
  develop:
    host:
    port:
  production:
    host:
    port:

this is the format in my yaml file

the expected code of this is i want to create a dynamic api url in local, develop and production format like api : local = host+port, same to develop and production to dynamically configure it easily and set up

Would appreciate your help in golang struct and also for your help.

  • 写回答

1条回答 默认 最新

  • donglaoe2979 2019-05-28 01:43
    关注

    This online resource will get you halfway there:

    https://mengzhuo.github.io/yaml-to-go/

    Pasting your yaml yields this:

    type AutoGenerated struct {
        API struct {
            Local struct {
                Host string `yaml:"host"`
                Port int    `yaml:"port"`
            } `yaml:"local"`
            Develop struct {
                Host interface{} `yaml:"host"`
                Port interface{} `yaml:"port"`
            } `yaml:"develop"`
            Production struct {
                Host interface{} `yaml:"host"`
                Port interface{} `yaml:"port"`
            } `yaml:"production"`
        } `yaml:"api"`
        RestAPI struct {
            Local struct {
                Host string `yaml:"host"`
                Port int    `yaml:"port"`
            } `yaml:"local"`
            Develop struct {
                Host interface{} `yaml:"host"`
                Port interface{} `yaml:"port"`
            } `yaml:"develop"`
            Production struct {
                Host interface{} `yaml:"host"`
                Port interface{} `yaml:"port"`
            } `yaml:"production"`
        } `yaml:"rest-api"`
    }
    

    There's obvious sub-type duplicates. So it can be trimmed.

    First pass:

    type Address struct {
        Host string `yaml:"host"`
        Port int    `yaml:"port"`
    }
    
    type MyConfig struct {
        API struct {
            Local      Address `yaml:"local"`
            Develop    Address `yaml:"develop"`
            Production Address `yaml:"production"`
        } `yaml:"api"`
        RestAPI struct {
            Local      Address `yaml:"local"`
            Develop    Address `yaml:"develop"`
            Production Address `yaml:"production"`
        } `yaml:"rest-api"`
    }
    

    Second (and final) pass:

    type Address struct {
        Host string `yaml:"host"`
        Port int    `yaml:"port"`
    }
    
    type Deployment struct {
        Local      Address `yaml:"local"`
        Develop    Address `yaml:"develop"`
        Production Address `yaml:"production"`
    }
    
    type MyConfig struct {
        API     Deployment `yaml:"api"`
        RestAPI Deployment `yaml:"rest-api"`
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line