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

Fully overrides default slice fields of config (#4001) #6163

Closed

Conversation

mcdoker18
Copy link

Description:

mapstructure library doesn't override full slice during unmarshalling. Origin issue: mitchellh/mapstructure#74 (comment) To address this we zeroes every slice before unmarshalling unless user provided slice is nil.

Link to tracking Issue: #4001

@codecov
Copy link

codecov bot commented Sep 28, 2022

Codecov Report

Base: 92.06% // Head: 92.06% // Increases project coverage by +0.00% 🎉

Coverage data is based on head (cfebf0b) compared to base (ee02bc7).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6163   +/-   ##
=======================================
  Coverage   92.06%   92.06%           
=======================================
  Files         219      219           
  Lines       13242    13252   +10     
=======================================
+ Hits        12191    12201   +10     
  Misses        828      828           
  Partials      223      223           
Impacted Files Coverage Δ
confmap/confmap.go 93.93% <100.00%> (+0.39%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Member

@dmitryax dmitryax left a comment

Choose a reason for hiding this comment

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

Thanks for addressing the issue! Overall LGTM. Just one nit regarding tests

confmap/confmap_test.go Outdated Show resolved Hide resolved
// To address this we zeroes every slice before unmarshalling unless user provided slice is nil.
func zeroesSliceHookFunc() mapstructure.DecodeHookFuncValue {
return func(from reflect.Value, to reflect.Value) (interface{}, error) {
if from.Kind() == reflect.Slice && from.IsNil() {
Copy link
Member

Choose a reason for hiding this comment

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

Len() == 0? No need to re-initialize if empty?

Copy link
Author

Choose a reason for hiding this comment

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

If user provides empty list in the config explicitly then we zeroes the default value of the field.

Copy link
Contributor

Choose a reason for hiding this comment

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

@bogdandrutu do you mean if both source and destination are 0 then we should just return?

Copy link
Member

Choose a reason for hiding this comment

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

Lets start with this:
nil != initialized and len 0

foo:
foo: []

What is the behavior we want in these cases? Please document them somewhere, in order for me to understand if the implementation is ok.

Copy link
Member

Choose a reason for hiding this comment

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

Ping on this.

@mcdoker18 mcdoker18 force-pushed the feature/slice_config branch 2 times, most recently from 62b2fba to ca970c1 Compare September 30, 2022 08:12
@github-actions
Copy link
Contributor

This PR was marked stale due to lack of activity. It will be closed in 14 days.

@github-actions github-actions bot added the Stale label Oct 15, 2022
@mcdoker18 mcdoker18 force-pushed the feature/slice_config branch from ca970c1 to 9213f8d Compare October 15, 2022 16:12
@mcdoker18
Copy link
Author

@bogdandrutu @dmitryax please can you take a look? All comments are fixed.

@github-actions github-actions bot removed the Stale label Oct 16, 2022
.chloggen/config-slice-fields.yaml Outdated Show resolved Hide resolved
confmap/confmap_test.go Outdated Show resolved Hide resolved
confmap/confmap_test.go Show resolved Hide resolved
confmap/confmap.go Outdated Show resolved Hide resolved
mapstructure library doesn't override full slice during unmarshalling.
Origin issue: mitchellh/mapstructure#74 (comment)
To address this we empty every slice before unmarshalling unless user provided slice is nil.
@mcdoker18 mcdoker18 force-pushed the feature/slice_config branch from 9213f8d to cfebf0b Compare October 17, 2022 08:33
@mcdoker18
Copy link
Author

@dmitryax fixed

Copy link
Member

@dmitryax dmitryax left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@codeboten codeboten left a comment

Choose a reason for hiding this comment

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

The PR looks ok to me, will merge once @bogdandrutu's comments have been addressed

// To address this we zeroes every slice before unmarshalling unless user provided slice is nil.
func zeroesSliceHookFunc() mapstructure.DecodeHookFuncValue {
return func(from reflect.Value, to reflect.Value) (interface{}, error) {
if from.Kind() == reflect.Slice && from.IsNil() {
Copy link
Contributor

Choose a reason for hiding this comment

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

@bogdandrutu do you mean if both source and destination are 0 then we should just return?

}

if to.CanSet() && to.Kind() == reflect.Slice {
to.Set(reflect.MakeSlice(reflect.SliceOf(to.Type().Elem()), 0, 0))
Copy link
Member

Choose a reason for hiding this comment

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

why 0 capacity? I think default in golang is 8?

// To address this we zeroes every slice before unmarshalling unless user provided slice is nil.
func zeroesSliceHookFunc() mapstructure.DecodeHookFuncValue {
return func(from reflect.Value, to reflect.Value) (interface{}, error) {
if from.Kind() == reflect.Slice && from.IsNil() {
Copy link
Member

Choose a reason for hiding this comment

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

Lets start with this:
nil != initialized and len 0

foo:
foo: []

What is the behavior we want in these cases? Please document them somewhere, in order for me to understand if the implementation is ok.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 8, 2022

This PR was marked stale due to lack of activity. It will be closed in 14 days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants