jinzhu / configor

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

when u64 config has non zero default and zero in config, config has no effect #50

Open ailisp opened 4 years ago

ailisp commented 4 years ago

Having this as part of my config:

        IdleMachines uint64 `yaml:"idle_machines" default:"2"`

and in config.yaml if it's

idle_machines: 3

It has effect

But in config.yaml if it's

idle_machines: 0

There's no effect and IdleMachines is still 2

lukasaron commented 4 years ago

This behavior is correct and expected to work this way. When you provide a value in your config.yaml file such value has the higher priority then default tag in the code. When you remove the line from your config.yaml you should be able to get the default value, in this case number 2.

ailisp commented 4 years ago

This behavior is correct and expected to work this way. When you provide a value in your config.yaml file such value has the higher priority then default tag in the code. When you remove the line from your config.yaml you should be able to get the default value, in this case number 2.

Actually not, I'm not remove the idle_machines: line, instead I set it to idle_machines: 0, so as you point out, idle_machines: 0 in config.yml should take have higher priority over:

        IdleMachines uint64 `yaml:"idle_machines" default:"2"`

But finaly idle_machines is 2, means it's not true

lukasaron commented 4 years ago

I have tested locally your issue and I can't reproduce it, to be honest.

I will paste the whole code I used:

config.yaml: idle_machines: 0

main.go:


import (
    "fmt"
    "github.com/jinzhu/configor"
    "log"
)

type Config struct {
    IdleMachines uint64 `yaml:"idle_machines" default:"2"`
}

func main() {
    var config Config
    err := configor.Load(&config,"<ABSOLUTE_PATH_TO_THE_FILE>/config.yaml")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(config.IdleMachines)
}

Result: 0

makarchuk commented 3 years ago

I believe this is a duplicate of #34 and has been fixed already