Skip to content

Commit

Permalink
Rename sanitise to sanitize (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
prateek authored and robskillington committed Jul 31, 2017
1 parent ae42cdc commit 2605c40
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 77 deletions.
4 changes: 2 additions & 2 deletions m3/sanitise.go → m3/sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
)

var (
// DefaultSanitiserOpts are the options for the default M3 Sanitiser
DefaultSanitiserOpts = tally.SanitiseOptions{
// DefaultSanitizerOpts are the options for the default M3 Sanitizer
DefaultSanitizerOpts = tally.SanitizeOptions{
NameCharacters: tally.ValidCharacters{
Ranges: tally.AlphanumericRange,
Characters: tally.UnderscoreDashDotCharacters,
Expand Down
70 changes: 35 additions & 35 deletions sanitise.go → sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var (
DefaultReplacementCharacter = '_'

// AlphanumericRange is the range of alphanumeric characters.
AlphanumericRange = []SanitiseRange{
AlphanumericRange = []SanitizeRange{
{rune('a'), rune('z')},
{rune('A'), rune('Z')},
{rune('0'), rune('9')}}
Expand All @@ -49,78 +49,78 @@ var (
'_'}
)

// SanitiseFn returns a sanitised version of the input string.
type SanitiseFn func(string) string
// SanitizeFn returns a sanitized version of the input string.
type SanitizeFn func(string) string

// SanitiseRange is a range of characters (inclusive on both ends).
type SanitiseRange [2]rune
// SanitizeRange is a range of characters (inclusive on both ends).
type SanitizeRange [2]rune

// ValidCharacters is a collection of valid characters.
type ValidCharacters struct {
Ranges []SanitiseRange
Ranges []SanitizeRange
Characters []rune
}

// SanitiseOptions are the set of configurable options for sanitisation.
type SanitiseOptions struct {
// SanitizeOptions are the set of configurable options for sanitisation.
type SanitizeOptions struct {
NameCharacters ValidCharacters
KeyCharacters ValidCharacters
ValueCharacters ValidCharacters
ReplacementCharacter rune
}

// Sanitiser sanitises the provided input based on the function executed.
type Sanitiser interface {
// Name sanitises the provided `name` string.
// Sanitizer sanitizes the provided input based on the function executed.
type Sanitizer interface {
// Name sanitizes the provided `name` string.
Name(n string) string

// Key sanitises the provided `key` string.
// Key sanitizes the provided `key` string.
Key(k string) string

// Value sanitises the provided `value` string.
// Value sanitizes the provided `value` string.
Value(v string) string
}

// NewSanitiser returns a new sanitiser based on provided options.
func NewSanitiser(opts SanitiseOptions) Sanitiser {
return sanitiser{
nameFn: opts.NameCharacters.sanitiseFn(opts.ReplacementCharacter),
keyFn: opts.KeyCharacters.sanitiseFn(opts.ReplacementCharacter),
valueFn: opts.ValueCharacters.sanitiseFn(opts.ReplacementCharacter),
// NewSanitizer returns a new sanitizer based on provided options.
func NewSanitizer(opts SanitizeOptions) Sanitizer {
return sanitizer{
nameFn: opts.NameCharacters.sanitizeFn(opts.ReplacementCharacter),
keyFn: opts.KeyCharacters.sanitizeFn(opts.ReplacementCharacter),
valueFn: opts.ValueCharacters.sanitizeFn(opts.ReplacementCharacter),
}
}

// NoOpSanitiseFn returns the input un-touched.
func NoOpSanitiseFn(v string) string { return v }
// NoOpSanitizeFn returns the input un-touched.
func NoOpSanitizeFn(v string) string { return v }

// NewNoOpSanitiser returns a sanitiser which returns all inputs un-touched.
func NewNoOpSanitiser() Sanitiser {
return sanitiser{
nameFn: NoOpSanitiseFn,
keyFn: NoOpSanitiseFn,
valueFn: NoOpSanitiseFn,
// NewNoOpSanitizer returns a sanitizer which returns all inputs un-touched.
func NewNoOpSanitizer() Sanitizer {
return sanitizer{
nameFn: NoOpSanitizeFn,
keyFn: NoOpSanitizeFn,
valueFn: NoOpSanitizeFn,
}
}

type sanitiser struct {
nameFn SanitiseFn
keyFn SanitiseFn
valueFn SanitiseFn
type sanitizer struct {
nameFn SanitizeFn
keyFn SanitizeFn
valueFn SanitizeFn
}

func (s sanitiser) Name(n string) string {
func (s sanitizer) Name(n string) string {
return s.nameFn(n)
}

func (s sanitiser) Key(k string) string {
func (s sanitizer) Key(k string) string {
return s.keyFn(k)
}

func (s sanitiser) Value(v string) string {
func (s sanitizer) Value(v string) string {
return s.valueFn(v)
}

func (c *ValidCharacters) sanitiseFn(repChar rune) SanitiseFn {
func (c *ValidCharacters) sanitizeFn(repChar rune) SanitizeFn {
return func(value string) string {
var buf *bytes.Buffer
for idx, ch := range value {
Expand Down
12 changes: 6 additions & 6 deletions sanitise_test.go → sanitize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ import (
"github.com/stretchr/testify/require"
)

func newTestSanitiser() SanitiseFn {
func newTestSanitizer() SanitizeFn {
c := &ValidCharacters{
Ranges: AlphanumericRange,
Characters: UnderscoreDashCharacters,
}
return c.sanitiseFn(DefaultReplacementCharacter)
return c.sanitizeFn(DefaultReplacementCharacter)
}

func TestSanitiseIdentifierAllValidCharacters(t *testing.T) {
func TestSanitizeIdentifierAllValidCharacters(t *testing.T) {
allValidChars := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
fn := newTestSanitiser()
fn := newTestSanitizer()
require.Equal(t, allValidChars, fn(allValidChars))
}

func TestSanitiseTestCases(t *testing.T) {
fn := newTestSanitiser()
func TestSanitizeTestCases(t *testing.T) {
fn := newTestSanitizer()
type testCase struct {
input string
output string
Expand Down
50 changes: 25 additions & 25 deletions scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type scope struct {
cachedReporter CachedStatsReporter
baseReporter BaseStatsReporter
defaultBuckets Buckets
sanitiser Sanitiser
sanitizer Sanitizer

registry *scopeRegistry
status scopeStatus
Expand Down Expand Up @@ -101,7 +101,7 @@ type ScopeOptions struct {
CachedReporter CachedStatsReporter
Separator string
DefaultBuckets Buckets
SanitiseOptions *SanitiseOptions
SanitizeOptions *SanitizeOptions
}

// NewRootScope creates a new root Scope with a set of options and
Expand All @@ -123,9 +123,9 @@ func NewTestScope(
}

func newRootScope(opts ScopeOptions, interval time.Duration) *scope {
sanitiser := NewNoOpSanitiser()
if o := opts.SanitiseOptions; o != nil {
sanitiser = NewSanitiser(*o)
sanitizer := NewNoOpSanitizer()
if o := opts.SanitizeOptions; o != nil {
sanitizer = NewSanitizer(*o)
}

if opts.Tags == nil {
Expand All @@ -147,13 +147,13 @@ func newRootScope(opts ScopeOptions, interval time.Duration) *scope {
}

s := &scope{
separator: sanitiser.Name(opts.Separator),
prefix: sanitiser.Name(opts.Prefix),
separator: sanitizer.Name(opts.Separator),
prefix: sanitizer.Name(opts.Prefix),
reporter: opts.Reporter,
cachedReporter: opts.CachedReporter,
baseReporter: baseReporter,
defaultBuckets: opts.DefaultBuckets,
sanitiser: sanitiser,
sanitizer: sanitizer,

registry: &scopeRegistry{
subscopes: make(map[string]*scope),
Expand All @@ -171,7 +171,7 @@ func newRootScope(opts ScopeOptions, interval time.Duration) *scope {

// NB(r): Take a copy of the tags on creation
// so that it cannot be modified after set.
s.tags = s.copyAndSanitiseMap(opts.Tags)
s.tags = s.copyAndSanitizeMap(opts.Tags)

// Register the root scope
s.registry.subscopes[scopeRegistryKey(s.prefix, s.tags)] = s
Expand Down Expand Up @@ -277,7 +277,7 @@ func (s *scope) reportRegistryWithLock() {
}

func (s *scope) Counter(name string) Counter {
name = s.sanitiser.Name(name)
name = s.sanitizer.Name(name)
s.cm.RLock()
val, ok := s.counters[name]
s.cm.RUnlock()
Expand All @@ -300,7 +300,7 @@ func (s *scope) Counter(name string) Counter {
}

func (s *scope) Gauge(name string) Gauge {
name = s.sanitiser.Name(name)
name = s.sanitizer.Name(name)
s.gm.RLock()
val, ok := s.gauges[name]
s.gm.RUnlock()
Expand All @@ -323,7 +323,7 @@ func (s *scope) Gauge(name string) Gauge {
}

func (s *scope) Timer(name string) Timer {
name = s.sanitiser.Name(name)
name = s.sanitizer.Name(name)
s.tm.RLock()
val, ok := s.timers[name]
s.tm.RUnlock()
Expand All @@ -348,7 +348,7 @@ func (s *scope) Timer(name string) Timer {
}

func (s *scope) Histogram(name string, b Buckets) Histogram {
name = s.sanitiser.Name(name)
name = s.sanitizer.Name(name)

if b == nil {
b = s.defaultBuckets
Expand Down Expand Up @@ -378,12 +378,12 @@ func (s *scope) Histogram(name string, b Buckets) Histogram {
}

func (s *scope) Tagged(tags map[string]string) Scope {
tags = s.copyAndSanitiseMap(tags)
tags = s.copyAndSanitizeMap(tags)
return s.subscope(s.prefix, tags)
}

func (s *scope) SubScope(prefix string) Scope {
prefix = s.sanitiser.Name(prefix)
prefix = s.sanitizer.Name(prefix)
return s.subscope(s.fullyQualifiedName(prefix), nil)
}

Expand Down Expand Up @@ -417,7 +417,7 @@ func (s *scope) subscope(prefix string, immutableTags map[string]string) Scope {
cachedReporter: s.cachedReporter,
baseReporter: s.baseReporter,
defaultBuckets: s.defaultBuckets,
sanitiser: s.sanitiser,
sanitizer: s.sanitizer,
registry: s.registry,

counters: make(map[string]*counter),
Expand Down Expand Up @@ -521,26 +521,26 @@ func (s *scope) Close() error {
return nil
}

// NB(prateek): We assume concatenation of sanitised inputs is
// sanitised. If that stops being true, then we need to sanitise the
// NB(prateek): We assume concatenation of sanitized inputs is
// sanitized. If that stops being true, then we need to sanitize the
// output of this function.
func (s *scope) fullyQualifiedName(name string) string {
if len(s.prefix) == 0 {
return name
}
// NB: we don't need to sanitise the output of this function as we
// sanitise all the the inputs (prefix, separator, name); and the
// output we're creating is a concatenation of the sanitised inputs.
// NB: we don't need to sanitize the output of this function as we
// sanitize all the the inputs (prefix, separator, name); and the
// output we're creating is a concatenation of the sanitized inputs.
// If we change the concatenation to involve other inputs or characters,
// we'll need to sanitise them too.
// we'll need to sanitize them too.
return fmt.Sprintf("%s%s%s", s.prefix, s.separator, name)
}

func (s *scope) copyAndSanitiseMap(tags map[string]string) map[string]string {
func (s *scope) copyAndSanitizeMap(tags map[string]string) map[string]string {
result := make(map[string]string, len(tags))
for k, v := range tags {
k = s.sanitiser.Key(k)
v = s.sanitiser.Value(v)
k = s.sanitizer.Key(k)
v = s.sanitizer.Value(v)
result[k] = v
}
return result
Expand Down
4 changes: 2 additions & 2 deletions scope_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ func BenchmarkCounterAllocation(b *testing.B) {
}
}

func BenchmarkSanitisedCounterAllocation(b *testing.B) {
func BenchmarkSanitizedCounterAllocation(b *testing.B) {
root, _ := NewRootScope(ScopeOptions{
Prefix: "funkytown",
Reporter: NullStatsReporter,
SanitiseOptions: &alphanumericSanitiserOpts,
SanitizeOptions: &alphanumericSanitizerOpts,
}, 0)
s := root.(*scope)

Expand Down
14 changes: 7 additions & 7 deletions scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ import (
)

var (
// alphanumericSanitiserOpts is the options to create a sanitiser which uses
// the alphanumeric SanitiseFn.
alphanumericSanitiserOpts = SanitiseOptions{
// alphanumericSanitizerOpts is the options to create a sanitizer which uses
// the alphanumeric SanitizeFn.
alphanumericSanitizerOpts = SanitizeOptions{
NameCharacters: ValidCharacters{
Ranges: AlphanumericRange,
Characters: UnderscoreDashCharacters,
Expand Down Expand Up @@ -365,12 +365,12 @@ func TestWriteOnce(t *testing.T) {
assert.Nil(t, r.timers["ticky"])
}

func TestCounterSanitised(t *testing.T) {
func TestCounterSanitized(t *testing.T) {
r := newTestStatsReporter()

root, closer := NewRootScope(ScopeOptions{
Reporter: r,
SanitiseOptions: &alphanumericSanitiserOpts,
SanitizeOptions: &alphanumericSanitizerOpts,
}, 0)
defer closer.Close()

Expand Down Expand Up @@ -595,15 +595,15 @@ func TestTaggedSubScope(t *testing.T) {
}, r.histograms["foo.bar"].tags)
}

func TestTaggedSanitisedSubScope(t *testing.T) {
func TestTaggedSanitizedSubScope(t *testing.T) {
r := newTestStatsReporter()

ts := map[string]string{"env": "test:env"}
root, closer := NewRootScope(ScopeOptions{
Prefix: "foo",
Tags: ts,
Reporter: r,
SanitiseOptions: &alphanumericSanitiserOpts,
SanitizeOptions: &alphanumericSanitizerOpts,
}, 0)
defer closer.Close()

Expand Down

0 comments on commit 2605c40

Please sign in to comment.