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

fix: parsing config input and convert to match the type #3306

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

akoserwal
Copy link

@akoserwal akoserwal commented Apr 26, 2024

Description (what this PR does / why we need it):

Convert to type based on the followed pattern:

  • bool: true/false
  • float64: has(.) parseFloat
  • int64: parseInt
  • string: default
  • ${key:"defaultValue"}

fix: #3198

Which issue(s) this PR fixes (resolves / be part of):

Other special notes for the reviewers:

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Apr 26, 2024
@akoserwal
Copy link
Author

@@shenqidebaozi Please review

@shenqidebaozi
Copy link
Sponsor Member

shenqidebaozi commented Apr 30, 2024

@akoserwal Okay, no problem. Let me find some more people to watch together @go-kratos/contributor

// Default to string if no other conversion succeeds
return input
}
func expand(s string, mapping func(string) string) interface{} {
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a new feature that modifies the default behavior, so it should be an optional feature.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shenqidebaozi How do you suggest to make it an optional feature?

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because before the feature is added, all configurations will be parsed into strings, and users may have code to handle the conversion of strings to other types. If we modify the default behavior, it may cause exceptions for users.

Copy link
Author

@akoserwal akoserwal Apr 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with your point to make it an optional behaviour.

Making it optional

Like:

Changing
type Resolver func(map[string]interface{}) error
to
type Resolver func(map[string]interface{}, bool) error
and
func defaultResolver(input map[string]interface{}, enableTypeConversion bool) error

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can leave the Resolver method definition unchanged and only make modifications within the internal implementation

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func newDefaultResolver(o bool) func(map[string]interface{}) error {
	return func(input map[string]interface{}) error {
		// do something
	}
}
// New a config with options.
func New(opts ...Option) Config {
	o := options{
		decoder:  defaultDecoder,
		resolver: newDefaultResolver(false),
		merge: func(dst, src interface{}) error {
			return mergo.Map(dst, src, mergo.WithOverride)
		},
	}
	for _, opt := range opts {
		opt(&o)
	}
	return &config{
		opts:   o,
		reader: newReader(o),
	}
}

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can refer to the code above

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NewDefaultResolver needs to be exported only then user will be able to make it configured like

c := config.New(
        config.WithResolver(config.NewDefaultResolver(true)),
        config.WithSource(
            file.NewSource(flagconf),
        ),
    )

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can export a configuration method instead of exporting the entire implementation

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please review @shenqidebaozi and @go-kratos/contributor

@akoserwal akoserwal force-pushed the fix-config-parsing branch 2 times, most recently from 5e4599d to 48bfca4 Compare May 2, 2024 16:01
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Issue with configuration file to render the values from environment variables
2 participants