corso/src/pkg/path/elements.go
Keepers 50e92b65c6
add Concealer compliance to paths (#3017)
Adds compliance with clues.Concealer to the paths
package.  Also introduces a new struct in the same
space: Elements, which is a thin wrapper around
a slice of strings so that subsections of a path
or builder can carry the same pii behavior
without additional work on the consumer's end.

---

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

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

#### Type of change

- [x] 🌻 Feature
- [x] 🤖 Supportability/Tests

#### Issue(s)

* #2024

#### Test Plan

- [x]  Unit test
- [x] 💚 E2E
2023-04-11 22:04:34 +00:00

89 lines
2.3 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)
}