Comments (7)
🤖
Hey @akoserwal, great to see you back! 😊 Hope you've been doing awesome since our last chat.
The issue you're experiencing is due to the way the default resolver in Kratos handles environment variables. The default resolver only supports flat keys, meaning it can only resolve environment variables that correspond to top-level keys in the configuration. It doesn't support nested keys like "data.testDB.endpoint".
Here's a snippet of the default resolver code for reference:
o := options{
decoder: defaultDecoder,
resolver: defaultResolver,
merge: func(dst, src interface{}) error {
return mergo.Map(dst, src, mergo.WithOverride)
},
}
If you want to override nested keys with environment variables, you would need to implement a custom resolver that supports this feature. This custom resolver should be able to parse the environment variable names into nested keys.
Unfortunately, without the implementation of the defaultResolver
, I can't provide a specific code example on how to modify it to support nested keys. However, the general idea would be to split the environment variable name on a certain character (like underscore or dot) and use the resulting parts as nested keys.
You can find more information about the default resolver and its limitations in the Kratos repository.
I hope this helps clarify the issue. If you have any further questions, feel free to ask.
Sources
About Dosu
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.
Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.
If you want to continue the conversation, start your reply with @dosu-bot.
from kratos.
Hi, @akoserwal,
I'm helping the Kratos team manage their backlog and am marking this issue as stale. From what I understand, the issue involves a configuration file not rendering values from environment variables as expected in Kratos version v2.7.2 and Go version 1.19-1.21 on Mac OS. The environment variable "TEST_ENDPOINT" is set in the config file, but it does not override the key value as "data.testDB.endpoint." The default resolver in Kratos only supports flat keys, not nested keys, and a custom resolver that supports this feature would need to be implemented. There is also a discussion about the correct format of placeholders and handling non-string types in the comments.
Could you please confirm if this issue is still relevant to the latest version of the Kratos repository? If it is, please let the Kratos team know by commenting on the issue. Otherwise, feel free to close the issue yourself, or the issue will be automatically closed in 7 days.
Thank you!
from kratos.
I think here is a bug. Consider to change "$TEST_ENDPOINT" to "${ENDPOINT}" to avoid this temporary.
I made a reproduce project at local. Some of key codes:
server:
http:
addr: "${ADDR}"
c := config.New(
config.WithSource(
env.NewSource("TEST_"),
file.NewSource(flagconf),
),
)
c.Load()
var bc conf.Bootstrap
c.Scan(&bc)
fmt.Println(bc.Server.Http.Addr)
fmt.Println(c.Value("ADDR").String())
fmt.Println(c.Value("server.http.addr").String())
After I execute export TEST_ADDR=0.0.0.0:8000
, codes run properly(print "0.0.0.0:8000").
I guess there has some mistakes of the format of placeholder. The comment say that the correct format is "${key:default}".
Lines 85 to 97 in 9106991
from kratos.
It's a mistake of doc. kratos don't support "$key" format by default. I found this test:
Lines 132 to 136 in 9106991
from kratos.
Thanks, @kvii, for clarifying.
It works if the export TEST_ADDR=0.0.0.0:8000 and use fmt.Println(bc.Server.Http.Addr)
For boolean, I am getting
Error
panic: proto: (line 1:133): invalid value for bool type: "true"
data:
test:
endpoint: "${ENDPOINT}"
useTLS: "${TLS:false}"
export TEST_TLS=true
from kratos.
Same issue at #1565. Unfortunately, non-string types are not supported in Scan
because that will include more complexity. You can use c.Value("data.test.useTLS").Bool()
to get the bool value explicitly. But I think this doesn't match your case.
from kratos.
Issue is still relevant, I have PR opened to fix this issue
from kratos.
Related Issues (20)
- Package version dependency issue HOT 4
- go generate ./...failed HOT 5
- [Feature]Add svrType related fields in proto to protoc-gen-go-http and http transport HOT 3
- proto file used to build the api document reported an error HOT 1
- consul watch, the server is offline and will not receive any notification HOT 6
- [Feature] Add NewClient method to gRPC Client HOT 3
- [Question] DDD的最佳实践方式?包括CQRS、Clean Architecture、六边形结构 HOT 5
- [Question]请教下kratos如何定义支持上传文件接口? HOT 2
- metrics statistics and usage issues [Question] HOT 5
- `stream` response in proto failed to generate http client HOT 6
- [Feature] Add kratos errors to buf remote plugin HOT 1
- [Question] I modified it based on examples/helloworld and found that client/main.go reported an error when executing. I am not sure if it is a BUG. HOT 1
- HTTPClient proto: syntax error (line 1:1): unexpected token 404 HOT 3
- [Question] how to set response content-type as xml for one method HOT 2
- [Question] HTTP Server. extra add route do not pass middleware. HOT 2
- I a new release due soon ? HOT 4
- [Question] Can I custom project layouts by using kratos to generate HOT 1
- [Question]Excuse me, what is the error when compiling? HOT 2
- Kratos middleware seems to only apply to unary gRPC operations HOT 7
- t
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kratos.