-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2159 from hashicorp/b-consul-config
Fixed merging consul config
- Loading branch information
Showing
13 changed files
with
255 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package helper | ||
|
||
// boolToPtr returns the pointer to a boolean | ||
func BoolToPtr(b bool) *bool { | ||
return &b | ||
} | ||
|
||
// MapStringStringSliceValueSet returns the set of values in a map[string][]string | ||
func MapStringStringSliceValueSet(m map[string][]string) []string { | ||
set := make(map[string]struct{}) | ||
for _, slice := range m { | ||
for _, v := range slice { | ||
set[v] = struct{}{} | ||
} | ||
} | ||
|
||
flat := make([]string, 0, len(set)) | ||
for k := range set { | ||
flat = append(flat, k) | ||
} | ||
return flat | ||
} | ||
|
||
func SliceStringToSet(s []string) map[string]struct{} { | ||
m := make(map[string]struct{}, (len(s)+1)/2) | ||
for _, k := range s { | ||
m[k] = struct{}{} | ||
} | ||
return m | ||
} | ||
|
||
// SliceStringIsSubset returns whether the smaller set of strings is a subset of | ||
// the larger. If the smaller slice is not a subset, the offending elements are | ||
// returned. | ||
func SliceStringIsSubset(larger, smaller []string) (bool, []string) { | ||
largerSet := make(map[string]struct{}, len(larger)) | ||
for _, l := range larger { | ||
largerSet[l] = struct{}{} | ||
} | ||
|
||
subset := true | ||
var offending []string | ||
for _, s := range smaller { | ||
if _, ok := largerSet[s]; !ok { | ||
subset = false | ||
offending = append(offending, s) | ||
} | ||
} | ||
|
||
return subset, offending | ||
} | ||
|
||
func SliceSetDisjoint(first, second []string) (bool, []string) { | ||
contained := make(map[string]struct{}, len(first)) | ||
for _, k := range first { | ||
contained[k] = struct{}{} | ||
} | ||
|
||
offending := make(map[string]struct{}) | ||
for _, k := range second { | ||
if _, ok := contained[k]; ok { | ||
offending[k] = struct{}{} | ||
} | ||
} | ||
|
||
if len(offending) == 0 { | ||
return true, nil | ||
} | ||
|
||
flattened := make([]string, 0, len(offending)) | ||
for k := range offending { | ||
flattened = append(flattened, k) | ||
} | ||
return false, flattened | ||
} | ||
|
||
// Helpers for copying generic structures. | ||
func CopyMapStringString(m map[string]string) map[string]string { | ||
l := len(m) | ||
if l == 0 { | ||
return nil | ||
} | ||
|
||
c := make(map[string]string, l) | ||
for k, v := range m { | ||
c[k] = v | ||
} | ||
return c | ||
} | ||
|
||
func CopyMapStringInt(m map[string]int) map[string]int { | ||
l := len(m) | ||
if l == 0 { | ||
return nil | ||
} | ||
|
||
c := make(map[string]int, l) | ||
for k, v := range m { | ||
c[k] = v | ||
} | ||
return c | ||
} | ||
|
||
func CopyMapStringFloat64(m map[string]float64) map[string]float64 { | ||
l := len(m) | ||
if l == 0 { | ||
return nil | ||
} | ||
|
||
c := make(map[string]float64, l) | ||
for k, v := range m { | ||
c[k] = v | ||
} | ||
return c | ||
} | ||
|
||
func CopySliceString(s []string) []string { | ||
l := len(s) | ||
if l == 0 { | ||
return nil | ||
} | ||
|
||
c := make([]string, l) | ||
for i, v := range s { | ||
c[i] = v | ||
} | ||
return c | ||
} | ||
|
||
func CopySliceInt(s []int) []int { | ||
l := len(s) | ||
if l == 0 { | ||
return nil | ||
} | ||
|
||
c := make([]int, l) | ||
for i, v := range s { | ||
c[i] = v | ||
} | ||
return c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package helper | ||
|
||
import ( | ||
"reflect" | ||
"sort" | ||
"testing" | ||
) | ||
|
||
func TestSliceStringIsSubset(t *testing.T) { | ||
l := []string{"a", "b", "c"} | ||
s := []string{"d"} | ||
|
||
sub, offending := SliceStringIsSubset(l, l[:1]) | ||
if !sub || len(offending) != 0 { | ||
t.Fatalf("bad %v %v", sub, offending) | ||
} | ||
|
||
sub, offending = SliceStringIsSubset(l, s) | ||
if sub || len(offending) == 0 || offending[0] != "d" { | ||
t.Fatalf("bad %v %v", sub, offending) | ||
} | ||
} | ||
|
||
func TestMapStringStringSliceValueSet(t *testing.T) { | ||
m := map[string][]string{ | ||
"foo": []string{"1", "2"}, | ||
"bar": []string{"3"}, | ||
"baz": nil, | ||
} | ||
|
||
act := MapStringStringSliceValueSet(m) | ||
exp := []string{"1", "2", "3"} | ||
sort.Strings(act) | ||
if !reflect.DeepEqual(act, exp) { | ||
t.Fatalf("Bad; got %v; want %v", act, exp) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.