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

Feature/support labels #26

Closed

Conversation

huguesmcneilduval
Copy link

@huguesmcneilduval huguesmcneilduval commented Nov 25, 2021

Changes Description

Added support for labels. A spring config server path is "app/profiles/label" . https://cloud.spring.io/spring-cloud-config/reference/html/#_locating_remote_configuration_resources

Flatting the configuration in a "map" way.

From

{
 "my.config1": "value",
 "my.config2": "value2"
}

to

{
 "my": {
    "config1": "value,
    "config2": "value2"
  }
}

Associated Issues

I needed the labels and a get_property function to retrieve "my.config1" value for example.

Affected Files

See commits

Unit Tests Changed or Added

I've added tests for all files

Let me know if this interested you. I'll will probably add more functionality to this lib as I go along.

…ath":"value2" to "my.config: path:value, secondpath:value2. Its hard to describe in git commit, but Im flatting the config in a yaml way
* "feature/support-labels"

* Transforming configs like "my.config.path":"value, "my.config.secondpath":"value2" to "my.config: path:value, secondpath:value2. Its hard to describe in git commit, but Im flatting the config in a yaml way
client.go Outdated
@@ -4,11 +4,12 @@ import (
"context"
"errors"
"fmt"
"github.com/Piszmog/cfservices"
"golang.org/x/oauth2/clientcredentials"
Copy link
Owner

Choose a reason for hiding this comment

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

Please revert this. For this project, import formatting uses gofmt instead of goimports.

Hopefully imports formats get figured out (see #37719)

Copy link
Author

Choose a reason for hiding this comment

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

ok so if I understand well, goimports is a superset of gofmt. gofmt format struct, field and whatnot while goimports add format for imports?
I've manually reverted the import to what they were, I can't seem to use gofmt to reformat struct.

My understanding of reading on the web is that goimports is just better than gofmt.

@@ -3,12 +3,13 @@ package cloudconfigclient_test
import (
"context"
"errors"
"github.com/Piszmog/cloudconfigclient/v2"
"github.com/stretchr/testify/require"
"golang.org/x/oauth2/clientcredentials"
Copy link
Owner

Choose a reason for hiding this comment

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

Same here

Copy link
Author

Choose a reason for hiding this comment

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

fixed

@@ -52,6 +56,18 @@ func (s *Source) HandlePropertySources(handler PropertySourceHandler) {
}
}

func (s *Source) Get(key string, defaultValue string) interface{} {
Copy link
Owner

Choose a reason for hiding this comment

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

docs

Copy link
Author

Choose a reason for hiding this comment

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

Done.

Also this is where the Map I've added could be used. If we use the map to retrieve the config value, we would have complexity O(1) instead of O(n) on every search right. Therefore I think this lib should use the Map as it's core.

configuration.go Outdated
func (s *Source) Get(key string, defaultValue string) interface{} {
for _, propertySource := range s.PropertySources {
for _key, value := range propertySource.Source {
fmt.Printf("key=%s, value=%v\n", _key, value)
Copy link
Owner

Choose a reason for hiding this comment

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

the library should avoid unnecessary logging/noise. Please remove this

Copy link
Author

Choose a reason for hiding this comment

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

done

configuration.go Outdated
@@ -52,6 +56,18 @@ func (s *Source) HandlePropertySources(handler PropertySourceHandler) {
}
}

func (s *Source) Get(key string, defaultValue string) interface{} {
for _, propertySource := range s.PropertySources {
for _key, value := range propertySource.Source {
Copy link
Owner

Choose a reason for hiding this comment

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

rename _key to sourceKey to make it more clear. I was confused at first what _key was

Copy link
Author

Choose a reason for hiding this comment

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

done

go.mod Outdated
@@ -1,4 +1,4 @@
module github.com/Piszmog/cloudconfigclient/v2
module github.com/duvalhub/cloudconfigclient
Copy link
Owner

Choose a reason for hiding this comment

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

revert

Copy link
Author

Choose a reason for hiding this comment

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

done

@@ -1,8 +1,11 @@
package cloudconfigclient
Copy link
Owner

Choose a reason for hiding this comment

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

General Formatting needs to be done. Please use gofmt

Copy link
Owner

@Piszmog Piszmog left a comment

Choose a reason for hiding this comment

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

See comments

@Piszmog
Copy link
Owner

Piszmog commented Nov 25, 2021

So the flatness is from the Config Server itself. The key values are in a Properties format. Looking at the code and your initial PR comment, I would recommend a function that converts Properties format to the structure that you want, vs what you have now.

For example,

func (p *PropertySource) FromProperties() map[string]interface{} {
  ...
}

This is where we would unflatten the key values

return dest
}

type BasicAuthTransport struct {
Copy link
Owner

Choose a reason for hiding this comment

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

this bit and down seems redundant and is more application specific implementation of this library. I do not think it belong in this library.

Copy link
Author

Choose a reason for hiding this comment

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

I've added this BasicAuthTransport because this lib does not support Basic Authentication with ConfigServer (unless I missed something). This bit of code handle that.

@huguesmcneilduval
Copy link
Author

huguesmcneilduval commented Dec 1, 2021

So the flatness is from the Config Server itself. The key values are in a Properties format. Looking at the code and your initial PR comment, I would recommend a function that converts Properties format to the structure that you want, vs what you have now.

For example,

func (p *PropertySource) FromProperties() map[string]interface{} {
  ...
}

This is where we would unflatten the key values

My idea is that the returned format from Config Server is a bit useless. What gives to loop through all PropertySource when we can access the value straight in a map. It's also less efficient. Like the function func (s *Source) Get(key string, defaultValue string) interface{} is not efficient at the moment. I feel like this whole lib should be based on the Map insteand of on

Also my plan (or my used case) is to be able to map a config key to a struct, like this :
structObject := &StructObject{} json := source.Data["path.to.config.dict"] mapstructure.Decode(json, &structObject )
This is a application specific but by experience, I know that we always want to consume configuration by mapping them to a struct (or java class, or python dataclass, etc). I would like to have a function in this lib that can do what @ConfigurationProperties does basically. Give a struct type and a key path to config and get your struct back populated with the values in the conf.

client.go Outdated
@@ -4,11 +4,12 @@ import (
"context"
"errors"
"fmt"
"github.com/Piszmog/cfservices"
"golang.org/x/oauth2/clientcredentials"
Copy link
Author

Choose a reason for hiding this comment

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

ok so if I understand well, goimports is a superset of gofmt. gofmt format struct, field and whatnot while goimports add format for imports?
I've manually reverted the import to what they were, I can't seem to use gofmt to reformat struct.

My understanding of reading on the web is that goimports is just better than gofmt.

@@ -3,12 +3,13 @@ package cloudconfigclient_test
import (
"context"
"errors"
"github.com/Piszmog/cloudconfigclient/v2"
"github.com/stretchr/testify/require"
"golang.org/x/oauth2/clientcredentials"
Copy link
Author

Choose a reason for hiding this comment

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

fixed

@@ -52,6 +56,18 @@ func (s *Source) HandlePropertySources(handler PropertySourceHandler) {
}
}

func (s *Source) Get(key string, defaultValue string) interface{} {
Copy link
Author

Choose a reason for hiding this comment

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

Done.

Also this is where the Map I've added could be used. If we use the map to retrieve the config value, we would have complexity O(1) instead of O(n) on every search right. Therefore I think this lib should use the Map as it's core.

configuration.go Outdated
@@ -52,6 +56,18 @@ func (s *Source) HandlePropertySources(handler PropertySourceHandler) {
}
}

func (s *Source) Get(key string, defaultValue string) interface{} {
for _, propertySource := range s.PropertySources {
for _key, value := range propertySource.Source {
Copy link
Author

Choose a reason for hiding this comment

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

done

configuration.go Outdated
func (s *Source) Get(key string, defaultValue string) interface{} {
for _, propertySource := range s.PropertySources {
for _key, value := range propertySource.Source {
fmt.Printf("key=%s, value=%v\n", _key, value)
Copy link
Author

Choose a reason for hiding this comment

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

done

return dest
}

type BasicAuthTransport struct {
Copy link
Author

Choose a reason for hiding this comment

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

I've added this BasicAuthTransport because this lib does not support Basic Authentication with ConfigServer (unless I missed something). This bit of code handle that.

configuration.go Outdated
return dest
}

func insertInMapRecursion(s []string, value interface{}, dest map[string]interface{}) map[string]interface{} {
Copy link
Author

Choose a reason for hiding this comment

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

insertInMap and insertInMapRecursion have the same functional logic. We should keep one of the two.

go.mod Outdated
@@ -1,4 +1,4 @@
module github.com/Piszmog/cloudconfigclient/v2
module github.com/duvalhub/cloudconfigclient
Copy link
Author

Choose a reason for hiding this comment

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

done

@sonarcloud
Copy link

sonarcloud bot commented Dec 1, 2021

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@Piszmog
Copy link
Owner

Piszmog commented Feb 27, 2022

Hi, sorry for taking a long time to get back to you on this. I have add the functionality to unmarshal the ProeprtySource slice in Source to a struct that you provide.

See #27 for an example

@Piszmog Piszmog closed this Feb 27, 2022
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