Skip to content

Commit

Permalink
refactor!: Remove use of TOML package
Browse files Browse the repository at this point in the history
BREAKING CHANGE: PutConfigurationToml has be rework to be PutConfigurationMap

Signed-off-by: Leonard Goodell <leonard.goodell@intel.com>
  • Loading branch information
Leonard Goodell committed Mar 21, 2023
1 parent 7b762e4 commit 5ececa6
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 61 deletions.
9 changes: 3 additions & 6 deletions configuration/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,16 @@

package configuration

import (
"github.com/pelletier/go-toml"
)

type Client interface {
// HasConfiguration checks to see if the Configuration service contains the service's configuration.
HasConfiguration() (bool, error)

// HasSubConfiguration checks to see if the Configuration service contains the service's sub configuration.
HasSubConfiguration(name string) (bool, error)

// PutConfigurationToml puts a full toml configuration into the Configuration service
PutConfigurationToml(configuration *toml.Tree, overwrite bool) error
// PutConfigurationMap puts a full map configuration into the Configuration service
// The sub-paths to where the values are to be stored in the Configuration service are generated from the map key.
PutConfigurationMap(configuration map[string]any, overwrite bool) error

// PutConfiguration puts a full configuration struct into the Configuration service
PutConfiguration(configStruct interface{}, overwrite bool) error
Expand Down
13 changes: 5 additions & 8 deletions configuration/mocks/Client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.20
require (
github.com/hashicorp/consul/api v1.20.0
github.com/mitchellh/consulstructure v0.0.0-20190329231841-56fdc4d2da54
github.com/pelletier/go-toml v1.9.5
github.com/stretchr/testify v1.8.2
)

Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down
22 changes: 11 additions & 11 deletions internal/pkg/consul/client.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (c) 2021 Intel Corporation
// Copyright (c) 2023 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,7 @@ package consul

import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
Expand All @@ -26,11 +27,9 @@ import (
"sync"
"time"

"github.com/edgexfoundry/go-mod-configuration/v3/pkg/types"
consulapi "github.com/hashicorp/consul/api"
"github.com/mitchellh/consulstructure"
"github.com/pelletier/go-toml"

"github.com/edgexfoundry/go-mod-configuration/v3/pkg/types"
)

const (
Expand Down Expand Up @@ -140,11 +139,11 @@ func (client *consulClient) HasSubConfiguration(name string) (bool, error) {
return true, nil
}

// PutConfigurationToml puts a full toml configuration into Consul
func (client *consulClient) PutConfigurationToml(configuration *toml.Tree, overwrite bool) error {
// PutConfigurationMap puts a full configuration map into Consul.
// The sub-paths to where the values are to be stored in Consul are generated from the map key.
func (client *consulClient) PutConfigurationMap(configuration map[string]any, overwrite bool) error {

configurationMap := configuration.ToMap()
keyValues := convertInterfaceToConsulPairs("", configurationMap)
keyValues := convertInterfaceToConsulPairs("", configuration)

// Put config properties into Consul.
for _, keyValue := range keyValues {
Expand All @@ -161,17 +160,18 @@ func (client *consulClient) PutConfigurationToml(configuration *toml.Tree, overw

// PutConfiguration puts a full configuration struct into the Configuration provider
func (client *consulClient) PutConfiguration(configuration interface{}, overwrite bool) error {
bytes, err := toml.Marshal(configuration)
configMap := make(map[string]any)
bytes, err := json.Marshal(configuration)
if err != nil {
return err
}

tree, err := toml.LoadBytes(bytes)
err = json.Unmarshal(bytes, &configMap)
if err != nil {
return err
}

err = client.PutConfigurationToml(tree, overwrite)
err = client.PutConfigurationMap(configMap, overwrite)
if err != nil {
return err
}
Expand Down
69 changes: 36 additions & 33 deletions internal/pkg/consul/client_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (c) 2021 Intel Corporation
// Copyright (c) 2023 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,6 @@ package consul

import (
"fmt"
"log"
"net/http/httptest"
"net/url"
"os"
Expand All @@ -29,7 +28,6 @@ import (
"time"

"github.com/hashicorp/consul/api"
"github.com/pelletier/go-toml"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -90,10 +88,10 @@ func TestIsAlive(t *testing.T) {
func TestHasConfigurationFalse(t *testing.T) {
client := makeConsulClient(t, getUniqueServiceName(), "", nil)

// Make sure the configuration doesn't already exists
// Make sure the configuration doesn't already exist
reset(t, client)

// Don't push anything in yet so configuration will not exists
// Don't push anything in yet so configuration will not exist

actual, err := client.HasConfiguration()
if !assert.NoError(t, err) {
Expand All @@ -106,7 +104,7 @@ func TestHasConfigurationFalse(t *testing.T) {
func TestHasConfigurationTrue(t *testing.T) {
client := makeConsulClient(t, getUniqueServiceName(), "", nil)

// Make sure the configuration doesn't already exists
// Make sure the configuration doesn't already exist
reset(t, client)

// Now push a value so the configuration will exist
Expand All @@ -123,7 +121,7 @@ func TestHasConfigurationTrue(t *testing.T) {
func TestHasConfigurationPartialServiceKey(t *testing.T) {
client := makeConsulClient(t, getUniqueServiceName(), "", nil)

// Make sure the configuration doesn't already exists
// Make sure the configuration doesn't already exist
reset(t, client)

base := client.configBasePath
Expand Down Expand Up @@ -166,7 +164,7 @@ func TestHasConfigurationError(t *testing.T) {
func TestHasSubConfigurationFalse(t *testing.T) {
client := makeConsulClient(t, getUniqueServiceName(), "", nil)

// Make sure the configuration doesn't already exists
// Make sure the configuration doesn't already exist
reset(t, client)

// Now push a value so some configuration will exist
Expand All @@ -183,7 +181,7 @@ func TestHasSubConfigurationFalse(t *testing.T) {
func TestHasSubConfigurationTrue(t *testing.T) {
client := makeConsulClient(t, getUniqueServiceName(), "", nil)

// Make sure the configuration doesn't already exists
// Make sure the configuration doesn't already exist
reset(t, client)

// Now push a value so some configuration will exist
Expand Down Expand Up @@ -221,7 +219,7 @@ func TestConfigurationValueExists(t *testing.T) {
client := makeConsulClient(t, uniqueServiceName, "", nil)
expected := false

// Make sure the configuration doesn't already exists
// Make sure the configuration doesn't already exist
reset(t, client)

actual, err := client.ConfigurationValueExists(key)
Expand Down Expand Up @@ -310,7 +308,7 @@ func TestPutConfigurationValue(t *testing.T) {

client := makeConsulClient(t, uniqueServiceName, "", nil)

// Make sure the configuration doesn't already exists
// Make sure the configuration doesn't already exist
reset(t, client)

_, _ = client.consulClient.KV().Delete(expectedFullKey, nil)
Expand Down Expand Up @@ -414,7 +412,7 @@ func configValueSet(key string, client *consulClient) bool {
return exists
}

func TestPutConfigurationTomlNoPreviousValues(t *testing.T) {
func TestPutConfigurationMapNoPreviousValues(t *testing.T) {
client := makeConsulClient(t, getUniqueServiceName(), "", nil)

// Make sure the tree of values doesn't exist.
Expand All @@ -426,11 +424,7 @@ func TestPutConfigurationTomlNoPreviousValues(t *testing.T) {
}()

configMap := createKeyValueMap()
configuration, err := toml.TreeFromMap(configMap)
if err != nil {
log.Fatalf("unable to create TOML Tree from map: %v", err)
}
err = client.PutConfigurationToml(configuration, false)
err := client.PutConfigurationMap(configMap, false)
if !assert.NoError(t, err) {
t.Fatal()
}
Expand All @@ -449,7 +443,7 @@ func TestPutConfigurationTomlNoPreviousValues(t *testing.T) {
}
}

func TestPutConfigurationTomlWithoutOverWrite(t *testing.T) {
func TestPutConfigurationMapWithoutOverWrite(t *testing.T) {
client := makeConsulClient(t, getUniqueServiceName(), "", nil)

// Make sure the tree of values doesn't exist.
Expand All @@ -462,8 +456,7 @@ func TestPutConfigurationTomlWithoutOverWrite(t *testing.T) {

configMap := createKeyValueMap()

configuration, _ := toml.TreeFromMap(configMap)
err := client.PutConfigurationToml(configuration, false)
err := client.PutConfigurationMap(configMap, false)
if !assert.NoError(t, err) {
t.Fatal()
}
Expand All @@ -474,10 +467,15 @@ func TestPutConfigurationTomlWithoutOverWrite(t *testing.T) {
configMap["float64"] = 2.4
configMap["string"] = "bye"
configMap["bool"] = false
subMap := configMap["sub-map"].(map[string]any)
subMap["int"] = 45
subMap["int64"] = 789
subMap["float64"] = 23.45
subMap["string"] = "another value"
subMap["bool"] = true

// Try to put new values with overwrite = false
configuration, _ = toml.TreeFromMap(configMap)
err = client.PutConfigurationToml(configuration, false)
err = client.PutConfigurationMap(configMap, false)
if !assert.NoError(t, err) {
t.Fatal()
}
Expand All @@ -496,7 +494,7 @@ func TestPutConfigurationTomlWithoutOverWrite(t *testing.T) {
}
}

func TestPutConfigurationTomlOverWrite(t *testing.T) {
func TestPutConfigurationMapOverWrite(t *testing.T) {
client := makeConsulClient(t, getUniqueServiceName(), "", nil)

// Make sure the tree of values doesn't exist.
Expand All @@ -508,8 +506,7 @@ func TestPutConfigurationTomlOverWrite(t *testing.T) {

configMap := createKeyValueMap()

configuration, _ := toml.TreeFromMap(configMap)
err := client.PutConfigurationToml(configuration, false)
err := client.PutConfigurationMap(configMap, false)
if !assert.NoError(t, err) {
t.Fatal()
}
Expand All @@ -521,8 +518,7 @@ func TestPutConfigurationTomlOverWrite(t *testing.T) {
configMap["bool"] = false

// Try to put new values with overwrite = True
configuration, _ = toml.TreeFromMap(configMap)
err = client.PutConfigurationToml(configuration, true)
err = client.PutConfigurationMap(configMap, true)
if !assert.NoError(t, err) {
t.Fatal()
}
Expand Down Expand Up @@ -585,7 +581,7 @@ func TestWatchForChanges(t *testing.T) {
assert.NotNil(t, loggingChanges)
logInfo := loggingChanges.(*LoggingInfo)

// first pass is for Consul Decoder always sending data once watch has been setup. It hasn't actually changed
// first pass is for Consul Decoder always sending data once watch has been set up. It hasn't actually changed
if loggingPass == 1 {
if !assert.Equal(t, logInfo.File, expectedConfig.Logging.File) {
t.Fatal()
Expand Down Expand Up @@ -613,7 +609,7 @@ func TestAccessToken(t *testing.T) {
client := makeConsulClient(t, uniqueServiceName, "", nil)
expectedErrMsg := "Unexpected response code: 403"
valueName := "testAccess"
// Test if have access to endpoint w/o access token set
// Test if there is access to endpoint w/o access token set

_, err := client.GetConfigurationValue(valueName)
require.NoError(t, err)
Expand Down Expand Up @@ -668,20 +664,27 @@ func makeConsulClient(t *testing.T, serviceName string, accessToken string, toke
return client
}

func createKeyValueMap() map[string]interface{} {
configMap := make(map[string]interface{})
func createKeyValueMap() map[string]any {
configMap := make(map[string]any)

configMap["int"] = 1
configMap["int64"] = int64(64)
configMap["float64"] = float64(1.4)
configMap["float64"] = 1.4
configMap["string"] = "hello"
configMap["bool"] = true
configMap["sub-map"] = map[string]any{
"string": "some value",
"bool": false,
"int": 6,
"int64": int64(34),
"float64": 6.3,
}

return configMap
}

func reset(t *testing.T, client *consulClient) {
// Make sure the configuration doesn't already exists
// Make sure the configuration doesn't already exist
if mockConsul != nil {
mockConsul.Reset()
} else {
Expand Down

0 comments on commit 5ececa6

Please sign in to comment.