douchuituo3032 2018-07-06 08:19
浏览 344
已采纳

在golang中正确映射YAML配置文件

I write an API in go which can create organisation which have default policies rules.

I want to use an external config YAML file to include some policies in my API (I actually put the policies inside my code in the function which creates my entity organisation) :

policy.yml

- role: "admin"
  organisationid: organisation.ID
  policies:
    [{Object: "/*", Action: "*"}]

- role: "user"
  organisationid: organisation.ID
  policies:
    [{Object: "/me", Action: "GET"},
    {Object: "/organisations", Action: "GET"},
    {Object: "/acl/roles", Action: "GET"}]

I extract it using go-yaml lib and the expected output should be :

[{admin organisation.ID [{/* *}]} {user organisation.ID [{/me GET} {/organisations GET} {/acl/roles GET}]}]

But when I extract it in a struct like :

// OrganisationRole ...
type OrganisationRoleNoPolicy struct {
    Role           string              `json:"role"`
    OrganisationID string              `json:"organisation"`
    Policies       []map[string]string `json:"policies"`
}

func extractYaml() (config []OrganisationRoleNoPolicy) {

    filename := "policy.yml"
    source, err := ioutil.ReadFile(filename)
    if err != nil {
        panic(err)
    }
    err = yaml.Unmarshal(source, &config)
    if err != nil {
        log.Fatalf("error: %v", err)
    }
    fmt.Printf("--- config:
%v

", config)
    return
}

I get this :

[{admin organisation.ID [map[Object:/* Action:*]]} {user organisation.ID [map[Object:/me Action:GET] map[Object:/organisations Action:GET] map[Object:/acl/roles Action:GET]]}]

Maybe I don't get well how to use or properly write YAML, so guys could you help me understanding how to map it to obtain the output expected.

  • 写回答

1条回答 默认 最新

  • duangan6133 2018-07-06 09:31
    关注

    Finally find it, I have to change my Yaml a little to have a proper array, the case seems to be very important too :

    - role: "admin"
      organisationid: organisation.ID
      policies:
        - {object: "/*", action: "*"}
    
    - role: "user"
      organisationid: organisation.ID
      policies:
        - {object: "/me", action: "GET"}
        - {object: "/organisations", action: "GET"}
        - {object: "/acl/roles", action: "GET"}
    

    and my struct is a bit differents it contains a struct than include my two strings :

    type policy struct {
        Object string `json:"obj" binding:"required"`
        Action string `json:"act" binding:"required"`
    }
    
    // OrganisationRole ...
    type OrganisationRole struct {
        Role           string   `json:"role"`
        OrganisationID string   `json:"organisation"`
        Policies       []policy `json:"policies"`
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 鼠标右键,撤销删除 复制 移动,要怎样删除
  • ¥15 使用MATLAB进行余弦相似度计算加速
  • ¥15 服务器安装php5.6版本
  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题
  • ¥15 我想在WPF的Model Code中获取ViewModel Code中的一个参数
  • ¥15 arcgis处理土地利用道路 建筑 林地分类
  • ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
  • ¥100 寻会做云闪付tn转h5支付链接的技术
  • ¥15 DockerSwarm跨节点无法访问问题