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

Ability Decode to Multiple structs #58

Open
BoraBak opened this issue Jun 6, 2021 · 5 comments
Open

Ability Decode to Multiple structs #58

BoraBak opened this issue Jun 6, 2021 · 5 comments

Comments

@BoraBak
Copy link

BoraBak commented Jun 6, 2021

In case the properties file has a logical division of the configurations, I'd like to decode to several dedicated structs.

For example:
Given the following config.properties file

app.port=3000
db.host=127.0.0.1
db.port=8000
db.username=username123
db.password=password123
pglistener.min_reconn=10 * time.Second
pglistener.max_reconn=1 * time.Minute
...
...

I'd like to decode to the following structs:

type Config struct {
	AppPort string `properties:"app.port,default=3000"`
}

type DBConfig struct {
	Host          string  `properties:"db.host,default=127.0.0.1"`
	Port           int       `properties:"db.port,default=5432"`
	Username string `properties:"db.username"`
	Password  string `properties:"db.password"`
}


type PGListener struct {
	MinReconnectInterval time.Duration `properties:"pglistener.min_reconn"`
	MaxReconnectInterval time.Duration `properties:"pglistener.max_reconn"`
}
@magiconair
Copy link
Owner

Does it work if you embed them into one large struct?

@BoraBak
Copy link
Author

BoraBak commented Jun 6, 2021

@magiconair not sure what you meant:

Option-1 doesn't work.

type Config struct {
	AppPort     string `properties:"app.port,default=3000"`
        DBConfig   *DBConfig
        PGListener *PGListener
}

Option-2 works, but it's not my desired solution/architecture.

type Config struct {
	AppPort     string `properties:"app.port,default=3000"`
	Host          string  `properties:"db.host,default=127.0.0.1"`
	Port           int       `properties:"db.port,default=5432"`
	Username string `properties:"db.username"`
	Password  string `properties:"db.password"`
	MinReconnectInterval time.Duration `properties:"pglistener.min_reconn"`
	MaxReconnectInterval time.Duration `properties:"pglistener.max_reconn"`
}

@magiconair
Copy link
Owner

Does it work if you rename DBConfig to DB in option 1?

@BoraBak
Copy link
Author

BoraBak commented Jun 6, 2021

@magiconair nope.

@KangKangTao
Copy link

maybe embedding mean this, move the prefix in tag of embedded struct to tag of parent struct:

type Config struct {
	AppPort string `properties:"app.port,default=3000"`
	DBConfig `properties:"db"`
	PGListener `properties:"pglistener"`
}

type DBConfig struct {
	Host          string  `properties:"host,default=127.0.0.1"`
	Port           int       `properties:"port,default=5432"`
	Username string `properties:"username"`
	Password  string `properties:"password"`
}

type PGListener struct {
	MinReconnectInterval time.Duration `properties:"min_reconn"`
	MaxReconnectInterval time.Duration `properties:"max_reconn"`
}

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

3 participants