corso/src/pkg/path/elements.go
Keepers 1b417af5bb
minor updates to the path.Path interface (#3384)
Swaps path.Append from a single item method to accept a variadic list of string elements.  Since 95% of all calls to path.Append were items, also adds a shorthand
AppendItem func to the interface for easy clarity. Finally, adds a Last() method to elements for getting the last element in the slice.

---

#### Does this PR need a docs update or release note?

- [x]  No

#### Type of change

- [x] 🧹 Tech Debt/Cleanup

#### Test Plan

- [x]  Unit test
- [x] 💚 E2E
2023-05-12 01:29:17 +00:00

98 lines
2.4 KiB
Go

package path
import (
"fmt"
"github.com/alcionai/clues"
"github.com/alcionai/corso/src/internal/common/pii"
)
var piiSafePathElems = pii.MapWithPlurals(
// services
UnknownService.String(),
ExchangeService.String(),
OneDriveService.String(),
SharePointService.String(),
ExchangeMetadataService.String(),
OneDriveMetadataService.String(),
SharePointMetadataService.String(),
// categories
UnknownCategory.String(),
EmailCategory.String(),
ContactsCategory.String(),
EventsCategory.String(),
FilesCategory.String(),
ListsCategory.String(),
LibrariesCategory.String(),
PagesCategory.String(),
DetailsCategory.String(),
)
var (
// interface compliance required for handling PII
_ clues.Concealer = &Elements{}
_ fmt.Stringer = &Elements{}
// interface compliance for the observe package to display
// values without concealing PII.
_ clues.PlainStringer = &Elements{}
)
// Elements are a PII Concealer-compliant slice of elements within a path.
type Elements []string
// NewElements creates a new Elements slice by splitting the provided string.
func NewElements(p string) Elements {
return Split(p)
}
// Conceal produces a concealed representation of the elements, suitable for
// logging, storing in errors, and other output.
func (el Elements) Conceal() string {
escaped := make([]string, 0, len(el))
for _, e := range el {
escaped = append(escaped, escapeElement(e))
}
return join(pii.ConcealElements(escaped, piiSafePathElems))
}
// Format produces a concealed representation of the elements, even when
// used within a PrintF, suitable for logging, storing in errors,
// and other output.
func (el Elements) Format(fs fmt.State, _ rune) {
fmt.Fprint(fs, el.Conceal())
}
// String returns a string that contains all path elements joined together.
// Elements that need escaping are escaped. The result is not concealed, and
// is not suitable for logging or structured errors.
func (el Elements) String() string {
escaped := make([]string, 0, len(el))
for _, e := range el {
escaped = append(escaped, escapeElement(e))
}
return join(escaped)
}
// PlainString returns an unescaped, unmodified string of the joined elements.
// The result is not concealed, and is not suitable for logging or structured
// errors.
func (el Elements) PlainString() string {
return join(el)
}
// Last returns the last element. Returns "" if empty.
func (el Elements) Last() string {
if len(el) == 0 {
return ""
}
return el[len(el)-1]
}