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

required tag not being honoured #35

Open
shaumux opened this issue Aug 16, 2018 · 4 comments
Open

required tag not being honoured #35

shaumux opened this issue Aug 16, 2018 · 4 comments

Comments

@shaumux
Copy link

shaumux commented Aug 16, 2018

I created a config struct like this

type rabbitMQ struct {
	USER     string `config:"user,required"`
	PASSWORD string `config:"password,required"`
	HOST     string `config:"host"`
	PORT     string `config:"port"`
}

type dbConfig struct {
	DIALECT  string `config:"dialect,required"`
	NAME     string `config:"name,required"`
	USER     string `config:"user,required"`
	PASSWORD string `config:"password,required"`
	HOST     string `config:"host"`
	PORT     string `config:"port"`
}

type Config struct {
	DATABASES   map[string]dbConfig `config:"databases,required"`
	RABBITMQ    rabbitMQ            `config:"rabbitmq,required"`
	ENVIRONMENT string              `config:"environment"`
}

confita seems to be ignoring the required tag for all but the password field in the rabbitMQ config
The file I'm reading from is a yaml file

@yaziine
Copy link
Contributor

yaziine commented Sep 6, 2018

Hi @shaumux, thank you for reporting this. Can you share us your yaml file please (without sensitive data of course 😉) ?

@shaumux
Copy link
Author

shaumux commented Sep 10, 2018

Here's the yaml file:


databases:
  DEFAULT:
    dialect: postgres
    name: mercury
    user: shaumux
    password: shaumux
    host: localhost
    port: 5432

rabbitmq:
  user: guest
  password: guest
  host: localhost
  port: 5432

Here's the code for loading:

logger.Info("Initializing with production settings")
	once.Do(func() {
		prodDB := dbConfig{
			DIALECT: "postgres",
		}
		config = &Config{
			ENVIRONMENT: "DEVELOPMENT",
			DATABASES: map[string]dbConfig{
				"DEFAULT": devDB,
			},
		}
		var confBackends []backend.Backend
		if filePath != nil {
			if filepath.IsAbs(*filePath) != true {
				logger.Info(*filePath + " is not absolute, constructing absolute path")
				filePath, err := filepath.Abs(*filePath)
				if err != nil {
					logger.WithFields(logrus.Fields{"error": err}).Fatal("Failed to construct absolute file path with error:")
				}
				logger.WithFields(logrus.Fields{"path": filePath}).Debug("Constructed absolute filepath")
			}
			if _,err := os.Stat(*filePath); err != nil {
				if os.IsNotExist(err) {
					logger.WithFields(logrus.Fields{"ConfigFiile": *filePath, "error": err}).Warning("File does not exist")
				}
			}else{
				confBackends = append(confBackends, file.NewBackend(*filePath))
			}
		}
		confBackends = append(confBackends, env.NewBackend())
		loader := confita.NewLoader(confBackends...)
		err := loader.Load(context.Background(), config)
		if err != nil {
			logger.WithFields(logrus.Fields{"error": err}).Fatal("Failed to initialize app")
		}
})

This works even if I start removing fields from the yaml file

@yaziine
Copy link
Contributor

yaziine commented Oct 11, 2018

Hey @shaumux, unfortunately, maps are not supported on Confita. It involves a lot of reflections and we do not want to go over it. Instead of having a map for each database configuration you could have a field for each? I know that is not the best solution but this might be a way to bypass the issue.

@shaumux
Copy link
Author

shaumux commented Oct 12, 2018

The issue I'm having is not with the Database config but rather RabbitMQ config.

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