Skip to content

Commit c2d468b

Browse files
stevendannaalexpop
authored andcommitted
compliance, nodemanager: removed unused functions (#1138)
While this removes code, I think it is a bit of a downside as these functions are kind of a family of functions that make sense to travel together. My hope in cleaning up this kind of thing between services is so that we can start to see what kind of functionality might be useful to share somewhere. Signed-off-by: Steven Danna <[email protected]>
1 parent e4ae3ff commit c2d468b

File tree

4 files changed

+1
-263
lines changed

4 files changed

+1
-263
lines changed

components/compliance-service/dao/pgdb/tags.go

+1-29
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package pgdb
22

33
import (
4-
"encoding/json"
4+
"github.com/pkg/errors"
55

66
"github.com/chef/automate/components/compliance-service/api/common"
7-
"github.com/pkg/errors"
87
)
98

109
type tag struct {
@@ -68,30 +67,3 @@ func RemoveKeyValue(tags []*common.Kv, key string) []*common.Kv {
6867
}
6968
return tags
7069
}
71-
72-
// KeyValueToRawMap helps convert an array of KeyValues in a Map and convert it to json
73-
func KeyValueToRawMap(arr []*common.Kv) (json.RawMessage, error) {
74-
zaMap := make(map[string]string, 0)
75-
for _, kv := range arr {
76-
zaMap[kv.Key] = kv.Value
77-
}
78-
jsonMap, err := json.Marshal(zaMap)
79-
if err != nil {
80-
return jsonMap, errors.Wrap(err, "keyValueToRawMap unable to marshal map")
81-
}
82-
return jsonMap, nil
83-
}
84-
85-
// RawMapToKeyValue helps convert an array of KeyValues in a Map and convert it to json
86-
func RawMapToKeyValue(rawJSON json.RawMessage) ([]*common.Kv, error) {
87-
var zaMap map[string]string
88-
var zaArray []*common.Kv
89-
err := json.Unmarshal(rawJSON, &zaMap)
90-
if err != nil {
91-
return zaArray, errors.Wrap(err, "rawMapToKeyValue unable to unmarshal map")
92-
}
93-
for k, v := range zaMap {
94-
zaArray = append(zaArray, &common.Kv{Key: k, Value: v})
95-
}
96-
return zaArray, nil
97-
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package pgdb
22

33
import (
4-
"encoding/json"
54
"testing"
65

76
"github.com/chef/automate/components/compliance-service/api/common"
@@ -64,61 +63,3 @@ func TestRemoveKeyValueReturnsArrayWithKeyRemoved(t *testing.T) {
6463

6564
assert.Equal(t, []*common.Kv{&secret2}, result)
6665
}
67-
68-
func TestKeyValueToRawMapReturnsEmptyMessageWhenArrayEmpty(t *testing.T) {
69-
var tags []*common.Kv
70-
var bytes []byte
71-
var err error
72-
var j json.RawMessage
73-
74-
if j, err = KeyValueToRawMap(tags); err != nil {
75-
t.FailNow()
76-
}
77-
78-
bytes, _ = j.MarshalJSON()
79-
80-
assert.Equal(t, "{}", string(bytes))
81-
}
82-
83-
func TestKeyValueToRawMapReturnsJsonMessage(t *testing.T) {
84-
secret1 := common.Kv{Key: "key1", Value: "value1"}
85-
secret2 := common.Kv{Key: "key2", Value: "value2"}
86-
tags := []*common.Kv{&secret1, &secret2}
87-
88-
var bytes []byte
89-
var err error
90-
var j json.RawMessage
91-
92-
if j, err = KeyValueToRawMap(tags); err != nil {
93-
t.FailNow()
94-
}
95-
96-
bytes, _ = j.MarshalJSON()
97-
98-
assert.Equal(t, "{\"key1\":\"value1\",\"key2\":\"value2\"}", string(bytes))
99-
}
100-
101-
func TestRawMapToKeyValueReturnsEmptyTagsWhenJsonEmpty(t *testing.T) {
102-
var tags []*common.Kv
103-
j := json.RawMessage(`{}`)
104-
105-
result, err := RawMapToKeyValue(j)
106-
107-
if assert.Nil(t, err) {
108-
assert.ElementsMatch(t, tags, result)
109-
}
110-
}
111-
112-
func TestRawMapToKeyValueReturnsKeyValues(t *testing.T) {
113-
secret1 := common.Kv{Key: "key1", Value: "value1"}
114-
secret2 := common.Kv{Key: "key2", Value: "value2"}
115-
tags := []*common.Kv{&secret1, &secret2}
116-
117-
j := json.RawMessage(`{"key1":"value1","key2":"value2"}`)
118-
119-
result, err := RawMapToKeyValue(j)
120-
121-
if assert.Nil(t, err) {
122-
assert.ElementsMatch(t, tags, result)
123-
}
124-
}

components/nodemanager-service/pgdb/tags.go

-51
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package pgdb
22

33
import (
4-
"encoding/json"
5-
64
"github.com/pkg/errors"
75
"github.com/sirupsen/logrus"
86

@@ -104,52 +102,3 @@ func (trans *DBTrans) tagNode(nodeID string, tagIDs []string) error {
104102
}
105103
return trans.Insert(links...)
106104
}
107-
108-
// FindKeyValue finds a Tag object in the array based on key match
109-
func FindKeyValue(tags []*common.Kv, key string) *common.Kv {
110-
for _, tag := range tags {
111-
if tag.Key == key {
112-
return tag
113-
}
114-
}
115-
return &common.Kv{}
116-
}
117-
118-
// RemoveKeyValue removes an item from the array base on key match
119-
func RemoveKeyValue(tags []*common.Kv, key string) []*common.Kv {
120-
for i, tag := range tags {
121-
if tag.Key == key {
122-
tags[i] = tags[len(tags)-1]
123-
return tags[:len(tags)-1]
124-
}
125-
}
126-
return tags
127-
}
128-
129-
// KeyValueToRawMap helps convert an array of KeyValues in a Map and convert it to json
130-
func KeyValueToRawMap(arr []*common.Kv) (json.RawMessage, error) {
131-
zaMap := make(map[string]string, 0)
132-
for _, kv := range arr {
133-
zaMap[kv.Key] = kv.Value
134-
}
135-
jsonMap, err := json.Marshal(zaMap)
136-
if err != nil {
137-
return jsonMap, errors.Wrap(err, "keyValueToRawMap unable to marshal map")
138-
}
139-
return jsonMap, nil
140-
}
141-
142-
// RawMapToKeyValue helps convert an array of KeyValues in a Map and convert it to json
143-
func RawMapToKeyValue(rawJSON json.RawMessage) ([]*common.Kv, error) {
144-
var zaMap map[string]string
145-
err := json.Unmarshal(rawJSON, &zaMap)
146-
if err != nil {
147-
return nil, errors.Wrap(err, "rawMapToKeyValue unable to unmarshal map")
148-
}
149-
150-
zaArray := make([]*common.Kv, 0, len(zaMap))
151-
for k, v := range zaMap {
152-
zaArray = append(zaArray, &common.Kv{Key: k, Value: v})
153-
}
154-
return zaArray, nil
155-
}

components/nodemanager-service/pgdb/tags_test.go

-124
This file was deleted.

0 commit comments

Comments
 (0)