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

refactor: refactor config file resolve #1132

Merged
merged 1 commit into from
Apr 18, 2018
Merged

refactor: refactor config file resolve #1132

merged 1 commit into from
Apr 18, 2018

Conversation

Ace-Tang
Copy link
Contributor

Ⅰ. Describe what this PR did

move daemon config code from main.go to daemon/config/config.go,
suporrts to resolve config file contains nested object.
for eg, resolve file

map[string]interface{}{
		"a": "a",
		"b": "b",
		"c": "c",
		"iter1": map[string]interface{}{
			"i1": "i1",
			"i2": "i2",
		},
		"iter11": map[string]interface{}{
			"ii1": map[string]interface{}{
				"iii1": "iii1",
				"iii2": "iii2",
			},
		},
	}

to

map[string]interface{}{
		"a":    "a",
		"b":    "b",
		"c":    "c",
		"i1":   "i1",
		"i2":   "i2",
		"iii1": "iii1",
		"iii2": "iii2",
	}

Ⅱ. Does this pull request fix one issue?

Ⅲ. Describe how you did it

Ⅳ. Describe how to verify it

Ⅴ. Special notes for reviews

@codecov-io
Copy link

codecov-io commented Apr 16, 2018

Codecov Report

Merging #1132 into master will decrease coverage by 0.01%.
The diff coverage is 15.38%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1132      +/-   ##
==========================================
- Coverage   16.06%   16.04%   -0.02%     
==========================================
  Files         173      174       +1     
  Lines        9930     9992      +62     
==========================================
+ Hits         1595     1603       +8     
- Misses       8218     8272      +54     
  Partials      117      117
Impacted Files Coverage Δ
pkg/utils/utils.go 78.57% <100%> (+0.34%) ⬆️
daemon/config/config.go 10% <12%> (ø)

@@ -110,3 +115,96 @@ func (cfg *Config) Validate() error {

return nil
}

//MergeConfigConfigurations merges flagSet flags and config file flags into Config.
func (cfg *Config) MergeConfigConfigurations(config *Config, flagSet *pflag.FlagSet) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

merge config configuration, do we have duplicated config in this function naming?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

how about MergeConfiguration.

Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah, that is much better.

}

// iterateConfig resolves key-value from config file iteratly.
func iterateConfig(origin map[string]interface{}, config map[string]interface{}) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we need to make this function some kind of robust. If the config is nil, I am afraid this function would panic. How about doing this nil-checking to improve robustness?

Copy link
Contributor Author

@Ace-Tang Ace-Tang Apr 17, 2018

Choose a reason for hiding this comment

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

I refer to make this function before iterateConfig, so that origin can not be nil, and since it not a common function, will not be used by other function ,add nil check is redundancy

if len(origin) == 0 {
                return nil
        }
fileFlags := make(map[string]interface{}, 0)
  iterateConfig(origin, fileFlags)

Copy link
Collaborator

Choose a reason for hiding this comment

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

I still insist on make functions independent from each other. As if you encapsulate this function out of the another place which is calling this. It has a trend to be general. If that, we cannot prevent others to use it in the future, as a result, there will be potential possibility to panic.

In addition, if the code which is calling this function changes or removes the nil checking in the upper layer, there will be potential panic as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

for range a nil map will never cause panic, since nil value will convert to a nil map(map[] ), that add a nil check is useless.

1. move daemon config code from main.go to daemon/config/config.go.
2. supports to resolve config file contains nested object.
3. skip check conflict in slice type flags and merge them.

Signed-off-by: Ace-Tang <aceapril@126.com>
return
}
if v, exist := fileFlags[f.Name]; exist {
conflictFlags = append(conflictFlags, fmt.Sprintf("from flag: %s and from config file: %s", f.Value.String(), v))
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we need to remove : in the error message.
Otherwise the returned error is quite unreadable.

@allencloud
Copy link
Collaborator

LGTM

@pouchrobot pouchrobot added the LGTM one maintainer or community participant agrees to merge the pull reuqest. label Apr 18, 2018
@allencloud allencloud merged commit fac1f29 into AliyunContainerService:master Apr 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/refactor LGTM one maintainer or community participant agrees to merge the pull reuqest. size/XL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants