-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
write encoded source_paths to DB to correctly handle database names w…
…ith commas
- Loading branch information
Showing
5 changed files
with
141 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package types | ||
|
||
import ( | ||
"encoding/base64" | ||
"strings" | ||
) | ||
|
||
func ParseSourcePaths(str string) []string { | ||
if str == "" { | ||
return make([]string, 0) | ||
} | ||
codedSlice := strings.Split(str, ",") | ||
slice := make([]string, len(codedSlice)) | ||
for i, s := range codedSlice { | ||
data, err := base64.StdEncoding.DecodeString(s) | ||
if err != nil { | ||
slice[i] = s | ||
} else { | ||
slice[i] = string(data) | ||
} | ||
} | ||
return slice | ||
} | ||
|
||
func SerializeSourcePaths(slice []string) string { | ||
codedSlice := make([]string, len(slice)) | ||
for i, s := range slice { | ||
codedSlice[i] = base64.StdEncoding.EncodeToString([]byte(s)) | ||
} | ||
return strings.Join(codedSlice, ",") | ||
} |
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,92 @@ | ||
package types | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestParseSourcePaths(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
expected []string | ||
}{ | ||
{ | ||
name: "Parse base64", | ||
input: "L3Rlc3RpbmctZ2xvYmFsL2lhbQ==,L3BhdGgsd2l0aCxjb21tYXM=", | ||
expected: []string{"/testing-global/iam", "/path,with,commas"}, | ||
}, | ||
{ | ||
name: "Parse plaintext", | ||
input: "hello,world", | ||
expected: []string{"hello", "world"}, | ||
}, | ||
{ | ||
name: "Empty input string", | ||
input: "", | ||
expected: []string{}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := ParseSourcePaths(tt.input) | ||
if !reflect.DeepEqual(result, tt.expected) { | ||
t.Errorf("expected %v, got %v", tt.expected, result) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestSerializeSourcePaths(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input []string | ||
expected string | ||
}{ | ||
{ | ||
name: "Serialize base64", | ||
input: []string{"hello", "world"}, | ||
expected: "aGVsbG8=,d29ybGQ=", | ||
}, | ||
{ | ||
name: "Serialize plaintext", | ||
input: []string{"hello"}, | ||
expected: "aGVsbG8=", | ||
}, | ||
{ | ||
name: "Serialize empty slice", | ||
input: []string{}, | ||
expected: "", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := SerializeSourcePaths(tt.input) | ||
if result != tt.expected { | ||
t.Errorf("expected %s, got %s", tt.expected, result) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestSymmetry(t *testing.T) { | ||
tests := []struct { | ||
input []string | ||
}{ | ||
{ | ||
input: []string{"/testing-global/iam", "idk,,,strangepath"}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run("", func(t *testing.T) { | ||
serialized := SerializeSourcePaths(tt.input) | ||
parsed := ParseSourcePaths(serialized) | ||
if !reflect.DeepEqual(parsed, tt.input) { | ||
t.Errorf("expected %v, got %v", tt.input, parsed) | ||
} | ||
}) | ||
} | ||
} |