sethvargo / go-envconfig

A Go library for parsing struct tags from environment variables.
Apache License 2.0
1.05k stars 56 forks source link

[BUG] non-zero value is not overwritter when using overwrite #117

Closed mozarik closed 3 hours ago

mozarik commented 3 hours ago

Im using os.Setenv and Lookuper function. bot have the same result is not overwrite non-zero value the struct have. Is this expected behaviour. Or is there something wrong with my code ?

If the struct field has a non-zero value and a default is set: If no environment variable is specified, the struct field's existing value will be used (the default is ignored). If an environment variable is specified, the struct field's existing value will be overwritten with the environment variable value.

type DB struct {
    Name string `envconfig:"DB_HOST_NAME, default=default, overwrite"`
}

func TestOverWriteWithEnv2(t *testing.T) {
    os.Clearenv()
    os.Setenv("DB_HOST_NAME", "bar")

    dbCred := DB{
        Name: "meta-name",
    }

    err := envconfig.Process(context.TODO(), &dbCred)
    require.NoError(t, err)
    assert.Equal(t, "bar", dbCred.Name)
}
=== RUN   TestOverWriteWithEnv2
            Error Trace:    /Users/ruangguru/Data/data-tools/qwarks-2/playground_test.go:100
            Error:          Not equal:
                            expected: "bar"
                            actual  : "meta-name"

                            Diff:
                            --- Expected
                            +++ Actual
                            @@ -1 +1 @@
                            -bar
                            +meta-name
            Test:           TestOverWriteWithEnv2
--- FAIL: TestOverWriteWithEnv2 (0.00s)
func TestOverWriteWithEnv(t *testing.T) {
    os.Clearenv()
    lookuper := envconfig.MapLookuper(map[string]string{
        "DB_HOST_NAME": "bar",
    })

    type DB struct {
        Name string `envconfig:"DB_HOST_NAME, overwrite"`
    }

    dbCred := DB{
        Name: "meta-name",
    }

    err := envconfig.ProcessWith(context.TODO(), &envconfig.Config{
        Target:   &dbCred,
        Lookuper: lookuper,
    })

    require.NoError(t, err)
    assert.Equal(t, "bar", dbCred.Name)
}
=== RUN   TestOverWriteWithEnv
    /Users/ruangguru/Data/data-tools/qwarks-2/playground_test.go:82:
            Error Trace:    /Users/ruangguru/Data/data-tools/qwarks-2/playground_test.go:82
            Error:          Not equal:
                            expected: "bar"
                            actual  : "meta-name"

                            Diff:
                            --- Expected
                            +++ Actual
                            @@ -1 +1 @@
                            -bar
                            +meta-name
            Test:           TestOverWriteWithEnv
--- FAIL: TestOverWriteWithEnv (0.00s)
mozarik commented 3 hours ago

Sigh im using envconfig in the annotations my bad. u can delete this issue

mozarik commented 3 hours ago

but i found a bug

something like this is FAILED test

type DB struct {
    Name string `env:"DB_HOST_NAME,default=test,overwrite"`
}

func TestOverWriteWithEnv2(t *testing.T) {
    os.Clearenv()
    os.Setenv("DB_HOST_NAME", "bar")

    dbCred := &DB{
        Name: "meta-name",
    }

    err := envconfig.Process(context.TODO(), dbCred)
    require.NoError(t, err)
    assert.Equal(t, "bar", dbCred.Name)
}

but if i change it to this it PASS

type DB struct {
    Name string `env:"DB_HOST_NAME,overwrite,default=test"`
}