构造深层结构不起作用

i’ve struct which responsible to parse data from yaml file While this struct is working sometimes I got some newly fields which I need to parse

This is working

  - name: test1
    type: type
    path: path

This is not

 - name: test1
    type: type
    path: path
    build-parameters:
       maven-opts:
          defines:
              skipTests: true

This is the struct

type Modules struct {
    Name string
    Type string
    Path string
    Parameters Parameters `yaml:"build-parameters,omitempty"`
}

And the parameters is type of:

type Parameters map[string]string

How I should construct my struct to accept this build-parameters entries also?

This is the library I use

https://github.com/go-yaml/yaml

Your struct does not match the data structure. If Parameters is a map[string]string, it accepts key-value pairs where the key and value are both string. In your data, build-parameters contains an object, which contains an object, which contains a key-value pair.

You could either redefine Parameters to be map[string]interface{} and use type assertions, or you can define the entire structure, e.g.:

type Parameters struct {
    MavenOpts struct {
        Defines map[string]string
    } `yaml:"maven-opts"`
}

If you use the empty interface, you'll have to use type assertions, which can get pretty cumbersome. For example:

if opts, ok := module.Parameters["maven-opts"].(map[string]interface{}); ok {
    if defines,ok := opts["defines"].(map[string]interface{}); ok {
        if skipTests,ok := defines["skipTests"].(bool); ok {
            // skipTests is the bool value from the yaml
        }
    }
}