corso/src/internal/kopia/path_encoder.go
Keepers 62daf10213
migrate onedrive using prefix collection (#3122)
#### Does this PR need a docs update or release note?

- [x] 🕐 Yes, but in a later PR

#### Type of change

- [x] 🌻 Feature

#### Issue(s)

* #2825

#### Test Plan

- [x] 💪 Manual
- [x]  Unit test
- [x] 💚 E2E
2023-04-24 17:36:10 +00:00

54 lines
1.4 KiB
Go

package kopia
import (
"encoding/base64"
"path"
"github.com/alcionai/clues"
)
var encoder = base64.URLEncoding
// encodeElements takes a set of strings and returns a slice of the strings
// after encoding them to a file system-safe format. Elements are returned in
// the same order they were passed in.
func encodeElements(elements ...string) []string {
encoded := make([]string, 0, len(elements))
for _, e := range elements {
encoded = append(encoded, encoder.EncodeToString([]byte(e)))
}
return encoded
}
func decodeElements(elements ...string) ([]string, error) {
decoded := make([]string, 0, len(elements))
for _, e := range elements {
bs, err := encoder.DecodeString(e)
if err != nil {
return nil, clues.Wrap(err, "decoding element").With("element", e)
}
decoded = append(decoded, string(bs))
}
return decoded, nil
}
// encodeAsPath takes a set of elements and returns the concatenated elements as
// if they were a path. The elements are joined with the separator in the golang
// path package.
func encodeAsPath(elements ...string) string {
// Needs `/` to be used a separator here
//nolint:forbidigo
return path.Join(encodeElements(elements...)...)
}
// decodeElement takes an encoded element and decodes it if possible.
func decodeElement(element string) (string, error) {
r, err := encoder.DecodeString(element)
return string(r), err
}