jinzhu / configor

Golang Configuration tool that support YAML, JSON, TOML, Shell Environment
MIT License
1.74k stars 204 forks source link

Recursive nested YAML structure #54

Open mbecker opened 4 years ago

mbecker commented 4 years ago

Hi,

I'm running in the issue that the YAML config is not loaded correctly or better the programm runs in an infinitive loop.

Given is the following YAML config:

smartdevices:
  - name: Tado_Esszimmer
    id: tado.0.466027.Rooms.1
    tags:
      product: tado
      room: esszimmer
    values:
    - id: tado.0.466027.Rooms.1.Actual_Humidity
      field: humidity
    - id: tado.0.466027.Rooms.1.Actual_Temperature
      field: temperature
    - id: tado.0.466027.Rooms.1.openWindow
      values:
      - id: tado.0.466027.Rooms.1.openWindow.detectedTime
        field: open_window_detected_time
      - id: tado.0.466027.Rooms.1.openWindow.durationInSeconds
        field: open_window_duration_in_seconds
      - id: tado.0.466027.Rooms.1.openWindow.expiry
        field: open_window_expiry
      - id: tado.0.466027.Rooms.1.openWindow.remainingTimeInSeconds
        field: open_window_remaining_time

The Go definition of that as structs:

// IoDevice defines an object/device
type IoDevice struct {
    Name string `yaml:"name"`
    ID   string `yaml:"id"`
    Tags struct {
        Product string `yaml:"product"`
        Haus    string `yaml:"haus"`
        Room    string `yaml:"room"`
    } `yaml:"tags"`
    Values []IoValue `yaml:"values"`
}

// IoValue defines the value
type IoValue struct {
    ID    string `yaml:"id"`
    Field string `yaml:"field,omitempty"`
    Values []IoValue `yaml:"values"`
}

func main() {
    c := make(chan os.Signal, 1)
    signal.Notify(c, os.Interrupt, syscall.SIGTERM)

    // Load config.yaml file
    path, err := os.Getwd()
    if err != nil {
        log.Println(err)
    }
    fmt.Println(path) // for example /home/user
    err = configor.New(&configor.Config{Debug: true}).Load(&YAMLConfig, filepath.Join(path, "config.yaml"))
    if err != nil {
        log.Fatal(err)
    }

    for _, device := range YAMLConfig.IoBrokerDevices {
        if len(device.Name) > 0 {
            fmt.Println("Not null")
        }
        fmt.Printf("Lenn values: %d\n", len(device.Name))
    }
}

The ouput is as follows

C:\apps\gomqtt
Current environment: 'development'
Loading configurations from file 'C:\apps\iobroker\gomqtt\config.yaml'...

End then it runs in an infintive loop. The next lines are never executed.

Any hints?

Thanks for your help!