Comments (5)
🤖
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.
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.
Related Issues (20)
- [Question] http API returns json parameter name not as expected HOT 7
- In version 1.0.0, Mergo moves to a vanity URL: dario.cat/mergo. Can update to the latest version? HOT 6
- How to configure when dealing with multiple DB instances? Are there any practices?[Question] HOT 5
- [Question]Why kratos load yaml as json and unmarshal to struct HOT 1
- 使用openapiv2,出现protoc-gen-openapiv2/options/annotations.proto: File not found. HOT 3
- After rpc call, error cannot be returned correctly HOT 3
- [Question]After rpc call, error cannot be returned correctly [rpc调用后,错误无法解析] HOT 4
- [Feature] Logo adjustment suggestions HOT 2
- Unable to customize http.ResponseWriter because of wrong implementation of Kratos' http.wrapper.Result() HOT 4
- Test failed on different time zone. HOT 1
- [consul Registry]: When grpc enters idle mode, “last connection error” occurs on the client and cannot be recovered HOT 1
- [Question]Why design middleware after bind? HOT 1
- [Question] Shall we follow the behavior of the document of http.ResponseWriter? HOT 1
- kratos run runs successfully but docker run fails HOT 5
- [Question]Use make config under windows to report an error HOT 3
- github.com/go-kratos/kratos/pkg/cache/redis 代码找不到了, 新版位置在哪里啊 HOT 6
- [Feature] log Helper sprint, sprintf is executed before Filter Level, the performance consumption is very high HOT 3
- [Question]Transaction nested conflict stuck HOT 3
- [Feature] consule register need suport tags[] HOT 2
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.