Skip to content

Allow to diff two configurations and return the difference in the keys.#93

Merged
urso merged 3 commits intoelastic:masterfrom
ph:feature/allow-to-diff-configuration-keys
Jan 8, 2018
Merged

Allow to diff two configurations and return the difference in the keys.#93
urso merged 3 commits intoelastic:masterfrom
ph:feature/allow-to-diff-configuration-keys

Conversation

@ph
Copy link
Copy Markdown
Contributor

@ph ph commented Nov 27, 2017

Pass 2 difference Config object to DiffKeys and it will return a Diff
type that will contains the following:

  • Added keys
  • Keep keys
  • Removed keys

Add a few helpers method on the type to know if something is different
between the 2 configuration.

@ph
Copy link
Copy Markdown
Contributor Author

ph commented Nov 27, 2017

@urso When working on the implicit keystore usage in beats, I needed something like this.

diff.go Outdated
}
} else {
// We should not get here
panic("unknown config type")
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I believe this should not happen but I think it could.

@urso
Copy link
Copy Markdown

urso commented Nov 27, 2017

+1

In go-ucfg, the top-level package is mostly the core for packing/unpacking/querying the config object. All extra functionality is in sub-packages.

As getters/setters can operate on complete settings paths, adding the FlattenKeys as method to *Config does totally make sense to me.

@ph
Copy link
Copy Markdown
Contributor Author

ph commented Nov 28, 2017

@urso I have applied the changes we were talking about.

"fmt"
"strings"

ucfg "github.com/elastic/go-ucfg"
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Note: This line is equivalent to "github.com/elastic/go-ucfg".

goimports normaly adds this kind of line. If - is used in package names, the later part of the name makes the package.

diff/keys.go Outdated
}

// Keys takes two configuration and return the difference between the defined keys
func Keys(old, new *ucfg.Config, opts ...ucfg.Option) Diff {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is basically the constructor. How about naming it one of (Make/Build/Create)Diff, Compare, CompareConfigs or Changes?

// FlattenedKeys return a flattened views of the set keys in the configuration
func (c *Config) FlattenedKeys(opts ...Option) []string {
var keys []string
normalizedOptions := makeOptions(opts)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Not sure, but pathSep might be empty here. If so add:

if normalizedOptions.pathSep == "" {
  normalizedOptions.pathSep = "."
}

ucfg.go Outdated
}
} else {
// We should not get here
panic("unknown config type")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

hm... not sure about 'empty' config objects.

panic("unknown config type")
}

sort.Strings(keys)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We need to sort here? If so the godoc should reflect the list returned is always being sorted.

@ph
Copy link
Copy Markdown
Contributor Author

ph commented Nov 28, 2017

@urso I choose CompareConfigs, I think it will be more explicit that way.

sort.Strings(results)

assert.Equal(t, expected, results)
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

tests are quite similar. Rather introduce a table-driven test + use Run like:

func TestFlattenKeys(t *testing.T) {
  tests := struct {
    name string
    pathSep string
  }{
    { "withDot", "." },
    { "emptySep", "" },
  }

  sorted := func(s []string) []string {
    sort.Strings(s)
    return s
  }

  cfg := map[string]interface{}{ ... }
  expected := sorted([]string { ... })

  for _, test := range tests {
    sep := test.pathSep
    t.Run(test.name, func(t *testing.T) {
      opts := []Option{}
      if sep != "" {
        opts = append(opts, PathSep(sep))
      }

      c, err := NewFrom(cfg, opts...)
      if err != nil { t.Fatal(err) }
      assert.Equal(t, expected, sorted(c.FlattenKeys(opts...)))
    })
  }
}

@ph
Copy link
Copy Markdown
Contributor Author

ph commented Jan 8, 2018

@urso, I've moved the two similar test into a table driven test.

@ph
Copy link
Copy Markdown
Contributor Author

ph commented Jan 8, 2018

seems to be a problem with the ci, I see that you are working on it on #95.

@ph
Copy link
Copy Markdown
Contributor Author

ph commented Jan 8, 2018

rebased with master, the ci should run.

ph added 3 commits January 8, 2018 16:13
Pass 2 difference Config object to DiffKeys and it will return a Diff
type that will contains the following:

- Added keys
- Keep keys
- Removed keys

Add a few helpers method on the type to know if something is different
between the 2 configuration.
@urso urso merged commit b392c80 into elastic:master Jan 8, 2018
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.

2 participants