Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions libs/tags/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"regexp"
"unicode"

"github.com/databricks/cli/libs/textutil"

"golang.org/x/text/unicode/rangetable"
)

Expand All @@ -20,17 +22,17 @@ var awsChars = rangetable.Merge(
var awsTag = &tag{
keyLength: 127,
keyPattern: regexp.MustCompile(`^[\d \w\+\-=\.:\/@]*$`),
keyNormalize: chain(
normalizeMarks(),
replaceNotIn(latin1, '_'),
replaceNotIn(awsChars, '_'),
keyNormalize: textutil.Chain(
textutil.NormalizeMarks(),
textutil.ReplaceNotIn(textutil.Latin1, '_'),
textutil.ReplaceNotIn(awsChars, '_'),
),

valueLength: 255,
valuePattern: regexp.MustCompile(`^[\d \w\+\-=\.:/@]*$`),
valueNormalize: chain(
normalizeMarks(),
replaceNotIn(latin1, '_'),
replaceNotIn(awsChars, '_'),
valueNormalize: textutil.Chain(
textutil.NormalizeMarks(),
textutil.ReplaceNotIn(textutil.Latin1, '_'),
textutil.ReplaceNotIn(awsChars, '_'),
),
}
12 changes: 7 additions & 5 deletions libs/tags/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package tags
import (
"regexp"

"github.com/databricks/cli/libs/textutil"

"golang.org/x/text/unicode/rangetable"
)

Expand All @@ -12,14 +14,14 @@ var azureForbiddenChars = rangetable.New('<', '>', '*', '&', '%', ';', '\\', '/'
var azureTag = &tag{
keyLength: 512,
keyPattern: regexp.MustCompile(`^[^<>\*&%;\\\/\+\?]*$`),
keyNormalize: chain(
replaceNotIn(latin1, '_'),
replaceIn(azureForbiddenChars, '_'),
keyNormalize: textutil.Chain(
textutil.ReplaceNotIn(textutil.Latin1, '_'),
textutil.ReplaceIn(azureForbiddenChars, '_'),
),

valueLength: 256,
valuePattern: regexp.MustCompile(`^.*$`),
valueNormalize: chain(
replaceNotIn(latin1, '_'),
valueNormalize: textutil.Chain(
textutil.ReplaceNotIn(textutil.Latin1, '_'),
),
}
22 changes: 12 additions & 10 deletions libs/tags/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package tags
import (
"regexp"
"unicode"

"github.com/databricks/cli/libs/textutil"
)

// Tag keys and values on GCP are limited to 63 characters and must match the
Expand Down Expand Up @@ -45,19 +47,19 @@ var gcpInner = &unicode.RangeTable{
var gcpTag = &tag{
keyLength: 63,
keyPattern: regexp.MustCompile(`^([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]$`),
keyNormalize: chain(
normalizeMarks(),
replaceNotIn(latin1, '_'),
replaceNotIn(gcpInner, '_'),
trimIfNotIn(gcpOuter),
keyNormalize: textutil.Chain(
textutil.NormalizeMarks(),
textutil.ReplaceNotIn(textutil.Latin1, '_'),
textutil.ReplaceNotIn(gcpInner, '_'),
textutil.TrimIfNotIn(gcpOuter),
),

valueLength: 63,
valuePattern: regexp.MustCompile(`^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$`),
valueNormalize: chain(
normalizeMarks(),
replaceNotIn(latin1, '_'),
replaceNotIn(gcpInner, '_'),
trimIfNotIn(gcpOuter),
valueNormalize: textutil.Chain(
textutil.NormalizeMarks(),
textutil.ReplaceNotIn(textutil.Latin1, '_'),
textutil.ReplaceNotIn(gcpInner, '_'),
textutil.TrimIfNotIn(gcpOuter),
),
}
16 changes: 0 additions & 16 deletions libs/tags/latin_test.go

This file was deleted.

14 changes: 8 additions & 6 deletions libs/tags/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ import (
"regexp"
"strings"
"unicode"

"github.com/databricks/cli/libs/textutil"
)

// The tag type holds the validation and normalization rules for
// a cloud provider's resource tags as applied by Databricks.
type tag struct {
keyLength int
keyPattern *regexp.Regexp
keyNormalize transformer
keyNormalize textutil.Transformer

valueLength int
valuePattern *regexp.Regexp
valueNormalize transformer
valueNormalize textutil.Transformer
}

func (t *tag) ValidateKey(s string) error {
Expand All @@ -27,7 +29,7 @@ func (t *tag) ValidateKey(s string) error {
if len(s) > t.keyLength {
return fmt.Errorf("key length %d exceeds maximum of %d", len(s), t.keyLength)
}
if strings.ContainsFunc(s, func(r rune) bool { return !unicode.Is(latin1, r) }) {
if strings.ContainsFunc(s, func(r rune) bool { return !unicode.Is(textutil.Latin1, r) }) {
return errors.New("key contains non-latin1 characters")
}
if !t.keyPattern.MatchString(s) {
Expand All @@ -40,7 +42,7 @@ func (t *tag) ValidateValue(s string) error {
if len(s) > t.valueLength {
return fmt.Errorf("value length %d exceeds maximum of %d", len(s), t.valueLength)
}
if strings.ContainsFunc(s, func(r rune) bool { return !unicode.Is(latin1, r) }) {
if strings.ContainsFunc(s, func(r rune) bool { return !unicode.Is(textutil.Latin1, r) }) {
return errors.New("value contains non-latin1 characters")
}
if !t.valuePattern.MatchString(s) {
Expand All @@ -50,9 +52,9 @@ func (t *tag) ValidateValue(s string) error {
}

func (t *tag) NormalizeKey(s string) string {
return t.keyNormalize.transform(s)
return t.keyNormalize.TransformString(s)
}

func (t *tag) NormalizeValue(s string) string {
return t.valueNormalize.transform(s)
return t.valueNormalize.TransformString(s)
}
87 changes: 0 additions & 87 deletions libs/tags/transform.go

This file was deleted.

25 changes: 0 additions & 25 deletions libs/tags/transform_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions libs/tags/latin.go → libs/textutil/latin.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package tags
package textutil

import "unicode"

// Range table for all characters in the Latin1 character set.
var latin1 = &unicode.RangeTable{
var Latin1 = &unicode.RangeTable{
R16: []unicode.Range16{
{0x0000, 0x00ff, 1},
},
Expand Down
16 changes: 16 additions & 0 deletions libs/textutil/latin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package textutil

import (
"testing"
"unicode"

"github.com/stretchr/testify/assert"
)

func TestLatinTable(t *testing.T) {
assert.True(t, unicode.In('\u0000', Latin1))
assert.True(t, unicode.In('A', Latin1))
assert.True(t, unicode.In('Z', Latin1))
assert.True(t, unicode.In('\u00ff', Latin1))
assert.False(t, unicode.In('\u0100', Latin1))
}
Loading
Loading