Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't load config into struct with json tags #85

Open
slon opened this issue May 27, 2020 · 1 comment
Open

Can't load config into struct with json tags #85

slon opened this issue May 27, 2020 · 1 comment

Comments

@slon
Copy link

slon commented May 27, 2020

I noticed a regression in behaviour. Here is code for reproducing the issue.

package confitajson

import (
	"context"
	"log"
	"testing"

	"github.com/heetch/confita"
	"github.com/heetch/confita/backend/file"
)

func TestLoad(t *testing.T) {
	b := file.NewBackend("config.json")

	var c struct {
		Field string `json:"field"`
	}

	if err := confita.NewLoader(b).Load(context.Background(), &c); err != nil {
		log.Fatalf("failed to load config: %v", err)
	}

	if c.Field != "value" {
		log.Fatal("field value is not updated")
	}
}

Content of config.json file is following:

{
    "field": "value"
}

When running this test on the latest confita release, struct field is not updated. But on version v0.7.0 code works fine.

prime@bee ~/C/confitajson> go get github.com/heetch/[email protected]
go: downloading github.com/heetch/confita v0.7.0
prime@bee ~/C/confitajson> go test .
ok  	github.com/slon/confitajson	0.002s
prime@bee ~/C/confitajson> go get github.com/heetch/confita
go: github.com/heetch/confita upgrade => v0.9.1
prime@bee ~/C/confitajson> go test .
2020/05/27 20:02:34 field value is not updated
FAIL	github.com/slon/confitajson	0.002s
FAIL

I also noticed, that removing these 3 lines, seems to fix this issue for me. https://github.com/heetch/confita/blob/master/config.go#L183-L185

Is that change in behaviour intentional, or is it indeed a regression?

@pancakebasspanda
Copy link

pancakebasspanda commented Feb 15, 2021

I noticed that in the current version if I use the config tag and json tags I get very different behaviors:

package main

import (
	"context"
	"github.com/heetch/confita"
	"github.com/heetch/confita/backend/file"
	"github.com/heetch/confita/backend/flags"
	log "github.com/sirupsen/logrus"
	"time"
)

type Config struct {
	Host        string        `config:"host"`
	Port        uint32        `config:"port"`
	Timeout     time.Duration `config:"timeout"`
	Password    string        `config:"password"`
	Name string `config:"name,required"`
	OrderValue float64 `config:"order_value"`
	itemNumber int `config:"item_number"`
}

func main() {
	loader := confita.NewLoader(
		file.NewBackend("cmd/confita/config.json"),
		flags.NewBackend(),
		)

	// default values
	cfg := Config{
		Host: "127.0.0.1",
		Port: 5656,
		Timeout: 5 * time.Second,
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5 * time.Second)
	defer cancel()

	// loading configuration
	if err := loader.Load(ctx, &cfg); err != nil {
		log.WithError(err).Fatal("loading config")
	}


	// check the config
	log.WithField("config", cfg).Info("config loaded")
}

Using the config tag, strings seem to load fine however floats and ints don't. If I then use the json tag, strings and floats load fine but not ints.

example JSON FIle:
{ "name" : "red shoes", "item_number" : 2, "order_value": 23.45 }

output with config tags:
time="2021-02-15T20:31:04Z" level=info msg="config loaded" config="{127.0.0.1 5656 5s red shoes 0 0}"

output with json tags:
time="2021-02-15T20:38:12Z" level=info msg="config loaded" config="{127.0.0.1 5656 5s red shoes 23.45 0}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants