Giter Site home page Giter Site logo

Comments (5)

dosubot avatar dosubot commented on May 28, 2024 2

🤖

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.

kvii avatar kvii commented on May 28, 2024

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}".

kratos/config/options.go

Lines 85 to 97 in 9106991

// defaultResolver resolve placeholder in map value,
// placeholder format in ${key:default}.
func defaultResolver(input map[string]interface{}) error {
mapper := func(name string) string {
args := strings.SplitN(strings.TrimSpace(name), ":", 2) //nolint:gomnd
if v, has := readValue(input, args[0]); has {
s, _ := v.String()
return s
} else if len(args) > 1 { // default value
return args[1]
}
return ""
}

from kratos.

kvii avatar kvii commented on May 28, 2024

It's a mistake of doc. kratos don't support "$key" format by default. I found this test:

{
name: "test $PORT",
path: "foo.bar.value2",
expect: "$PORT",
},

from kratos.

akoserwal avatar akoserwal commented on May 28, 2024

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.

kvii avatar kvii commented on May 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.